Registry / testing / chai-json-schema

chai-json-schema

JSON →
library1.5.1jsnpmunverified

Chai plugin for validating values against JSON Schema v4. Current stable version 1.5.1, maintained as a legacy plugin using Tiny Validator tv4 for schema validation. Supports draft-04 only, with no support for later drafts. Key differentiator: provides BDD and TDD assertions directly in Chai tests. Release cadence is low; last release was 2019. Consider chai-json-schema-ajv for newer JSON Schema versions.

npm install chai-json-schema
INSTALL
IMPORT
SIG · CHAI-JSON-SCHEMA
C
chai-json-schema
testingjavascriptv1.5.1
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 (plugin)
const chai = require('chai'); chai.use(require('chai-json-schema'));
import chaiJsonSchema from 'chai-json-schema';
CommonJS require is standard. ESM import may not work due to CJS-only exports.
assert.jsonSchema
const assert = require('chai').assert; assert.jsonSchema(value, schema);
assert.jsonSchema(schema, value);
Order is value then schema; ensure correct argument order.
expect().to.be.jsonSchema
expect(value).to.be.jsonSchema(schema);
expect(schema).to.be.jsonSchema(value);
BDD style uses chainable getter; value is subject, schema is argument.

Demonstrates BDD and TDD assertions for JSON Schema validation using chai-json-schema.

const chai = require('chai'); chai.use(require('chai-json-schema')); const expect = chai.expect; const fruitSchema = { type: 'object', required: ['name', 'color'], properties: { name: { type: 'string' }, color: { type: 'string' } } }; expect({ name: 'apple', color: 'red' }).to.be.jsonSchema(fruitSchema); expect({ name: 42 }).to.not.be.jsonSchema(fruitSchema); // Using assert: const assert = chai.assert; assert.jsonSchema({ name: 'banana', color: 'yellow' }, fruitSchema);
Debug
Known issues
deprecatedUses tv4, which only supports JSON Schema draft-04. Not compatible with draft-06/07/2019-09/2020-12.
fix
Switch to chai-json-schema-ajv for later drafts.
affects: >=1.0.0
gotchaSchema $ref requires pre-adding schemas via chai.tv4.addSchema(). Inline $ref will fail if not added.
fix
Use chai.tv4.addSchema(uri, schema) before asserting.
affects: >=1.0.0
gotchatv4 is not actively maintained. Potential security vulnerabilities in the dependency chain.
fix
Monitor for CVEs; consider alternative libraries like ajv + chai-json-schema-ajv.
affects: >=1.0.0
gotchaAMD or browser usage requires manual loading of jsonpointer and tv4 shims.
fix
Ensure tv4 global is available and jsonpointer is required as 'jsonpointer'.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: chai.use is not a function
Using chai-json-schema without importing chai first or incorrect chai version.
fix
Install chai as dependency: npm install chai. Then require('chai') before use.
Error: Invalid schema: unknown schema reference: #/definitions/...
$ref points to a schema not added via tv4.addSchema().
fix
Use chai.tv4.addSchema('http://example.com/schema', schema) to pre-register referenced schemas.
AssertionError: expected { Object (name, color) } to be json schema
Schema definition error or value does not match schema.
fix
Validate schema itself with tv4.validate(schema, tv4.getSchema('http://json-schema.org/draft-04/schema#')) or use online validator.
Upgrade
Version history
1.5.1latest on npm
Audit
Dependencies
chairequiredPeer dependency required for plugin functionality
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
chai-json-schema — npm install chai-json-schema · libregistry