npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-json-schema-ajv

v5.2.4

Published

A chai plugin for validate json schema.

Downloads

614,659

Readme

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.7.0|JSON Schema draft-07| |v4|>=4 <7|| |v5|>=4 <7|Same as v4, but different message format| |v5.2|>=4||

Installation

npm i ajv --save-dev # Or any version you prefer `npm i ajv@4 --save-dev`
npm i chai-json-schema-ajv --save-dev

Usage

Validate data (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')

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

Custom options

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

Option verbose

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

Option ajv

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)

Access ajv instance

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

assert.ok(chai.ajv instanceof Ajv)

TODO

  • Support browser side
  • Move to es2017 async/await
  • ~~Add linter~~
  • ~~Send option to ajv~~ (thanks @dimac)

License

MIT