# @sanity/validation

> Document validation for Sanity schemas

Latest version **6.17.0** (published 2026-09-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sanity/validation
pnpm add @sanity/validation
yarn add @sanity/validation
bun add @sanity/validation
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.17.0 |
| Published | 2026-09-29 |
| First published | 2018-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22.12 |
| Dependencies | 9 |
| Unpacked size | 213.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 6281 |
| Author | Sanity.io <hello@sanity.io> |
| Maintainers | sanity-svc.npm, sanity-io |
| Keywords | cms, content, headless, sanity, validation |

## Links

- npm: https://www.npmjs.com/package/@sanity/validation
- Repository: https://github.com/sanity-io/sanity
- Homepage: https://www.sanity.io/
- Issues: https://github.com/sanity-io/sanity/issues
- npm.io page: https://npm.io/package/@sanity/validation

## Dependencies (9)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.2
- [dequal](https://npm.io/package/dequal.md) ^2.0.3
- [i18next](https://npm.io/package/i18next.md) ^26.4.2
- [lodash-es](https://npm.io/package/lodash-es.md) ^4.18.1
- [@sanity/util](https://npm.io/package/@sanity/util.md) 6.17.0
- [@sanity/types](https://npm.io/package/@sanity/types.md) 6.17.0
- [@sanity/client](https://npm.io/package/@sanity/client.md) ^8.7.0
- [@sanity/schema](https://npm.io/package/@sanity/schema.md) 6.17.0
- [@sanity/id-utils](https://npm.io/package/@sanity/id-utils.md) ^1.1.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 6.17.0 (latest) — 2026-09-29
- 6.18.0-next.25 (next) — 2026-10-02
- 6.15.0 (stable) — 2026-09-17
- 2.36.6 (v2) — 2024-11-07
- 2.36.2-weighted-search.16 (weighted-search) — 2023-07-14
- 3.14.2-i18n.10 (i18n) — 2023-07-05
- 3.12.3-purple-fairy.34 (purple-fairy) — 2023-06-23
- 3.10.2-alpha.13 (alpha) — 2023-05-11
- 3.10.1-canary.1 (canary) — 2023-05-04
- 3.9.2-findability.52 (findability) — 2023-05-03
- 3.9.1-windTestInit4.32 (windTestInit4) — 2023-05-03
- 3.9.1-windTestInit3.31 (windTestInit3) — 2023-05-02
- 3.9.1-windTestInit2.28 (windTestInit2) — 2023-05-02
- 3.9.1-windTestInit.22 (windTestInit) — 2023-05-02
- 3.8.2-annotationfix.10 (annotationfix) — 2023-03-31
- … 851 more at https://npm.io/package/@sanity/validation/versions

## README

# @sanity/validation

Validates complete Sanity documents against a compiled Sanity schema.

```ts
import {validateDocument, validationMarkerCodes} from '@sanity/validation'

const result = await validateDocument({document, schema, client})

for (const marker of result.markers) {
  if (marker.code === validationMarkerCodes.stringMinimumLength) {
    const {actualLength, minimumLength} = marker.details || {}
    if (typeof actualLength === 'number' && typeof minimumLength === 'number') {
      console.log(`Expected at least ${minimumLength} characters, got ${actualLength}`)
    }
  }
}

const summary = result.markers
  .map((marker) => {
    const path = marker.path
      .map((segment) => (typeof segment === 'object' ? segment._key : segment))
      .join('.')

    return `[${marker.level}] ${path || '<document>'}: ${marker.message} (${marker.code})`
  })
  .join('\n')
```

Every failed marker includes a stable machine-readable `code` alongside its localized `message`,
`level`, and `path`. Built-in failures may also include structured `details`. Custom validators can
return their own `code` and `details`; custom codes should be namespaced, for example
`custom.seo-title`.

When a check cannot run, `result.status` is `notEvaluated`. Omitting `client` disables custom
callbacks and skips network checks. Pass `customValidation: false` to disable custom callbacks while
still providing a client.

Pass one `AbortSignal` to cancel validation and its pending network work. Built-in checks and client
requests made through a custom validator's `context.getClient()` inherit this signal. A custom
`getDocumentExists` callback receives it as an argument; custom work using another API should pass
`context.signal` to that API. Cancellation rejects with the signal's reason (an `AbortError` when
no custom reason was supplied). Work that does not accept an abort signal cannot be stopped, even
though validation itself rejects immediately.

```ts
const controller = new AbortController()
const validation = validateDocument({document, schema, client, signal: controller.signal})
const reason = new Error('Validation cancelled')

controller.abort(reason)
try {
  await validation
} catch (error) {
  if (error !== reason) throw error
}
```

The package does not apply mutations or decide whether a document may be edited or published.

## Migrating from `sanity`

Add `@sanity/validation` as a direct dependency. The workspace-based API is available as a
deprecated compatibility helper, so call sites that only import the validation function can
migrate by changing the imported symbol:

```ts
import {validateDocumentWithWorkspace} from '@sanity/validation'

const markers = await validateDocumentWithWorkspace({document, workspace})
```

Call sites that also import `ValidateDocumentOptions` from `sanity` should use
`ValidateDocumentWorkspaceOptions` for the workspace-shaped options.

Prefer `validateDocument({document, schema, client})` for new code.

---
_Source: https://npm.io/package/@sanity/validation · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
