Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
better-ajv-errors
Advanced tools
The better-ajv-errors package provides improved error messages for the AJV JSON schema validator. It helps developers understand validation errors more easily by presenting them in a more readable and user-friendly format.
Readable Error Messages
This feature provides more readable error messages when validation fails. The code sample demonstrates how to use better-ajv-errors to display a user-friendly error message when the data does not conform to the schema.
const Ajv = require('ajv');
const betterAjvErrors = require('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);
}
Custom Error Formatting
This feature allows for custom formatting of error messages. The code sample shows how to format the error messages for command-line interface (CLI) output with indentation.
const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors');
const ajv = new Ajv();
const schema = {
type: 'object',
properties: {
email: { type: 'string', format: 'email' }
},
required: ['email']
};
const data = {
email: 'not-an-email'
};
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);
}
The ajv-errors package allows for custom error messages in AJV. It provides a way to define custom error messages directly in the JSON schema, which can be useful for providing more context-specific error information. Unlike better-ajv-errors, which focuses on improving the readability of default error messages, ajv-errors allows for the customization of error messages at the schema level.
The ajv-i18n package provides internationalization (i18n) support for AJV error messages. It translates AJV error messages into different languages, making it useful for applications that need to support multiple languages. While better-ajv-errors focuses on making error messages more readable, ajv-i18n focuses on translating them into different languages.
JSON Schema validation for Human
Main goal of this library is to provide relevant error messages like the following:
You can also use it in "return" mode when library returns structured errors.
$ yarn add better-ajv-errors
$ # Or
$ npm i better-ajv-errors
Also make sure that you installed ajv package to validate data against JSON schemas.
You need to validate data first with ajv. Then you can pass validate.errors
object into better-ajv-errors
.
import Ajv from 'ajv';
// const Ajv = require('ajv');
import betterAjvErrors from 'better-ajv-errors';
// 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);
// ...validate data first
const print = betterAjvErrors({ schema, mode: 'print', indent: 2 });
if (!valid) {
print(data, validate.errors);
}
// ...validate data first
const getHumanErrors = betterAjvErrors({ schema, mode: 'return', indent: 2 });
if (!valid) {
const errors = getHumanErrors(data, validate.errors);
/*
errors is array: [
{
"error": "You're using invalid field FOO",
"line": 14,
"column": 75,
"suggestion": "Maybe you meant BAR?"
}
]
*/
}
FAQs
JSON Schema validation for Human
The npm package better-ajv-errors receives a total of 321,436 weekly downloads. As such, better-ajv-errors popularity was classified as popular.
We found that better-ajv-errors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.