Registry / testing / ajv-keywords

ajv-keywords

JSON →
library5.1.0jsnpmunverified

Custom JSON Schema keywords for the Ajv validator (v8). Current stable version 5.1.0, released regularly on npm. Provides additional validation keywords like typeof, instanceof, range, regexp, transform, uniqueItemProperties, allRequired, deepProperties, and select. Requires Ajv v8.8.2+ peer dependency. Ships TypeScript definitions. Commonly used to extend JSON Schema validation beyond the standard spec, especially for instance validation and dynamic defaults.

npm install ajv-keywords
INSTALL
IMPORT
SIG · AJV-KEYWORDS
A
ajv-keywords
testingjavascriptv5.1.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.

ajvKeywords
import ajvKeywords from 'ajv-keywords';
const ajvKeywords = require('ajv-keywords');
ESM-only since v4; CommonJS require not supported in default export
ajv (with keywords)
import Ajv from 'ajv'; import ajvKeywords from 'ajv-keywords'; const ajv = new Ajv(); ajvKeywords(ajv);
const Ajv = require('ajv'); const ajv = new Ajv(); require('ajv-keywords')(ajv);
For ESM projects, use import; require may work with older Node but is not recommended
definitions (definitions export)
import { definitions } from 'ajv-keywords';
const { definitions } = require('ajv-keywords');
definitions is a named export for use with Ajv's keywords option; ESM only
type keyword 'typeof'
import ajvKeywords from 'ajv-keywords'; ajvKeywords(ajv, 'typeof');
ajvKeywords(ajv, 'typeof', {});
Pass keyword name as string, not an options object. Options are per-keyword and passed differently.

Shows adding all keywords to Ajv, then using typeof and instanceof keywords in schemas.

import Ajv from 'ajv'; import ajvKeywords from 'ajv-keywords'; const ajv = new Ajv(); ajvKeywords(ajv); const schema = { type: 'object', required: ['name'], properties: { name: { type: 'string' }, age: { typeof: 'number' } } }; const data = { name: 'Alice', age: 30 }; const valid = ajv.validate(schema, data); console.log(valid); // true // With instanceof keyword ajvKeywords(ajv, 'instanceof'); const schema2 = { instanceof: 'RegExp' }; console.log(ajv.validate(schema2, /test/)); // true console.log(ajv.validate(schema2, 'test')); // false
Debug
Known issues
breakingajv-keywords v4+ requires Ajv v8. Using with Ajv v6 or v7 will throw errors.
fix
Upgrade Ajv to v8.8.2 or later, or use ajv-keywords v3 with Ajv v6.
affects: >=4.0.0
deprecatedThe select/selectCases/selectDefault keywords are deprecated and may be removed in future versions.
fix
Use JSON Schema if/then/else instead of select where possible.
affects: >=5.0.0
gotchaKeywords that modify data (transform, dynamicDefaults) are not supported in standalone validation code.
fix
Do not use these keywords when generating standalone validation code with Ajv's standalone module.
affects: >=4.0.0
gotchaThe instanceof keyword uses the global constructor to check instance, which may fail across realms (e.g., iframes, different V8 contexts).
fix
Avoid using instanceof for cross-realm validation; use typeof or custom keyword instead.
affects: >=1.0.0
breakingDefault export changed from function to object in v4. Use ajvKeywords(ajv) instead of AjvKeywords(ajv).
fix
Use import ajvKeywords from 'ajv-keywords' and call ajvKeywords(ajv).
affects: 4.0.0
Errors
Common errors & fixes
TypeError: ajvKeywords is not a function
Using default import as function but package not imported correctly in CJS context
fix
Use import ajvKeywords from 'ajv-keywords' (ESM) or const ajvKeywords = require('ajv-keywords').default (CJS with ESM interop)
Cannot find module 'ajv-keywords'
Package not installed or incorrect version for Ajv version
fix
npm install ajv-keywords@^5.0.0 for Ajv v8, or ajv-keywords@^3.5.2 for Ajv v6
ValidationError: data must match a schema (instanceof keyword)
Using instanceof keyword with a string literal instead of a constructor reference
fix
Ensure schema value is a string constructor name (e.g., 'RegExp', 'Array'), not an object or literal
Upgrade
Version history
5.1.0latest on npm
Audit
Dependencies
ajvrequiredpeer dependency required for all functionality
Agent activity
75 hits · last 30 days
node
70
OpenAI (training)
1
Resources
ajv-keywords — npm install ajv-keywords · libregistry