Registry / devops / ajv-draft-04

ajv-draft-04

JSON →
library1.0.0jsnpmunverified

Extends Ajv (v8.5+) to support JSON Schema draft-04 validation. Version 1.0.0 is the first stable release. Maintained by the Ajv team, it is the recommended way to validate draft-04 schemas with Ajv v8. It seamlessly integrates with Ajv's API and options, enabling strict mode, custom keywords, and formats. Unlike the older ajv v6, which natively supported draft-04, this package allows using the latest Ajv with legacy schemas. Ships TypeScript definitions.

npm install ajv-draft-04
INSTALL
IMPORT
SIG · AJV-DRAFT-04
A
ajv-draft-04
devopsjavascriptv1.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.

Ajv
import Ajv from 'ajv-draft-04'
import { Ajv } from 'ajv-draft-04'
The default export is the Ajv class, not a named export.
Ajv
const Ajv = require('ajv-draft-04')
const { Ajv } = require('ajv-draft-04')
CommonJS: the whole module is the constructor; named destructuring gives undefined.
Ajv (with formats)
import Ajv from 'ajv-draft-04' import addFormats from 'ajv-formats' addFormats(ajvInstance)
import Ajv from 'ajv-draft-04' import AjvFormats from 'ajv-draft-04/formats'
Formats are not bundled; you must add them separately using the 'ajv-formats' package.

Shows how to create an Ajv instance for draft-04, add format support, compile a schema, and validate data.

import Ajv from 'ajv-draft-04' import addFormats from 'ajv-formats' const ajv = new Ajv() addFormats(ajv) const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'integer', minimum: 0 } }, required: ['name'] } const validate = ajv.compile(schema) const valid = validate({ name: 'Alice', age: 30 }) console.log(valid) // true console.log(validate.errors) // null
Debug
Known issues
breakingPackage is ESM-only; CommonJS require may not work in some environments.
fix
Use dynamic import() or set type: 'module' in package.json if using Node <12.22.
affects: >=1.0.0
gotchaEnum values are not coerced: validate({ type: 'string', enum: ['a', 'b'] }, 'A') fails even with useDefaults or coerceTypes?
fix
Ensure enum values exactly match the data; draft-04 does not coerce.
affects: *
deprecatedThe 'id' keyword is used instead of '$id' in draft-04; using '$id' may cause undefined behavior.
fix
Use 'id' as the schema identifier; '$id' is for draft-06+.
affects: *
gotchaThe 'exclusiveMinimum' and 'exclusiveMaximum' are boolean (not numeric) in draft-04.
fix
Use exclusiveMinimum: true combined with minimum: 5, not exclusiveMinimum: 5.
affects: *
Errors
Common errors & fixes
TypeError: Ajv is not a constructor
Using named import instead of default import.
fix
Use 'import Ajv from "ajv-draft-04"' (default import).
SyntaxError: Cannot use import statement outside a module
Using ESM import in a CommonJS environment without type: 'module'.
fix
Add '"type": "module"' to package.json or use require('ajv-draft-04').
Error: unknown keyword "$id"
Using '$id' keyword which is not supported in draft-04.
fix
Replace '$id' with 'id' in your schema.
ValidationError: data must NOT have additional properties
Using additionalProperties: false with no properties defined.
fix
Remove additionalProperties or explicitly define properties.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
ajvrequiredpeer dependency: requires ajv ^8.5.0 as the core validation engine
Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources
ajv-draft-04 — npm install ajv-draft-04 · libregistry