Hold publishing and freeze fields with guards
Add stage guards that hold publishing and freeze fields on the subject document while a workflow instance is in a stage.
Early access
Workflows is in early access, built in public. Read How early access works before you rely on it.
A guard holds a document while a workflow instance is in one stage. You declare it on the stage. The engine writes it to the Content Lake as its own document when the instance enters the stage and removes it when the instance leaves.
This guide adds two guards to a review stage: one that holds publishing of the subject document, and one that freezes its headline and body.
Early access limitation. The engine and the Studio plugin honor a deployed guard. The Content Lake does not enforce Workflows’ temporary guard documents, so a guard does not stop a client that writes to the lake directly. Use dataset access control when a rule must hold against every writer.
Before you start, you need:
- A workflow definition with a
subjectfield, such as the one in the quick start. - The Workflows Studio plugin installed, so you can see the hold in Studio.
Hold publishing during a stage
Add a guard to the stage's guards array. This one denies publishing the subject document while the instance is in review:
import {defineGuard, defineStage} from '@sanity/workflow-engine/define'
defineStage({
name: 'review',
title: 'Editorial review',
guards: [
defineGuard({
name: 'hold-publishing',
title: 'No publishing during review',
match: {
idRefs: [{type: 'fieldRead', field: 'subject'}],
actions: ['publish'],
},
// No predicate: deny every publish while the instance is in this stage.
}),
],
// activities and transitions as before
})idRefs resolves the document to protect when the instance enters the stage. A fieldRead reads a workflow field, so here the guard protects whatever document the subject field points to. Stage and activity fields aren't available to a guard. The guard has no predicate, so it denies every publish it matches.
Freeze fields during a stage
To stop editors from changing some fields while still allowing other edits, guard the update action and add a predicate. Add this guard to the same guards array:
defineGuard({
name: 'freeze-copy',
title: 'Freeze the headline and body during review',
match: {
idRefs: [{type: 'fieldRead', field: 'subject'}],
actions: ['update'],
},
// Allow a draft edit only when it leaves title and body unchanged.
predicate: '!delta::changedAny((title, body))',
})A predicate is a GROQ expression that can read document, guard, and mutation. Only a strict true allows a matching mutation. An omitted or empty predicate denies it. Here, delta::changedAny((title, body)) is true when an edit touches either field, so the negated expression allows every other edit. See Guard predicates for the variables a predicate can read.
Choose which mutations to guard
Each guard lists one or more actions in match.actions:
update: edits to the draft.publish: publishing the draft.unpublish: unpublishing the published document.createanddelete: creating or deleting the document under the ID the guard matches.
Give each rule its own guard with its own name, as the two examples above do. Every matching guard is evaluated on its own, and one denial denies the mutation. No guard can widen what another one denies, and there is no evaluation order to depend on. A single guard whose actions span more than one group (update; publish and unpublish; create and delete) needs reader model 9 or later. Upgrade Workflows packages explains reader models.
Check that the guard is active
Deploy the definition, start an instance, and move it to the guarded stage, as in the quick start. Then open the subject document in Studio. The Publish action is disabled, and its tooltip names the workflow holding the document.
The Studio plugin checks guards only for Publish, Unpublish, and Delete. It doesn't check update guards while someone edits the form, so Studio doesn't stop edits to frozen fields. Today, only integrations that call documentActionDenials check an update guard.
To confirm the guard documents exist, query them by instance ID:
*[_type == "temp.system.guard" && sourceInstanceId == $instanceId]{
_id,
match
}The query returns one document per guard, with the resolved document IDs and Content Lake actions it matches.
Next steps
- Look up every guard property in Guard declaration.
- Learn which checks honor a guard today in Architecture, actors, and enforcement.
- Check guards from a custom integration with
documentActionDenialsin @sanity/workflow-engine on reference.sanity.io.
Visiting agent?
Workflows includes an MCP server for inspecting, operating, authoring, validating, and deploying workflows. Ask your human to set up the MCP server.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?