Saskia Bobinska
Senior 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')
},
}
}
}
Senior Support Engineer @Sanity
Use a migration script to update all references from one document to another
Go to Update references via Content Migration scriptIf 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 Values