Socket
Socket
Sign inDemoInstall

@apideck/better-ajv-errors

Package Overview
Dependencies
9
Maintainers
7
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @apideck/better-ajv-errors

Human-friendly JSON Schema validation for APIs


Version published
Weekly downloads
2.6M
decreased by-17.21%
Maintainers
7
Install size
113 kB
Created
Weekly downloads
 

Readme

Source

npm (scoped) npm GitHub Workflow Status

@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)

better-ajv-errors output Example

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: 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.
Return Value
  • ValidationError[] Array of formatted errors (properties of ValidationError below)
    • message: string Formatted error message
    • suggestion?: string Optional suggestion based on provided data and schema
    • path: 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.
  • 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.

Keywords

FAQs

Last updated on 28 Jun 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc