TracksMastering Content OperationsCoursesHandling schema changes confidentlyValidating documents in bulk
Handling schema changes confidently
Markdown Version

Validating documents in bulk

Log in to watch a video walkthrough of this lesson
Log in
Video thumbnail
Check document validation status across a whole dataset. Update schema with missing fields for content that's found in the dataset's documents.
Log in to mark your progress for each Lesson and Task

Since you have only three document schema types, it’s not that hard to get a sense of what’s going on from looking through the documents. Most projects with some maturity might have a lot of schema types and a lot of content. This is where it becomes useful to use the Sanity CLI to investigate mismatches between your content and the schema that is supposed to describe it.

Run the following to validate all documents from your terminal
# in apps/studio
pnpm dlx sanity@latest documents validate

The CLI will give you a small warning about potential pitfalls with this command, if you want to skip this warning in the future, you can run the following:

# in apps/studio
pnpm dlx sanity@latest documents validate -y

When the command has run, you may have a list of warnings or errors about missing field values:

Validation results:
✔ Valid: 408 documents
✖ Errors: 2 documents, 2 errors
⚠ Warnings: 2 documents, 2 warnings
ERROR event 57c1561e-378c-4124-9aa2-88c0db96a037
└─ slug .......................... ⚠ Required
└─ current ..................... ✖ Required
ERROR event drafts.57c1561e-378c-4124-9aa2-88c0db96a037
└─ slug .......................... ⚠ Required
└─ current ..................... ✖ Required

This means there is content in your dataset which is not aligned with the validation rules in your Studio schema. In the instance of our error above, there's one document that doesn't have a slug field value.

You can copy a document ID from the terminal into the search box of your studio to find and fix each document manually.

If you had validation errors, and have now fixed them, re-run:

# in apps/studio
pnpm dlx sanity@latest documents validate -y

And you should see output like:

Validation results:
✔ Valid: 408 documents
✖ Errors: 0 documents, 0 errors
⚠ Warnings: 0 documents, 0 warnings

Great! You now have a dataset full of valid content and the skills to ensure validation in bulk as part of future content operations.

You are now ready to do the opposite, namely, changing the schema and updating the content accordingly.

You have 1 uncompleted task in this lesson
0 of 1