Joint session with Vercel: How to build intelligent storefronts (May 15th)

Validate schema script

Studio v2

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

By Espen Hovlandsdal & Rune Botten


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

Espen Hovlandsdal

Open-sourceror @ Sanity.io

United States

Visit Espen Hovlandsdal's profile

Rune Botten

I find solutions

Visit Rune Botten's profile