# better-ajv-errors

> JSON Schema validation for Human

Latest version **2.0.3** (published 2025-12-31) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

**Score 65/100 (B)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2025-12-31 |
| First published | 2017-11-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.20.6 |
| Dependencies | 5 |
| Unpacked size | 103.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 245 |
| Author | Rifat Nabi |
| Maintainers | 1999, rifat, supertong |
| Keywords | json-schema, ajv, ajv-errors |

## Links

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

## Dependencies (5)

- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [leven](https://npm.io/package/leven.md) ^3.1.0 < 4
- [jsonpointer](https://npm.io/package/jsonpointer.md) ^5.0.1
- [@babel/code-frame](https://npm.io/package/@babel/code-frame.md) ^7.27.1
- [@humanwhocodes/momoa](https://npm.io/package/@humanwhocodes/momoa.md) ^2.0.4

## 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

- 2.0.3 (latest) — 2025-12-31
- 1.0.0-next.0 (next) — 2021-11-08
- 2.0.2 — 2025-05-31
- 2.0.1 — 2025-05-30
- 2.0.0 — 2025-05-29
- 1.2.0 — 2022-04-12
- 1.1.2 — 2021-11-26
- 1.1.1 — 2021-11-19
- 1.1.0 — 2021-11-13
- 1.0.0 — 2021-11-10
- 0.8.2 — 2021-11-07
- 0.8.1 — 2021-11-07
- 0.8.0 — 2021-11-03
- 0.7.0 — 2021-01-06
- 0.6.7 — 2019-09-21
- … 22 more at https://npm.io/package/better-ajv-errors/versions

## README

<h1 align="center">
  <img width="570" src="media/logo.png" alt="better-ajv-errors">
  <br>
</h1>

> JSON Schema validation for Human 👨‍🎤

Main goal of this library is to provide relevant error messages like the following:

<p align="center">
  <img src="media/screenshot.svg">
</p>

## Installation

```bash
$ npm i better-ajv-errors
$ # Or
$ yarn add 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 'better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('better-ajv-errors').default;
// Or
// const { default: betterAjvErrors } = require('better-ajv-errors');

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

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

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

if (!valid) {
  const output = betterAjvErrors(schema, data, validate.errors);
  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`

#### data

Type: `Object`

The JSON payload you validate against using `ajv`

#### errors

Type: `Array`

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

#### options

Type: `Object`

##### format

Type: `string`  
Default: `cli`  
Values: `cli` `js`

Use default `cli` output format if you want to **print** beautiful validation errors like following:

<img width="620" src="media/screenshot.svg">

Or, use `js` if you are planning to use this with some API. Your output will look like following:

```javascript
[
  {
    start: { line: 6, column: 15, offset: 70 },
    end: { line: 6, column: 26, offset: 81 },
    error:
      '/content/0/type should be equal to one of the allowed values: panel, paragraph, ...',
    suggestion: 'Did you mean paragraph?',
  },
];
```

##### indent

Type: `number` `null`  
Default: `null`

If you have an unindented JSON payload and you want the error output indented.

This option have no effect when using the `json` option.

##### json

Type: `string` `null`  
Default: `null`

Raw JSON payload used when formatting codeframe.
Gives accurate line and column listings.

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