
Product
Introducing Data Exports
Export Socket alert data to your own cloud storage in JSON, CSV, or Parquet, with flexible snapshot or incremental delivery.
chai-json-schema-ajv
Advanced tools
A chai plugin for validate json schema.
This is based on ajv, a JSON schema Validator.
| version | ajv version | json schema version |
|---|---|---|
| v1 | 4.11.8 | JSON Schema draft 4 |
| v2 | 5.5.2 | JSON Schema draft-06 |
| v3 | ^6.7.0 | JSON Schema draft-07 |
| v4 | >=4 <7 | |
| v5 | >=4 <7 | Same as v4, but different message format |
| v5.2 | >=4 |
npm i ajv --save-dev # Or any version you prefer `npm i ajv@4 --save-dev`
npm i chai-json-schema-ajv --save-dev
const chai = require('chai')
chai.use(require('chai-json-schema-ajv'))
const expect = chai.expect
const assert = chai.assert
const apple = {
name: 'foo',
color: ['red', 'green', 'yellow'],
value: 10
}
const car = {
name: 'bar',
speed: 1.1
}
const schema = {
title: 'fruit schema v0.1',
type: 'object',
required: ['name', 'color', 'value'],
properties: {
name: {
type: 'string',
minLength: 3
},
color: {
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
type: 'string'
}
},
value: {
type: 'integer',
minimum: 5
}
}
}
expect(apple).to.be.jsonSchema(schema, 'custom flag')
expect(car).to.not.be.jsonSchema(schema, 'custom flag')
assert.jsonSchema(apple, schema, 'custom flag')
assert.notJsonSchema(car, schema, 'custom flag')
const chai = require('chai')
chai.use(require('chai-json-schema-ajv'))
const expect = chai.expect
const assert = chai.assert
const schema = {
title: 'valid schema',
type: 'object',
required: ['name'],
properties: {
name: {
type: 'string',
minLength: 3
}
}
}
expect(schema, 'custom flag').to.be.validJsonSchema
expect({ type: '__invalid__' }, 'custom flag').to.not.be.validJsonSchema
assert.validJsonSchema(schema, 'custom flag')
assert.notValidJsonSchema({ type: '__invalid__' }, 'custom flag')
Options will also pass to ajv
...
const options = { ... }
chai.use(
require('chai-json-schema-ajv').create(options)
)
...
// Basically, it's same as `new Ajv(options)`
Default error message is parsed by ajv.errorsText.
...
chai.use(
require('chai-json-schema-ajv')
)
...
expected data to match json-schema
data should have required property 'color'
If you go with option {verbose: true}, it will print full errors.
...
chai.use(
require('chai-json-schema-ajv').create({
verbose: true
})
)
...
expected { name: 'bar', speed: 1.1 } to match json-schema
[ { keyword: 'required',
dataPath: '',
schemaPath: '#/required',
params: { missingProperty: 'color' },
message: 'should have required property \'color\'',
schema:
{ name: { type: 'string', minLength: 3 },
color:
{ type: 'array',
minItems: 1,
uniqueItems: true,
items: { type: 'string' } },
value: { type: 'integer', minimum: 5 } },
parentSchema:
{ title: 'fruit schema v0.1',
type: 'object',
required: [ 'name', 'color', 'value' ],
properties:
{ name: { type: 'string', minLength: 3 },
color:
{ type: 'array',
minItems: 1,
uniqueItems: true,
items: { type: 'string' } },
value: { type: 'integer', minimum: 5 } } },
data: { name: 'bar', speed: 1.1 } } ]
If you want to reuse ajv instance, you can
const ajv = new Ajv
...
chai.use(
require('chai-json-schema-ajv').create({
ajv
})
)
...
assert.ok(ajv === chai.ajv)
...
chai.use(
require('chai-json-schema-ajv')
)
...
assert.ok(chai.ajv instanceof Ajv)
MIT
chai-json-schema is another Chai plugin for JSON Schema validation. It is similar to chai-json-schema-ajv but uses a different underlying JSON Schema validator. While chai-json-schema-ajv uses AJV, chai-json-schema uses a simpler validator which might not support the latest JSON Schema standards as comprehensively as AJV.
AJV is a standalone JSON Schema validator that is highly performant and supports the latest JSON Schema standards. While chai-json-schema-ajv integrates AJV into Chai for testing purposes, AJV itself can be used independently for JSON Schema validation outside of a testing context.
FAQs
A chai plugin for validate json schema.
The npm package chai-json-schema-ajv receives a total of 27,278 weekly downloads. As such, chai-json-schema-ajv popularity was classified as popular.
We found that chai-json-schema-ajv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Export Socket alert data to your own cloud storage in JSON, CSV, or Parquet, with flexible snapshot or incremental delivery.

Research
/Security News
Bitwarden CLI 2026.4.0 was compromised in the Checkmarx supply chain campaign after attackers abused a GitHub Action in Bitwarden’s CI/CD pipeline.

Research
/Security News
Docker and Socket have uncovered malicious Checkmarx KICS images and suspicious code extension releases in a broader supply chain compromise.