
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
@sidvind/better-ajv-errors
Advanced tools
@sidvind/better-ajv-errors is a utility package designed to provide more human-readable error messages when using the Ajv JSON schema validator. It enhances the default error messages provided by Ajv, making it easier to understand and debug schema validation issues.
Human-readable error messages
This feature allows you to generate more readable error messages from Ajv validation errors. The code sample demonstrates how to use @sidvind/better-ajv-errors to enhance the error messages for a simple JSON schema validation.
const Ajv = require('ajv');
const betterAjvErrors = require('@sidvind/better-ajv-errors');
const ajv = new Ajv();
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' }
},
required: ['name', 'age']
};
const data = {
name: 'John Doe',
age: 'twenty-five'
};
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) {
const output = betterAjvErrors(schema, data, validate.errors, { format: 'js' });
console.log(output);
}
ajv-errors is an Ajv plugin that allows for custom error messages in JSON schema validation. It provides a way to define custom error messages directly in the schema, which can be more flexible but requires more setup compared to @sidvind/better-ajv-errors.
ajv-i18n is another Ajv plugin that provides internationalization (i18n) for Ajv error messages. It translates Ajv error messages into different languages, which can be useful for applications that need to support multiple languages. Unlike @sidvind/better-ajv-errors, it focuses on translation rather than improving readability.
JSON Schema validation for Human 👨🎤
Main goal of this library is to provide relevant error messages like the following:
This fork removes the legacy build and thus drops dependencies such as core-js
and @babel/runtime
which is not needed on modern installations.
The following PRs from upstream are merged:
The following PRs has been merged upstream:
$ npm i @sidvind/better-ajv-errors
Also make sure that you installed ajv package to validate data against JSON schemas.
First, you need to validate your payload with ajv
. If it's invalid then you can pass validate.errors
object into better-ajv-errors
.
import Ajv from 'ajv';
import betterAjvErrors from '@sidvind/better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('@sidvind/better-ajv-errors').default;
// Or
// const { default: betterAjvErrors } = require('@sidvid/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);
}
Returns formatted validation error to print in console
. See options.format
for further details.
Type: Object
The JSON Schema you used for validation with ajv
Type: Object
The JSON payload you validate against using ajv
Type: Array
Array of ajv validation errors
Type: Object
Type: string
Default: cli
Values: cli
js
Use default cli
output format if you want to print beautiful validation errors like following:
Or, use js
if you are planning to use this with some API. Your output will look like following:
[
{
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?',
},
];
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.
Type: string
null
Default: null
Raw JSON payload used when formatting codeframe. Gives accurate line and column listings.
4.0.0
FAQs
JSON Schema validation for Human
The npm package @sidvind/better-ajv-errors receives a total of 377,601 weekly downloads. As such, @sidvind/better-ajv-errors popularity was classified as popular.
We found that @sidvind/better-ajv-errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.