Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@stoplight/better-ajv-errors
Advanced tools
@stoplight/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 @stoplight/better-ajv-errors to transform AJV's default error messages into a more understandable format.
const Ajv = require('ajv');
const betterAjvErrors = require('@stoplight/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. Unlike @stoplight/better-ajv-errors, which focuses on making default error messages more readable, ajv-errors lets you define custom error messages directly in your JSON schema.
ajv-i18n is another AJV plugin that provides internationalization (i18n) for AJV error messages. It translates AJV's default error messages into various languages. While @stoplight/better-ajv-errors focuses on readability, ajv-i18n focuses on localization.
JSON Schema validation for Human 👨🎤
Main goal of this library is to provide relevant AJV error messages. It's a fork of great better-ajv-errors by Atlassian, with focus on being leaner.
$ yarn add @stoplight/better-ajv-errors
or
$ npm i @stoplight/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 '@stoplight/better-ajv-errors';
// const Ajv = require('ajv');
// 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);
if (!valid) {
const output = betterAjvErrors(schema, validate.errors, {
propertyPath: [],
targetValue: data,
});
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: Array
Array of ajv validation errors
Type: Object
Type: Array
Property path of a validated object that is a part of a bigger document. Might be empty if the validated object equals the whole document.
Type: Object
The JSON payload you validate against using ajv
.
FAQs
JSON Schema validation for Human
The npm package @stoplight/better-ajv-errors receives a total of 671,604 weekly downloads. As such, @stoplight/better-ajv-errors popularity was classified as popular.
We found that @stoplight/better-ajv-errors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.