Gathering detailed insights and metrics for chai-json-schema-ajv
Gathering detailed insights and metrics for chai-json-schema-ajv
Gathering detailed insights and metrics for chai-json-schema-ajv
Gathering detailed insights and metrics for chai-json-schema-ajv
npm install chai-json-schema-ajv
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
8 Stars
78 Commits
5 Forks
2 Watching
14 Branches
3 Contributors
Updated on 04 Nov 2021
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.4%
31,363
Compared to previous day
Last week
10.5%
165,344
Compared to previous week
Last month
31.7%
672,313
Compared to previous month
Last year
136.9%
4,419,667
Compared to previous year
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 |
1npm i ajv --save-dev # Or any version you prefer `npm i ajv@4 --save-dev` 2npm i chai-json-schema-ajv --save-dev
1const chai = require('chai') 2chai.use(require('chai-json-schema-ajv')) 3const expect = chai.expect 4const assert = chai.assert 5 6const apple = { 7 name: 'foo', 8 color: ['red', 'green', 'yellow'], 9 value: 10 10} 11const car = { 12 name: 'bar', 13 speed: 1.1 14} 15const schema = { 16 title: 'fruit schema v0.1', 17 type: 'object', 18 required: ['name', 'color', 'value'], 19 properties: { 20 name: { 21 type: 'string', 22 minLength: 3 23 }, 24 color: { 25 type: 'array', 26 minItems: 1, 27 uniqueItems: true, 28 items: { 29 type: 'string' 30 } 31 }, 32 value: { 33 type: 'integer', 34 minimum: 5 35 } 36 } 37} 38 39expect(apple).to.be.jsonSchema(schema, 'custom flag') 40expect(car).to.not.be.jsonSchema(schema, 'custom flag') 41 42assert.jsonSchema(apple, schema, 'custom flag') 43assert.notJsonSchema(car, schema, 'custom flag')
1const chai = require('chai') 2chai.use(require('chai-json-schema-ajv')) 3const expect = chai.expect 4const assert = chai.assert 5 6const schema = { 7 title: 'valid schema', 8 type: 'object', 9 required: ['name'], 10 properties: { 11 name: { 12 type: 'string', 13 minLength: 3 14 } 15 } 16} 17 18expect(schema, 'custom flag').to.be.validJsonSchema 19expect({ type: '__invalid__' }, 'custom flag').to.not.be.validJsonSchema 20 21assert.validJsonSchema(schema, 'custom flag') 22assert.notValidJsonSchema({ type: '__invalid__' }, 'custom flag')
Options will also pass to ajv
1... 2const options = { ... } 3chai.use( 4 require('chai-json-schema-ajv').create(options) 5) 6... 7 8// Basically, it's same as `new Ajv(options)`
Default error message is parsed by ajv.errorsText
.
1... 2chai.use( 3 require('chai-json-schema-ajv') 4) 5...
1expected data to match json-schema 2data should have required property 'color'
If you go with option {verbose: true}
, it will print full errors.
1... 2chai.use( 3 require('chai-json-schema-ajv').create({ 4 verbose: true 5 }) 6) 7...
1expected { name: 'bar', speed: 1.1 } to match json-schema 2[ { keyword: 'required', 3 dataPath: '', 4 schemaPath: '#/required', 5 params: { missingProperty: 'color' }, 6 message: 'should have required property \'color\'', 7 schema: 8 { name: { type: 'string', minLength: 3 }, 9 color: 10 { type: 'array', 11 minItems: 1, 12 uniqueItems: true, 13 items: { type: 'string' } }, 14 value: { type: 'integer', minimum: 5 } }, 15 parentSchema: 16 { title: 'fruit schema v0.1', 17 type: 'object', 18 required: [ 'name', 'color', 'value' ], 19 properties: 20 { name: { type: 'string', minLength: 3 }, 21 color: 22 { type: 'array', 23 minItems: 1, 24 uniqueItems: true, 25 items: { type: 'string' } }, 26 value: { type: 'integer', minimum: 5 } } }, 27 data: { name: 'bar', speed: 1.1 } } ]
If you want to reuse ajv instance, you can
1const ajv = new Ajv 2... 3chai.use( 4 require('chai-json-schema-ajv').create({ 5 ajv 6 }) 7) 8... 9 10assert.ok(ajv === chai.ajv)
1... 2chai.use( 3 require('chai-json-schema-ajv') 4) 5... 6 7assert.ok(chai.ajv instanceof Ajv)
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/8 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
13 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More