How to Implement Custom Document Validation Messages

2 replies
Last updated: Jul 5, 2022
Hello everyone, I have a question regarding validation at document level.While reading the doc (
https://www.sanity.io/docs/validation ), I realized there’s no info about custom validation messages when you want to address a specific field in the document, when validation triggers an error.I tried to implement the Validating children code as return value, but first I don’t have to validate an array, and second it’s not working (
https://www.sanity.io/docs/validation#9e69d5db6f72 ).At the moment, my return condition look like this and I can see the validation error on a document level (top bar) and not on the desired field.

...
return myCondition
      ? ({ message, paths: [{ name: 'name-of-the-field' }] })
      : true;
Does someone have experience with this scenario?
Jul 5, 2022, 7:59 AM
Reading Sanity code
https://github.com/sanity-io/sanity/blob/next/packages/%40sanity/types/src/paths/types.ts
https://github.com/sanity-io/sanity/blob/next/packages/%40sanity/types/src/paths/types.ts it seems that document level validation is done by passing the paths like the following.
paths: ['name-of-field']
Jul 5, 2022, 9:25 AM
Hello Andrea,could you maybe tell us exactly what you are trying to validate? If you’re not validating arrays, you don’t need the paths, which should be fine with field based validation only.
🙂
Jul 5, 2022, 11:12 AM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in