✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Understanding how to apply validation rules based on specific inputs or booleans in Sanity.io

26 replies
Last updated: Feb 26, 2021
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
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?
Feb 25, 2021, 11:49 PM
What is context here
Feb 25, 2021, 11:50 PM
Hi Dustin. I’m assuming a boolean checker called
trigger
and 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
Does true validate as passing and the string is the thrown error
Feb 26, 2021, 12:30 AM
Correct
Feb 26, 2021, 12:31 AM
My solution is incomplete. It’s not resolving to true. I will update it and follow up.
Feb 26, 2021, 12:56 AM
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
did you mean to name the prop
trigger
instead of
validate
?
Feb 26, 2021, 3:17 AM
I’m trying similar validations and I’m not getting any feedback from Sanity
Feb 26, 2021, 3:18 AM
No,
trigger
is part of
context.document
, and we are just checking if it's true.
Validate
is 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
{
      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
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
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
Is your boolean
deleted
a sibling of
title
?
Feb 26, 2021, 3:24 AM
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
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
well now it’s just blanket requiring the title
Feb 26, 2021, 3:26 AM
true will let the document save in this case, right?
Feb 26, 2021, 3:26 AM
That's right. When it evaluates to true it should permit you to save.
Feb 26, 2021, 3:27 AM
context.document.deleted ? true : "Title required"
this wont even let me publish
Feb 26, 2021, 3:30 AM
It always returns the string
Feb 26, 2021, 3:45 AM
that’s because it’s undefined. Just console.log it. That’s weird
Feb 26, 2021, 3:46 AM
it looks like
context.document.*
always returns undefined.
Feb 26, 2021, 3:53 AM
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
my context is set up differently because of tabs I think.
Feb 26, 2021, 3:59 AM
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.

Was this answer helpful?