# @stoplight/better-ajv-errors

> JSON Schema validation for Human

Latest version **1.0.3** (published 2022-08-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @stoplight/better-ajv-errors
pnpm add @stoplight/better-ajv-errors
yarn add @stoplight/better-ajv-errors
bun add @stoplight/better-ajv-errors
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-08-22 |
| First published | 2020-06-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^12.20 \|\| >= 14.13 |
| Dependencies | 2 |
| Unpacked size | 31.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Rifat Nabi |
| Maintainers | stoplight-devops |
| Keywords | json-schema, ajv, ajv-errors |

## Links

- npm: https://www.npmjs.com/package/@stoplight/better-ajv-errors
- Repository: https://github.com/stoplightio/better-ajv-errors
- Homepage: https://github.com/stoplightio/better-ajv-errors#readme
- Issues: https://github.com/stoplightio/better-ajv-errors/issues
- npm.io page: https://npm.io/package/@stoplight/better-ajv-errors

## Dependencies (2)

- [leven](https://npm.io/package/leven.md) ^3.1.0
- [jsonpointer](https://npm.io/package/jsonpointer.md) ^5.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-08-22
- 1.0.2 — 2022-06-06
- 1.0.1 — 2021-11-12
- 1.0.0 — 2021-11-12
- 0.3.0 — 2021-11-12
- 0.2.0 — 2021-07-06
- 0.1.1 — 2021-05-10
- 0.1.0 — 2021-04-26
- 0.0.4 — 2021-01-05
- 0.0.3 — 2020-10-05
- 0.0.2 — 2020-10-05
- 0.0.1 — 2020-10-05
- 0.0.0 — 2020-06-18

## README

> JSON Schema validation for Human 👨‍🎤

Main goal of this library is to provide relevant AJV error messages. It's a fork
of great [better-ajv-errors](https://github.com/atlassian/better-ajv-errors) by
Atlassian, with focus on being leaner.

## Installation

```bash
$ yarn add @stoplight/better-ajv-errors
```

or

```bash
$ npm i @stoplight/better-ajv-errors
```

Also make sure that you installed [ajv](https://www.npmjs.com/package/ajv)
package to validate data against JSON schemas.

## Usage

First, you need to validate your payload with `ajv`. If it's invalid then you
can pass `validate.errors` object into `better-ajv-errors`.

```js
import Ajv from 'ajv';
import betterAjvErrors from '@stoplight/better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('better-ajv-errors');

// You need to pass `jsonPointers: true`
const ajv = new Ajv({ jsonPointers: true });

// Load schema and data
const schema = ...;
const data = ...;

const validate = ajv.compile(schema);
const valid = validate(data);

if (!valid) {
  const output = betterAjvErrors(schema, validate.errors, {
    propertyPath: [],
    targetValue: data,
  });
  console.log(output);
}
```

## API

### betterAjvErrors(schema, data, errors, [options])

Returns formatted validation error to **print** in `console`. See
[`options.format`](#format) for further details.

#### schema

Type: `Object`

The JSON Schema you used for validation with `ajv`.

#### errors

Type: `Array`

Array of
[ajv validation errors](https://github.com/epoberezkin/ajv#validation-errors)

#### options

Type: `Object`

##### propertyPath

Type: `Array`

Property path of a validated object that is a part of a bigger document. Might
be empty if the validated object equals the whole document.

##### targetValue

Type: `Object`

The JSON payload you validate against using `ajv`.

---
_Source: https://npm.io/package/@stoplight/better-ajv-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
