Registry / testing / better-ajv-errors

better-ajv-errors

JSON →
library2.0.3jsnpmunverified

A library that formats JSON Schema validation errors from Ajv into human-readable messages with codeframe highlighting and suggestions. Current version 2.0.3 supports Ajv versions 4.11.8 through 8, and requires Node >= 18.20.6. Ships TypeScript types and offers both CLI (console-friendly) and JS (structured) output formats. Unlike raw Ajv error objects, better-ajv-errors provides line/column references and 'did you mean?' suggestions.

npm install better-ajv-errors
INSTALL
IMPORT
SIG · BETTER-AJV-ERRORS
B
better-ajv-errors
testingjavascriptv2.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (betterAjvErrors)
import betterAjvErrors from 'better-ajv-errors';
const betterAjvErrors = require('better-ajv-errors');
In CommonJS, use require('better-ajv-errors').default to access the default export.
default (CommonJS)
const betterAjvErrors = require('better-ajv-errors').default;
const betterAjvErrors = require('better-ajv-errors');
Direct require returns an object with a default property (ESM default export wrapped).
ErrorOutput type (TypeScript)
import type { ErrorOutput } from 'better-ajv-errors';
ErrorOutput is the type for output when format: 'js' is used. Available since v2.

Demonstrates validating a payload with Ajv and formatting errors with better-ajv-errors in CLI mode.

import Ajv from 'ajv'; import betterAjvErrors from 'better-ajv-errors'; const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'integer', minimum: 0 }, }, required: ['name', 'age'], }; const data = { name: 42, age: -1 }; const ajv = new Ajv({ allErrors: true, verbose: true }); const validate = ajv.compile(schema); const valid = validate(data); if (!valid) { const output = betterAjvErrors(schema, data, validate.errors, { format: 'cli', indent: 2 }); console.log(output); }
Debug
Known issues
gotchaRequires Ajv instance with allErrors: true and verbose: true to get detailed error information.
fix
new Ajv({ allErrors: true, verbose: true })
affects: *
gotchaCommonJS require: const betterAjvErrors = require('better-ajv-errors').default; not just require('better-ajv-errors').
fix
Use .default or an import statement.
affects: >=2.0.0
gotchaOutput format 'js' returns an array of objects with start/end positions; it is not the same as the default 'cli' string.
fix
Set format option to 'js' if you need machine-readable output.
affects: *
Errors
Common errors & fixes
TypeError: betterAjvErrors is not a function
Using CommonJS require without .default on versions >=2
fix
Use require('better-ajv-errors').default
Error: Unknown option "format"
Passing options as second or third argument incorrectly
fix
Call betterAjvErrors(schema, data, errors, { format: 'js' })
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies
ajvrequiredpeer dependency; used to produce the validation errors that better-ajv-errors formats
Agent activity
23 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources
better-ajv-errors — npm install better-ajv-errors · libregistry