Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@readme/better-ajv-errors
Advanced tools
@readme/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 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 @readme/better-ajv-errors to enhance the error messages for a simple JSON schema validation.
const Ajv = require('ajv');
const betterAjvErrors = require('@readme/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 a package that allows you to define custom error messages for your JSON schema validations. Unlike @readme/better-ajv-errors, which focuses on making default error messages more readable, ajv-errors lets you specify exactly what the error messages should be.
ajv-i18n provides internationalization support for AJV error messages. It translates AJV's default error messages into various languages. While @readme/better-ajv-errors focuses on readability, ajv-i18n focuses on localization.
ajv-keywords adds custom validation keywords to AJV, allowing for more complex validation logic. It doesn't directly improve error messages but can be used in conjunction with @readme/better-ajv-errors to provide more detailed validation and error reporting.
JSON Schema validation for Human 👨🎤
Main goal of this library is to provide relevant error messages like the following:
better-ajv-errors
^^^^^^
pointers.colorize
option for disabling colorization in format: cli
output.$ npm i 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');
// 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: boolean
Default: true
When disabled, if you are outputting cli
formatting it will be without colorized or styled content.
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
We found that @readme/better-ajv-errors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.