Registry / validation / json-schema-library

json-schema-library

JSON →
library11.5.0jsnpmunverified

json-schema-library is a customizable, hackable, and highly compliant JSON Schema validator and utility library for JavaScript/TypeScript, fully supporting JSON Schema drafts 04, 06, 07, 2019-09, and 2020-12. Version 11.5.0 is current (released 2024), with frequent updates. Unlike faster or more minimal validators, it prioritizes extensibility: you can customize drafts, add keyword extensions, and traverse schemas via SchemaNode. It's the most compliant validator per Bowtie reports, runs in Node and browser, and ships TypeScript types. Designed for developers building custom tools around JSON Schema, not just validation.

npm install json-schema-library
INSTALL
IMPORT
SIG · JSON-SCHEMA-LIBRAR
J
json-schema-library
validationjavascriptv11.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

compileSchema
import { compileSchema } from 'json-schema-library'
const { compileSchema } = require('json-schema-library'); compileSchema(schema);
ESM-only since v11. For TypeScript, use import syntax. CommonJS require may fail in some bundlers or Node versions.
SchemaNode
import { SchemaNode } from 'json-schema-library'
TypeScript type used for the result of compileSchema. Not available as a runtime value.
draft04
import { draft04 } from 'json-schema-library'
import { Draft04 } from 'json-schema-library'
Draft imports are lowercase, camelCase. Use draftXX to access draft-specific validation functions.
default
import jsonSchemaLibrary from 'json-schema-library'
import * as jsonSchemaLibrary from 'json-schema-library'
Default export is the library namespace; prefer named imports for tree-shaking.

Compiles a JSON Schema into a SchemaNode, validates data, generates default data, and navigates subschemas via JSON Pointer.

import { compileSchema } from 'json-schema-library'; const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number', minimum: 0 } }, required: ['name'] }; const compiled = compileSchema(schema); // Validate data const data = { name: 'Alice', age: 30 }; const result = compiled.validate(data); console.log('Valid:', result.valid); // true // Generate default data const defaultData = compiled.getData(); console.log(defaultData); // {} // Access a node by JSON Pointer const nameNode = compiled.getNode('/properties/name'); console.log(nameNode.schema); // { type: 'string' }
Debug
Known issues
breakingVersion 11 dropped CommonJS support. require('json-schema-library') may fail in older Node or non-ESM environments.
fix
Use ES modules: static import or dynamic import(). For legacy CommonJS, stick with v10.x.
affects: >=11.0.0
deprecatedThe `validate` method on SchemaNode now returns an object with `valid`, `errors`, `annotations`. The old method returning boolean is removed.
fix
Update code to destructure result: const { valid, errors } = schemaNode.validate(data);
affects: >=8.0.0
gotchacompileSchema expects a JSON Schema object, not a JSON string or file path. Attempting to pass a string will throw a confusing error.
fix
Parse JSON strings with JSON.parse() first, or import JSON files using import assertions (build tools) or fs.readFileSync with JSON.parse.
affects: *
gotchaWhen using draft-04, the `$schema` keyword in the schema object may be ignored if not set correctly; the library auto-detects based on draft. But if you explicitly set draft via options, it overrides $schema.
fix
Explicitly pass draft option: compileSchema(schema, { draft: 'draft-04' }) to force a specific draft.
affects: *
deprecatedgetNode() with a single string argument is deprecated. Use the overload with options object for better type safety.
fix
Change getNode('#/foo') to getNode('#/foo', { data: {} }) to avoid deprecated behavior.
affects: >=10.0.0
Errors
Common errors & fixes
TypeError: schemaNode.validate is not a function
You may have imported a version that doesn't export SchemaNode correctly, or you're using v11 and trying to call validate on a raw schema object.
fix
Ensure you call compileSchema(schema) first and use the returned SchemaNode. Check import: import { compileSchema } from 'json-schema-library'.
Cannot find module 'json-schema-library' or its corresponding type declarations
Missing @types/json-schema-library (unnecessary since v10 ships types) or incorrect module resolution in tsconfig.
fix
Update tsconfig.json: set 'moduleResolution' to 'node' or 'node16', and 'esModuleInterop' to true. Also ensure json-schema-library is installed.
Uncaught Error: Invalid schema: property 'properties' must be an object
The schema passed to compileSchema is invalid JSON Schema structure (likely malformed).
fix
Validate your schema first with a JSON Schema validator or check that properties is an object (not array or string).
ReferenceError: self is not defined
The library is trying to access the global object in a Node environment where `self` doesn't exist. This is a known bug in older versions (pre-v10).
fix
Upgrade to v10+ or use a polyfill for self: globalThis.self = globalThis;
Upgrade
Version history
11.5.0latest on npm
Audit
Dependencies
lodashrequiredDeep clone, merge, and utility functions used internally
uuidoptionalUnique identifiers for internal schema node tracking
Agent activity
27 hits · last 30 days
node
22
Amazon
1
Resources