Coder Social home page Coder Social logo

kochcj1 / better-ajv-errors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apideck-libraries/better-ajv-errors

0.0 0.0 0.0 913 KB

Human-friendly JSON Schema validation for APIs ๐Ÿ‘ฎโ€โ™€๏ธ

License: MIT License

TypeScript 100.00%

better-ajv-errors's Introduction

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.

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.

better-ajv-errors's People

Contributors

elsmr avatar thw0rted avatar dependabot[bot] avatar kochcj1 avatar gdewilde avatar wopian avatar jeffposnick avatar snyk-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.