# @readme/better-ajv-errors

> JSON Schema validation for Human

Latest version **2.4.0** (published 2025-10-24) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.4.0 |
| Published | 2025-10-24 |
| First published | 2021-10-11 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 6 |
| Unpacked size | 91.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Rifat Nabi |
| Maintainers | gkoberger, domharrington, mjcuva, kanadgupta, jonursenbach, rafegoldberg, dannobytes, gratcliff, llimllib, darrenyong, azinder1, kellyjosephprice |
| Keywords | json-schema, ajv, ajv-errors |

## Links

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

## Dependencies (6)

- [leven](https://npm.io/package/leven.md) ^3.1.0
- [picocolors](https://npm.io/package/picocolors.md) ^1.1.1
- [jsonpointer](https://npm.io/package/jsonpointer.md) ^5.0.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.22.5
- [@babel/code-frame](https://npm.io/package/@babel/code-frame.md) ^7.22.5
- [@humanwhocodes/momoa](https://npm.io/package/@humanwhocodes/momoa.md) ^2.0.3

## 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.4.0 (latest) — 2025-10-24
- 2.3.2 — 2025-02-28
- 2.3.1 — 2025-02-26
- 2.3.0 — 2025-02-25
- 2.2.2 — 2025-02-15
- 2.2.1 — 2025-02-15
- 2.2.0 — 2025-02-14
- 2.1.2 — 2025-01-29
- 2.1.1 — 2025-01-29
- 2.1.0 — 2025-01-29
- 2.0.0 — 2025-01-23
- 1.6.0 — 2023-03-27
- 1.5.0 — 2022-04-20
- 1.4.6 — 2022-04-12
- 1.4.5 — 2022-03-31
- … 11 more at https://npm.io/package/@readme/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>

## Differences from `better-ajv-errors`

- Only targets current Node LTS releases.
- AJV 8 support.
- Default errors stack the error message at the top of the location instead of the bottom.
  - This helps eliminate long blocks of `^^^^^^` pointers.
- Addition of a `colorize` option for disabling colorization in `format: cli` output.
- Up-to-date dependencies.

## Installation

```bash
$ npm i @readme/better-ajv-errors
$ yarn add @readme/better-ajv-errors
$ pnpm add @readme/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 '@readme/better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('@readme/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`

##### colorize

Type: `boolean`
Default: `true`

When disabled, if you are outputting `cli` formatting it will be without colorized or styled content.

##### 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/@readme/better-ajv-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
