chai-json-schema-ajv

Build Status Coverage Status

用於驗證 JSON Schema 的 Chai 外掛。

這是基於 ajv,一個 JSON Schema 驗證器。

版本 ajv 版本 JSON Schema 版本
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 與 v4 相同,但訊息格式不同
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

用法

驗證資料 (jsonSchema)

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')

驗證 schema (validJsonSchema)

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')

自訂選項

選項也會傳遞給 ajv

...
const options = { ... }
chai.use(
  require('chai-json-schema-ajv').create(options)
)
...

// Basically, it's same as `new Ajv(options)`

選項 verbose

預設錯誤訊息由 ajv.errorsText 解析。

...
chai.use(
  require('chai-json-schema-ajv')
)
...
expected data to match json-schema
data should have required property 'color'

如果您使用選項 {verbose: true},它將印出完整錯誤訊息。

...
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 } } ]

選項 ajv

如果您想重複使用 ajv 實例,您可以

const ajv = new Ajv
...
chai.use(
  require('chai-json-schema-ajv').create({
    ajv
  })
)
...

assert.ok(ajv === chai.ajv)

存取 ajv 實例

...
chai.use(
  require('chai-json-schema-ajv')
)
...

assert.ok(chai.ajv instanceof Ajv)

待辦事項

  • 支援瀏覽器端
  • 移至 es2017 async/await
  • 新增 linter
  • 將選項傳遞給 ajv (感謝 @dimac)

授權條款

MIT