Git Product home page Git Product logo

chai-json-schema-ajv's Introduction

chai-json-schema-ajv

Build Status Coverage Status

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.1.1 JSON Schema draft-07

Usage

npm install chai-json-schema-ajv

Basic (jsonSchema)

const chai = require('chai')
chai.use(require('chai-json-schema-ajv'))
const expect = chai.expect

let apple = {
  name: 'foo',
  color: ['red', 'green', 'yellow'],
  value: 10
}
let 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)

Basic (validJsonSchema)

const chai = require('chai')
chai.use(require('chai-json-schema-ajv'))
const expect = chai.expect

let schema = {
  title: 'valid schema',
  type: 'object',
  required: ['name'],
  properties: {
    name: {
      type: 'string',
      minLength: 3
    }
  }
}

expect(schema).to.be.validJsonSchema

With custom ajv options

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

...
  • options will be send to ajv

Verbose

Default error message is parsed by ajv.errorsText.

...
chai.use(require('chai-json-schema-ajv')
...
expected value not match the json-schema
data.value should be integer

It will print full errors with the option {verbose: true}

...
chai.use(require('chai-json-schema-ajv').withOptions({ verbose: true }))
...
expected value not match the json-schema
[
 {
   "keyword": "type",
   "dataPath": ".value",
   "schemaPath": "#/properties/value/type",
   "params": {
     "type": "integer"
   },
   "message": "should be integer",
   "schema": "integer",
   "parentSchema": {
     "type": "integer"
   },
   "data": 1.1
 }
]

TODO

  • support browser side
  • move to es2017 async/await
  • add lint
  • send option to ajv (thanks @dimac)

License

MIT

chai-json-schema-ajv's People

Contributors

curtishumphrey avatar dasithkuruppu avatar dimac avatar up9cloud avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.