@scalar/json-schema-validator
v0.1.2
Published
Validate documents against a JSON Schema with Ajv and human-friendly errors
Downloads
226,027
Readme
Scalar JSON Schema Validator
Validate documents against a JSON Schema with Ajv and get short, human-friendly error messages.
This is the shared validation engine behind @scalar/openapi-validator. It knows nothing about OpenAPI or AsyncAPI, so you can use it with any JSON Schema.
Scalar is an open-source API platform for teams who want beautiful developer interfaces without vendor lock-in.
- API References — Interactive API documentation from OpenAPI and AsyncAPI specs.
- Developer Docs — Write in Markdown/MDX, generate API references, sync with two-way Git.
- SDK Generator — Type-safe SDKs and CLIs in TypeScript, Python, Go, PHP, Java, and Ruby.
- API Client — Open-source, offline-first Postman alternative built on OpenAPI.
20M+ monthly npm installs · 15,500+ GitHub stars · MIT licensed · scalar.com
Installation
npm add @scalar/json-schema-validatorUsage
Pass a document (an object, or a JSON/YAML string) and a JSON Schema:
import { validate } from '@scalar/json-schema-validator'
const schema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
required: ['name'],
properties: { name: { type: 'string' } },
}
const result = validate({ name: 'Hello' }, schema)
console.log(result.valid)
if (!result.valid) {
console.log(result.errors)
}The dialect is picked automatically from the schema's $schema (JSON Schema draft-04, draft-07, and 2020-12 are supported).
Reuse a schema
When validating many documents against the same schema, compile it once:
import { createValidator } from '@scalar/json-schema-validator'
const validateUser = createValidator(schema)
validateUser({ name: 'Ada' })
validateUser({ name: 'Grace' })Extra formats
Register custom Ajv formats via formats:
validate(document, schema, {
formats: {
'media-range': true,
},
})Formats are applied when a schema is compiled, and validate caches the compiled
schema by identity. So formats only takes effect the first time it sees a given
schema object — later calls reuse the validator built from that first set. When
different documents need different formats for the same schema, build a validator
per format set with createValidator instead.
Throw on error
try {
validate(document, schema, { throwOnError: true })
} catch (error) {
// Handle the first validation error
}Community
We are API nerds. You too? Let's chat on Discord: https://discord.gg/scalar
License
The source code in this repository is licensed under MIT.
