๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

Validate schema script

By Espen Hovlandsdal & Rune Botten

A script to validate that your schema is free of errors and warnings

Warning

This schema is for an older version of Sanity Studio (v2), which is deprecated.

Learn how to migrate to the new Studio v3 โ†’

schema-check.js

// Run this file like this:
// npx sanity exec schema-check.js --mock-browser-env -- --strict
import schema from 'part:@sanity/base/schema'

let hasErrors = false
let hasWarnings = false

for (const item of schema._validation) {
  const path = item.path.map(segment => segment.name).join(' โ†’ ')
  console.log(`${path}:`)

  for (const problem of item.problems) {
    if (problem.severity === 'error') {
      hasErrors = true
      console.error(`  - [error] ${problem.message}`)
    } else if (problem.severity === 'warning') {
      hasWarnings = true
      console.warn(`  - [warning] ${problem.message}`)
    }
  }

  console.warn('')
}

if (hasErrors) {
  process.exit(1)
}

if (hasWarnings && process.argv.includes('--strict')) {
  process.exit(2)
}

This script is useful to run before deploying a Studio, for example as part of a build pipeline, where you can stop a deployment if there are errors or optionally warnings in the schema.

npx sanity exec schema-check.js --mock-browser-env -- --strict && do-your-deploy

Contributors

Other schemas by authors

Auto-reload Studio when changes are deployed

Drop this into your Studio to let editors know when there's a more recent version of your Studio available, making sure they have the latest fields and validations.

Go to Auto-reload Studio when changes are deployed