Registry / testing / chai-json-schema-ajv

chai-json-schema-ajv

JSON →
library5.2.4jsnpmunverified

Chai plugin for JSON Schema validation using Ajv. Current stable version is 5.2.4, released in mid-2020 with support for Ajv >=4 and JSON Schema drafts 4, 6, and 7. It provides fluent chai assertions (jsonSchema, validJsonSchema) for both data validation and schema validation. Unlike chai-json-schema (which uses tv4), this plugin uses Ajv for better performance and compliance. The library exposes a .create() method for custom Ajv options. Maintenance mode since v5, with no active development.

npm install chai-json-schema-ajv
INSTALL
IMPORT
SIG · CHAI-JSON-SCHEMA-A
C
chai-json-schema-ajv
testingjavascriptv5.2.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 chaiJsonSchema from 'chai-json-schema-ajv';
✗ const chaiJsonSchema = require('chai-json-schema-ajv');
CJS require works but for ESM projects use default import. Since v5, package has both CJS and ESM exports.
create
✓ import { create } from 'chai-json-schema-ajv';
✗ const { create } = require('chai-json-schema-ajv');
Named export for creating a chai plugin with custom Ajv options.
jsonSchema
✓ const chai = require('chai'); chai.use(require('chai-json-schema-ajv')); expect(data).to.be.jsonSchema(schema);
✗ import { jsonSchema } from 'chai-json-schema-ajv';
jsonSchema is a chai property, not a direct import. It is added to chai assertions after using the plugin.
validJsonSchema
✓ expect(schema).to.be.validJsonSchema;
✗ expect(schema).to.be.validJsonSchema(schema);
validJsonSchema is a property, not a method. It does not take arguments.

Demonstrates basic usage of jsonSchema and validJsonSchema chai assertions for data and schema validation.

const chai = require('chai'); const chaiJsonSchema = require('chai-json-schema-ajv'); chai.use(chaiJsonSchema); const expect = chai.expect; const data = { name: 'apple', color: ['red'], value: 10 }; const schema = { type: 'object', required: ['name', 'color', 'value'], properties: { name: { type: 'string', minLength: 3 }, color: { type: 'array', minItems: 1, items: { type: 'string' } }, value: { type: 'integer', minimum: 5 } } }; expect(data).to.be.jsonSchema(schema, 'data passes schema'); expect({}).to.not.be.jsonSchema(schema, 'empty fails'); // Validate schema itself expect(schema).to.be.validJsonSchema; expect({ type: 'invalid' }).to.not.be.validJsonSchema;
Debug
Known issues
breakingv5 changed error message format compared to v4; assertions may fail if tests rely on exact error text.
fix
Update test assertions to match new default error messages from Ajv.errorsText().
affects: >=5.0.0 <6.0.0
deprecatedAjv v8 is not supported; plugin only supports Ajv v4 to v7 (but documented as >=4). Use Ajv v7 for latest draft-07 support.
fix
Install Ajv v7: npm install ajv@7 --save-dev
affects: >=5.0.0
gotchaThe plugin does not automatically install Ajv; it must be added as a direct dependency. Missing Ajv leads to cryptic runtime errors.
fix
Ensure Ajv is installed: npm install ajv --save-dev
affects: >=1.0.0
gotchaUsing .create() without arguments will fail because it expects at least an empty object; otherwise Ajv constructor is not called correctly.
fix
Always provide an options object, even if empty: chai.use(require('chai-json-schema-ajv').create({}))
affects: >=1.0.0
gotchavalidJsonSchema is a property flag, not a chainable method. Attempting to call it as a function (e.g., .validJsonSchema(schema)) will fail silently or throw.
fix
Use it without parentheses: expect(schema).to.be.validJsonSchema;
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Ajv is not a constructor
Ajv version mismatch or missing install; default import may be needed for ESM.
fix
Install Ajv v7: npm install ajv@7. If using ESM, use import Ajv from 'ajv'.
AssertionError: expected [object Object] to be valid json schema
The schema itself is invalid (e.g., unknown type keyword or missing $schema).
fix
Validate the schema against JSON Schema meta-schema, or use .validJsonSchema assertion to check it first.
TypeError: chai.use(...) is not a function
Chai not installed or incorrectly imported; also could be due to chai-json-schema-ajv not being a proper plugin.
fix
Ensure chai is installed: npm install chai. Use chai.use(require('chai-json-schema-ajv')) exactly.
Error: Cannot find module 'ajv'
Ajv is not installed as a dependency.
fix
Install Ajv: npm install ajv --save-dev
Upgrade
Version history
5.2.4latest on npm
Audit
Dependencies
ajvoptionalpeer dependency required for JSON Schema validation; must be installed separately
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
chai-json-schema-ajv — npm install chai-json-schema-ajv · libregistry