Skip to content

Schema Impact

See what a schema change does to existing content before you merge: affected documents, broken app queries and new validation errors, in one comment on every pull request. A CLI and GitHub Action that only needs a Viewer token and never writes to your dataset.

By Shehab Emon

Install command

npx sanity-schema-impact --studio ./studio

sanity-schema-impact

npm CI License: MIT

Sanity schemas live in code, and content lives in the Content Lake. Changing the schema never changes the documents that already exist. Removed fields keep their data, removed types leave documents Studio can't show, and new validation rules fail on old documents without any warning. sanity documents validate checks a dataset against the current schema only. It doesn't compare two schemas, and it isn't meant to run on every pull request.

sanity-schema-impact runs on each pull request and reports what the change does to the content already in the dataset. It diffs the base and head schemas, counts the documents each change affects (drafts and release versions included), and lists the validation problems the change introduces.

What you get

One comment on the pull request, updated on every push. This one is from a real pull request against a test dataset of about 500 documents:

The schema impact comment on a pull request: removing two post fields leaves data in 360 and 249 documents, making the slug required affects 16 documents, one app query reads both removed fields, and a new 60-character title limit fails on 3 documents

It catches removed fields and types that still hold data, fields that became required, type changes, possible renames, changed option lists, and new validation errors from rules such as max(40) or custom validators. It can also list the queries in your app that read what the change removes.

Setup

It takes about five minutes, and it adds about a minute to each pull request's CI run.

1. Create a read-only token. At sanity.io/manage, open your project, go to API, then Tokens, and add a token with the Viewer role. In GitHub, save it as a repository secret named SANITY_AUTH_TOKEN (Settings, then Secrets and variables, then Actions).

2. Add the workflow. Create .github/workflows/schema-impact.yml:

name: Schema impact

on:
  pull_request:

# A newer push cancels the older run, so two runs never race on the comment.
concurrency:
  group: schema-impact-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  pull-requests: write

jobs:
  schema-impact:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7.0.1
      - uses: actions/setup-node@v7.1.0
        with:
          node-version: 22
          cache: npm # reuses downloaded packages between runs
      - run: npm ci # installs the Studio's dependencies
      - uses: shehabemon/sanity-schema-impact@v0
        env:
          SANITY_AUTH_TOKEN: ${{ secrets.SANITY_AUTH_TOKEN }}
          SANITY_STUDIO_PROJECT_ID: abc123xy # your project ID
          SANITY_STUDIO_DATASET: production
        with:
          studio: studio # the folder with sanity.config.ts

Change three values: your project ID, your dataset, and the Studio folder (. if sanity.config.ts is at the repository root). The project ID isn't a secret, so it can sit in the file. If sanity.config.ts defines several workspaces, also add workspace: <name> under with:. To check staging as well, list both datasets: SANITY_STUDIO_DATASET: production,staging. To compare with the schema bound to the dataset instead of main, set base: deployed, which has limits listed in the reference.

Studio embedded in a Next.js app? Point studio: at the app folder that holds sanity.config.ts. If the config reads NEXT_PUBLIC_SANITY_PROJECT_ID and NEXT_PUBLIC_SANITY_DATASET, as the npm create sanity setup does, add both under env: and list them in pass-env:. The Studio runs with a clean environment, so it only sees the variables you name. There's an example in the reference.

3. Open a pull request that changes the schema. The comment appears when the job finishes.

Using pnpm? Swap the setup-node and npm ci steps for these. pnpm/action-setup has to come first, so setup-node can cache the pnpm store:

- uses: pnpm/action-setup@v6.1.1
- uses: actions/setup-node@v7.1.0
  with:
    node-version: 22
    cache: pnpm
- run: pnpm install --frozen-lockfile
yarn, bun, or a Studio with its own lockfile

The Studio's dependencies only need to be installed before the action runs, with whatever your repository uses:

# yarn
- uses: actions/setup-node@v7.1.0
  with:
    node-version: 22
    cache: yarn
- run: yarn install --frozen-lockfile

# bun
- uses: oven-sh/setup-bun@v2
- run: bun install --frozen-lockfile

# a Studio in a subfolder with its own package-lock.json
- uses: actions/setup-node@v7.1.0
  with:
    node-version: 22
    cache: npm
    cache-dependency-path: studio/package-lock.json
- run: npm ci
  working-directory: studio

Yarn's Plug'n'Play mode isn't supported, because it leaves no node_modules for the Sanity CLI to run from. nodeLinker: node-modules works.

To skip pull requests that don't touch the Studio, add a paths filter under pull_request:, as shown in the reference. Most of the job's time is installing dependencies, so this saves the most.

Block the merge, or only warn

By default the check fails when a change leaves data behind, makes a field required for documents that lack it, changes a field's type where documents still use the old type, or introduces validation errors. Whether a failing check blocks the merge is up to your branch protection: it only blocks if you make it a required status check.

Choose what fails with fail-on. Every rule that matches but isn't listed still shows in the comment, as a warning:

with:
  studio: studio
  fail-on: data-loss # only lost data fails; new validation errors are warnings
  # fail-on: none    # never fail, just report

To block by default but let a deliberate change through, name a label. When the pull request has it, the check passes, and the comment still lists what the change does and names the label that accepted it:

on:
  pull_request:
    types: [opened, synchronize, reopened, labeled, unlabeled]

# ...
with:
  studio: studio
  allow-label: schema-change-approved

The labeled and unlabeled types make the check run again when the label is added or removed. Anyone who can label pull requests can accept a change this way. The rules are listed in the reference.

Check the queries in your app

If the same repository holds the site that reads the content, add its folder under with::

with:
  studio: studio
  queries: apps/web

The check then finds the GROQ queries in that folder, types each one against the old and the new schema, and lists the ones that read a removed field, a deleted type or a field whose type changed, with the file and line. It finds groq tagged templates, defineQuery calls and plain strings that start with *[, and fills in fragments from constants, imports and @/ paths. Your app's code is only read, never run.

Run it locally

From the branch with your schema change:

SANITY_AUTH_TOKEN=<viewer token> npx sanity-schema-impact --studio ./studio --project abc123xy --dataset production

It compares against main by default (--base <branch> to change it) and prints the same report as a table. With pnpm, use pnpm dlx sanity-schema-impact in place of npx sanity-schema-impact.

Safe to run

  • Read-only. It needs a Viewer token, which can't write to the dataset. The tool never writes content, and migrations are generated as files you review and run yourself.
  • The token stays hidden. It is never printed in a report or a log.
  • Forks are skipped. Pull requests from forks get no secrets, so the check skips with a warning and passes.
  • Verifiable releases. npm versions are published from CI with provenance, linked to the commit they were built from.

Custom validators in your Studio run during the check, with the token in their environment. The security notes explain why a dedicated Viewer token makes that safe. To report a security issue, see the security policy.

Requirements

  • Sanity Studio 3.33 or newer (tested on 3.79 and 6.25), standalone, in a monorepo, or embedded in a Next.js app
  • Node 22.12 or newer, with npm, pnpm, yarn or bun
  • GitHub Actions for the pull request comment. The CLI runs in any CI, and prints Markdown or JSON.
  • Large datasets: each run validates the whole dataset. See what a run costs.

The supported setups list what works and what doesn't.

Documentation

License

MIT