Saskia Bobinska
Support Engineer @Sanity
Saskia is located at Berlin Germany
When creating custom document actions, it can be necessary to check, wether all validation rules are full-filled.
// sanity.config.ts
...,
document: {
actions: (actions, context) => {
// we can even only add this action to a specific subset of doc types
if (context.schemaType === 'article') return [...actions, creatValidationAction(context)]
else return actions
},
},
...
// creatValidationAction.ts
import {isValidationErrorMarker} from '@sanity/types'
import {
DocumentActionComponent,
DocumentActionProps,
DocumentActionsContext,
useValidationStatus,
} from 'sanity'
export function createPreviewEmailAction(context: DocumentActionsContext): DocumentActionComponent {
// this action function has to return a function, that in turn returns an object
const id = context.documentId as string
const schemaType = context.schemaType as string
return (props: DocumentActionProps) => {
// check if Validation is ongoing and get the errors (and check if there are true errors)
const validationStatus = useValidationStatus(id, schemaType)
const hasValidationErrors = validationStatus.validation.some(isValidationErrorMarker)
return {
label: 'Validation Based Action!',
disabled: hasValidationErrors,
onHandle: () => {
// add your custom logic here
console.log('This is validated and can be executed')
},
}
}
}
Support Engineer @Sanity
If you install plugins or other packages with conflicting (peer) dependencies, you can use this useful workaround to unblock yourself.
Go to What to do when Studio crashes due to conflicts in dependency versions?Only return a value when a condition is fulfilled
Go to Conditional values in GROQ queriesIf you want to make it possible to use parameterised initialValue templates in reference fields, this is how!
Go to Create a new reference document with parameterised initial ValuesSometimes you need to change only one key-value-pair in your data, this is how you can do it in 2 lines
Go to Create a copy of an object and change a key-value pair