Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@apideck/better-ajv-errors
Advanced tools
@apideck/better-ajv-errors is a utility package designed to provide more human-readable error messages for JSON schema validation using Ajv. It enhances the default error messages provided by Ajv, making it easier to understand and debug validation issues.
Human-readable error messages
This feature provides more understandable error messages when JSON schema validation fails. The code sample demonstrates how to use @apideck/better-ajv-errors to enhance the error messages from Ajv.
{"const Ajv = require('ajv');":"","const betterAjvErrors = require('@apideck/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' };":"","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 custom error messages in Ajv. It provides a way to define custom error messages for specific validation keywords, offering more control over the error output. Unlike @apideck/better-ajv-errors, which focuses on making default error messages more readable, ajv-errors allows for custom error message definitions.
ajv-i18n is a package that provides internationalization (i18n) support for Ajv error messages. It translates Ajv error messages into different languages. While @apideck/better-ajv-errors focuses on making error messages more human-readable in English, ajv-i18n is useful for applications that need to support multiple languages.
Human-friendly JSON Schema validation for APIs
$ yarn add @apideck/better-ajv-errors
or
$ npm i @apideck/better-ajv-errors
Also make sure that you've installed ajv at version 8 or higher.
After validating some data with ajv, pass the errors to betterAjvErrors
import Ajv from 'ajv';
import { betterAjvErrors } from '@apideck/better-ajv-errors';
// Without allErrors: true, ajv will only return the first error
const ajv = new Ajv({ allErrors: true });
const valid = ajv.validate(schema, data);
if (!valid) {
const betterErrors = betterAjvErrors({ schema, data, errors: ajv.errors });
}
Function that formats ajv validation errors in a human-friendly format.
options: BetterAjvErrorsOptions
errors: ErrorObject[] | null | undefined
Your ajv errors, you will find these in the errors
property of your ajv instance (ErrorObject
is a type from the ajv package).data: Object
The data you passed to ajv to be validated.schema: JSONSchema
The schema you passed to ajv to validate against.basePath?: string
An optional base path to prefix paths returned by betterAjvErrors
. For example, in APIs, it could be useful to use '{requestBody}'
or '{queryParemeters}'
as a basePath. This will make it clear to users where exactly the error occurred.ValidationError[]
Array of formatted errors (properties of ValidationError
below)
message: string
Formatted error messagesuggestion?: string
Optional suggestion based on provided data and schemapath: string
Object path where the error occurred (example: .foo.bar.0.quz
)context: { errorType: DefinedError['keyword']; [additionalContext: string]: unknown }
errorType
is error.keyword
proxied from ajv
. errorType
can be used as a key for i18n if needed. There might be additional properties on context, based on the type of error.FAQs
Human-friendly JSON Schema validation for APIs
The npm package @apideck/better-ajv-errors receives a total of 2,730,020 weekly downloads. As such, @apideck/better-ajv-errors popularity was classified as popular.
We found that @apideck/better-ajv-errors demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.