Registry / devops / jsonpolice

jsonpolice

JSON →
library13.0.0jsnpmunverified

A JSON Schema validator library supporting Draft 7, 2019-09, and 2020-12 with automatic version detection, default values, property filtering, and external reference resolution. Version 13.0.0 requires Node.js >= 18.17.0 and is pure ESM. Key differentiators include context-aware read/write support, minimal dependencies (jsonref and lodash), full TypeScript definitions, and extensibility via custom validators. Released under MIT license by Vivocha.

npm install jsonpolice
INSTALL
IMPORT
SIG · JSONPOLICE
J
jsonpolice
devopsjavascriptv13.0.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.

create
import { create } from 'jsonpolice'
const create = require('jsonpolice')
Package is pure ESM; CJS require not supported.
Schema
import { Schema } from 'jsonpolice'
Class for custom schema extensions.
ValidationError
import { ValidationError } from 'jsonpolice'
Error class for validation failures.

Shows basic schema validation with required fields and error handling, then demonstrates default value assignment.

import { create } from 'jsonpolice'; const schema = await create({ type: 'object', properties: { name: { type: 'string', minLength: 1 }, email: { type: 'string', format: 'email' }, age: { type: 'integer', minimum: 0, maximum: 120 } }, required: ['name', 'email'] }); const validData = { name: 'John Doe', email: 'john@example.com', age: 30 }; try { const result = await schema.validate(validData); console.log('Valid:', result); } catch (error) { console.error('Validation failed:', error.message); } // With defaults const schema2 = await create({ type: 'object', properties: { name: { type: 'string' }, role: { type: 'string', default: 'user' }, active: { type: 'boolean', default: true }, credits: { type: 'number', default: 100 } } }); const data = { name: 'Alice' }; const result2 = await schema2.validate(data, { setDefault: true }); console.log(result2); // Output: { name: 'Alice', role: 'user', active: true, credits: 100 }
Debug
Known issues
breakingPackage version 13.0.0 dropped support for Node.js < 18.17.0.
fix
Upgrade Node.js to >= 18.17.0.
affects: >=13.0.0
breakingPackage is pure ESM since version 13.0.0; CommonJS require() will throw an error.
fix
Use import syntax or dynamic import(). For CommonJS projects, use async import().
affects: >=13.0.0
gotchaThe validate() method is async and returns a Promise. Forgetting to await leads to unresolved promise.
fix
Always await schema.validate(data) or use .then().
affects: >=0.0.0
gotchaThe create() function returns a Promise. Schema is not usable until resolved.
fix
Always await create(schema) or use .then().
affects: >=0.0.0
deprecatedDraft 7 support may be deprecated in future versions; prefer explicit version via $schema or options.version.
fix
Set $schema in schema or pass options.version to create().
affects: >=12.0.0
Errors
Common errors & fixes
TypeError: create is not a function
Using CJS require with ESM-only package.
fix
Use import { create } from 'jsonpolice' or dynamic import('jsonpolice').
ValidationError: data.name is required
Missing required field 'name' in the data object.
fix
Add the missing required property to the data.
Error [ERR_REQUIRE_ESM]: require() of ES Module
Attempting to load the package with require().
fix
Switch to ESM (add "type": "module" in package.json) or use dynamic import.
Upgrade
Version history
13.0.0latest on npm
Audit
Dependencies
jsonrefrequiredExternal reference resolution for $ref
lodashrequiredUtility functions for deep merging and object operations
Agent activity
4 hits · last 30 days
node
4
Resources
jsonpolice — npm install jsonpolice · libregistry