# chai-json-schema-ajv

> A chai plugin for validate json schema.

Latest version **5.2.4** (published 2021-04-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install chai-json-schema-ajv
pnpm add chai-json-schema-ajv
yarn add chai-json-schema-ajv
bun add chai-json-schema-ajv
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.2.4 |
| Published | 2021-04-19 |
| First published | 2017-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 0 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | up9cloud |
| Maintainers | up9cloud |
| Keywords | chai, chai-plugin, json-schema, assert, test, schema, ajv |

## Links

- npm: https://www.npmjs.com/package/chai-json-schema-ajv
- Repository: https://github.com/up9cloud/chai-json-schema-ajv
- Homepage: https://github.com/up9cloud/chai-json-schema-ajv#readme
- Issues: https://github.com/up9cloud/chai-json-schema-ajv/issues
- npm.io page: https://npm.io/package/chai-json-schema-ajv

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 5.2.4 (latest) — 2021-04-19
- 5.2.3 — 2020-12-25
- 5.2.2 — 2020-12-25
- 5.2.0 — 2020-12-25
- 5.1.0 — 2020-07-20
- 5.0.0 — 2019-06-17
- 4.0.1 — 2019-06-17
- 4.0.0 — 2019-01-22
- 3.0.1 — 2019-01-22
- 3.0.0 — 2018-02-09
- 2.0.1 — 2018-02-09
- 2.0.0 — 2017-12-11
- 1.0.0 — 2017-10-08
- 0.2.4 — 2017-05-21
- 0.2.3 — 2017-05-21
- … 4 more at https://npm.io/package/chai-json-schema-ajv/versions

## README

# chai-json-schema-ajv

[![Build Status](https://travis-ci.org/up9cloud/chai-json-schema-ajv.svg?branch=master)](https://travis-ci.org/up9cloud/chai-json-schema-ajv)
[![Coverage Status](https://coveralls.io/repos/github/up9cloud/chai-json-schema-ajv/badge.svg?branch=master)](https://coveralls.io/github/up9cloud/chai-json-schema-ajv?branch=master)

A chai plugin for validate json schema.

This is based on [ajv](https://github.com/epoberezkin/ajv), a JSON schema Validator.

|version|ajv version|json schema version|
|---|---|---|
|[v1](https://github.com/up9cloud/chai-json-schema-ajv/tree/v1)|4.11.8|[JSON Schema draft 4](http://json-schema.org/)|
|[v2](https://github.com/up9cloud/chai-json-schema-ajv/tree/v2)|5.5.2|[JSON Schema draft-06](https://trac.tools.ietf.org/html/draft-wright-json-schema-validation-01)|
|[v3](https://github.com/up9cloud/chai-json-schema-ajv/tree/v3)|^6.7.0|[JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html)|
|v4|>=4 <7||
|v5|>=4 <7|Same as v4, but different message format|
|v5.2|>=4||

## Installation

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

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

```js
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](https://github.com/epoberezkin/ajv#options)

```js
...
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`.

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

```console
expected data to match json-schema
data should have required property 'color'
```

If you go with option `{verbose: true}`, it will print full errors.

```js
...
chai.use(
  require('chai-json-schema-ajv').create({
    verbose: true
  })
)
...
```

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

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

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

### Access ajv instance

```js
...
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

---
_Source: https://npm.io/package/chai-json-schema-ajv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
