Understanding how to apply validation rules based on specific inputs or booleans in Sanity.io
26 replies
Last updated: Feb 26, 2021
G
Is it possible to only apply validation rules if a user fills out a specific input or checks a Boolean within the same object?
Feb 25, 2021, 10:53 PM
G
https://www.sanity.io/docs/validation the docs mention using context in custom validation methods but I can’t find out what that means or how exactly it’s working.
I also noticed it says you can access the nearest parent, does that mean you can access anything in the parent document from the object being validated?
I also noticed it says you can access the nearest parent, does that mean you can access anything in the parent document from the object being validated?
Feb 25, 2021, 11:49 PM
G
What is context here
Feb 25, 2021, 11:50 PM
G
Hi Dustin. I’m assuming a boolean checker called
triggerand the field you want to (conditionally) validate called `email`:
... { name: 'trigger', title: 'Validate', type: 'boolean', }, { name: 'email', type: 'string', title: 'Email', validation: Rule => Rule.custom((_, context) => context.document.trigger ? "Email is required" : true) }, ...
Feb 26, 2021, 12:20 AM
G
Does true validate as passing and the string is the thrown error
Feb 26, 2021, 12:30 AM
G
Correct
Feb 26, 2021, 12:31 AM
G
My solution is incomplete. It’s not resolving to true. I will update it and follow up.
Feb 26, 2021, 12:56 AM
G
Okay, you could try something like this:
validation: Rule => Rule.custom((validate, context) => (context.document.trigger && validate === undefined) ? "Email is required" : true)
Feb 26, 2021, 1:07 AM
G
did you mean to name the prop
triggerinstead of
validate?
Feb 26, 2021, 3:17 AM
G
I’m trying similar validations and I’m not getting any feedback from Sanity
Feb 26, 2021, 3:18 AM
G
No,
triggeris part of
context.document, and we are just checking if it's true.
Validateis the current field. If you want to post a snippet from your schema we can figure out the correct wording.
Feb 26, 2021, 3:20 AM
G
{ name: "title", title: "Title", description: "Use the correct title", type: "string", validation: Rule => Rule.custom((title, context) => context.document.deleted && title === undefined ? "Title is required" : true ) },
Feb 26, 2021, 3:21 AM
G
Here’s what I’m trying and it isn’t doing anything. It allows me to update the schema without a title and doesnt seem to recognize the deleted boolean
Feb 26, 2021, 3:21 AM
G
I want to be able to save the document without a title only if the boolean equal to true
Feb 26, 2021, 3:22 AM
G
Is your boolean
deleteda sibling of
title?
Feb 26, 2021, 3:24 AM
G
yeah here they are in the schema together
{ name: "title", title: "Title", description: "Use the correct title", type: "string", validation: Rule => Rule.custom((title, context) => context.document.deleted === true && title === undefined ? true : "Title required" ) }, { name: "deleted", title: "Deleted", description: "Prevent this variant from being added to the store", type: "boolean" },
Feb 26, 2021, 3:24 AM
G
If you want to allow a blank title when the boolean is true, the code will need to be reworked a bit. But I'm surprised it's not at least responding.
Feb 26, 2021, 3:25 AM
G
well now it’s just blanket requiring the title
Feb 26, 2021, 3:26 AM
G
true will let the document save in this case, right?
Feb 26, 2021, 3:26 AM
G
That's right. When it evaluates to true it should permit you to save.
Feb 26, 2021, 3:27 AM
G
context.document.deleted ? true : "Title required"
Feb 26, 2021, 3:30 AM
G
It always returns the string
Feb 26, 2021, 3:45 AM
G
that’s because it’s undefined. Just console.log it. That’s weird
Feb 26, 2021, 3:46 AM
G
it looks like
context.document.*always returns undefined.
Feb 26, 2021, 3:53 AM
G
user A
Alright, i figured it out. I always forget that my Sanity is set up a bit differently than most, so my document actually looks quite a bit different. This is what works for me{ name: "title", title: "Title", description: "Use the correct title", type: "string", validation: Rule => Rule.custom((title, context) => { const validTitle = title && title !== undefined; const isDeleted = context.document.content.main.deleted; if (isDeleted && !validTitle) { return true; } else if (validTitle) { return true; } else return "Needs a valid title"; }) },
Feb 26, 2021, 3:58 AM
G
my context is set up differently because of tabs I think.
Feb 26, 2021, 3:59 AM
G
That's good to know. Thanks Dustin. Glad you got it working.
Feb 26, 2021, 4:54 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.