Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
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:
$ npm i better-ajv-errors
$ # Or
$ yarn add 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 '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);
}
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.
FAQs
JSON Schema validation for Human
The npm package better-ajv-errors receives a total of 64,064 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.