JSON Schema
Validate JSON against a JSON Schema 2020-12 document — the dialect OpenAPI 3.1 adopted and the most widely implemented schema language. This is the standards-track counterpart to the fluent b.safeSchema builder (in-process, ergonomic) and the portable b.jtd (small, codegen-friendly): reach for b.jsonSchema when the schema is an existing JSON Schema document — an API contract, a config schema, an OpenAPI component.
compile(schema, opts) returns a reusable validator; validate(schema, instance, opts) compiles and runs in one call, returning { valid, errors } where each error names the failing instance location, the schema keyword, and a message. The full 2020-12 vocabulary is supported — every applicator (allOf / anyOf / oneOf / not / if-then-else, properties / patternProperties / additionalProperties / prefixItems / items / contains), the annotation-aware unevaluatedProperties / unevaluatedItems, every assertion keyword, and reference resolution ($ref / $anchor / $dynamicRef / $dynamicAnchor / $defs / $id base URIs). format is an annotation by default (opt in to assertion with assertFormat: true). External references resolve through an operator-supplied schema map (opts.schemas) — never a network fetch.
Two advanced behaviors are opt-in rather than built in: validating a schema document against the dialect metaschema works only if you supply that metaschema via opts.schemas (it is not bundled), and $vocabulary-based keyword selection is not honored — every standard keyword always asserts.
b.jsonSchema.compile(schema, opts?) #
{
schemas: object, // map of external $id/URI → schema, for $ref
assertFormat: boolean, // default: false (format is an annotation)
maxErrors: number, // default: 100 — stop collecting past this
}
Compile a JSON Schema 2020-12 document into a reusable validator. The returned object has validate(instance) → { valid, errors } and isValid(instance) → boolean. Compiling once and validating many instances avoids re-indexing the schema's references on every call.
var v = b.jsonSchema.compile({ type: "object",
properties: { n: { type: "integer" } }, required: ["n"] });
v.validate({ n: 1 }).valid; // → true
b.jsonSchema.validate(schema, instance, opts?) #
{
schemas: object, // map of external $id/URI → schema, for $ref
assertFormat: boolean, // default: false (format is an annotation)
maxErrors: number, // default: 100 — stop collecting past this
}
Compile schema and validate instance in one call, returning { valid, errors }. Each error is { instancePath, keyword, schemaPath, message }. For repeated validation against the same schema, use compile instead.
b.jsonSchema.validate({ type: "string", minLength: 2 }, "hi").valid;
// → true
b.jsonSchema.isValid(schema, instance, opts?) #
{
schemas: object, // map of external $id/URI → schema, for $ref
assertFormat: boolean, // default: false (format is an annotation)
maxErrors: number, // default: 100 — stop collecting past this
}
Boolean convenience form of validate.
b.jsonSchema.isValid({ type: "integer" }, 3); // → true
Last updated 2026-08-08T16:39:49.652Z by seeder.