Registry / testing / djv
library2.1.4jsnpmunverified

Dynamic JSON-Schema validator supporting draft-04 and draft-06. Version 2.1.4 is current stable; v2.0.0 made draft-06 the default. It provides lightweight, fast validation with an environment-based API, custom formats, error handlers, and schema import/export. Differentiates from ajv by being smaller and simpler, but less feature-rich and slower.

npm install djv
INSTALL
IMPORT
SIG · DJV
D
djv
testingjavascriptv2.1.4
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.

default
import djv from 'djv'
const djv = require('djv')
ESM default import since v2. For CommonJS use require('djv').default if using the built bundle.
new djv()
const env = new djv({ version: 'draft-06' })
const env = djv({ version: 'draft-06' })
djv is a constructor; must be called with new.
env.addSchema
env.addSchema('mySchema', schemaObject)
env.addSchema(schemaObject)
addSchema requires a name as first argument.

Creates a djv environment, adds a schema, and validates objects against it.

import djv from 'djv'; const env = new djv({ version: 'draft-06' }); const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number', minimum: 0 } }, required: ['name'] }; env.addSchema('person', schema); const validPerson = { name: 'Alice', age: 30 }; const invalidPerson = { age: -5 }; console.log(env.validate('person', validPerson)); // undefined console.log(env.validate('person', invalidPerson)); // error string
Debug
Known issues
breakingDefault schema version changed from draft-04 to draft-06 in v2.0.0
fix
Explicitly set version: new djv({ version: 'draft-04' }) or use @korzio/djv-draft-04 package.
affects: >=2.0.0
deprecatedrequire('djv') returns a function, not a constructor, in CJS environments before v2
fix
Use const djv = require('djv').default or upgrade to v2.
affects: <2.0.0
gotchavalidate() returns undefined for valid, but an error string for invalid (not boolean)
fix
Check if return value is undefined: if (env.validate(name, obj) === undefined) { /* valid */ }
affects: *
gotchaaddFormat does not handle all JSON Schema format keywords; only custom formats added via addFormat are recognized
fix
Use addFormat for custom formats, and be aware that standard formats like 'email' are not validated by default.
affects: *
gotchaSchema paths passed to validate() can use '#' to reference sub-schemas, but the base schema must be added first
fix
Always use addSchema() before validate() with a path like 'schemaName#/definitions/sub'.
affects: *
Errors
Common errors & fixes
djv is not a constructor
Using require('djv') as a constructor without default import in CJS, or using ESM import incorrectly.
fix
Use import djv from 'djv' or const djv = require('djv').default.
undefined is not a function
Calling djv() without new.
fix
Use const env = new djv();
Cannot read property 'addSchema' of undefined
env is undefined because djv was not instantiated correctly.
fix
Ensure you call new djv() and store result in a variable.
Upgrade
Version history
2.1.4latest on npm
Audit
Dependencies
@korzio/djv-draft-04optionalOptional dependency to support JSON-Schema draft-04 environment
Agent activity
9 hits · last 30 days
node
8
Resources
packagedjv
djv — npm install djv · libregistry