@apideck/better-ajv-errors (0.3.6)
Published 2026-02-24 13:47:52 +00:00 by atheaadmin
Installation
@apideck:registry=npm install @apideck/better-ajv-errors@0.3.6"@apideck/better-ajv-errors": "0.3.6"About this package
@apideck/better-ajv-errors 👮♀️
Human-friendly JSON Schema validation for APIs
- Readable and helpful ajv errors
- API-friendly format
- Suggestions for spelling mistakes
- Minimal footprint: 1.56 kB (gzip + minified)
Install
$ 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.
Usage
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 });
}
API
betterAjvErrors
Function that formats ajv validation errors in a human-friendly format.
Parameters
options: BetterAjvErrorsOptionserrors: ErrorObject[] | null | undefinedYour ajv errors, you will find these in theerrorsproperty of your ajv instance (ErrorObjectis a type from the ajv package).data: ObjectThe data you passed to ajv to be validated.schema: JSONSchemaThe schema you passed to ajv to validate against.basePath?: stringAn optional base path to prefix paths returned bybetterAjvErrors. 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.
Return Value
ValidationError[]Array of formatted errors (properties ofValidationErrorbelow)message: stringFormatted error messagesuggestion?: stringOptional suggestion based on provided data and schemapath: stringObject path where the error occurred (example:.foo.bar.0.quz)context: { errorType: DefinedError['keyword']; [additionalContext: string]: unknown }errorTypeiserror.keywordproxied fromajv.errorTypecan be used as a key for i18n if needed. There might be additional properties on context, based on the type of error.
Related
- atlassian/better-ajv-errors was the inspiration for this library. Atlassian's library is more focused on CLI errors, this library is focused on developer-friendly API error messages.
Dependencies
Dependencies
| ID | Version |
|---|---|
| json-schema | ^0.4.0 |
| jsonpointer | ^5.0.0 |
| leven | ^3.1.0 |
Development Dependencies
| ID | Version |
|---|---|
| @size-limit/preset-small-lib | ^7.0.8 |
| ajv | ^8.11.0 |
| eslint-plugin-prettier | ^4.0.0 |
| husky | ^8.0.1 |
| np | ^7.6.1 |
| size-limit | ^7.0.8 |
| tsdx | ^0.14.1 |
| tslib | ^2.4.0 |
| typescript | ^4.7.2 |
Peer Dependencies
| ID | Version |
|---|---|
| ajv | >=8 |
Keywords
apideck
ajv
json
schema
json-schema
errors
human
