MikeRalphson / better-json-schema-errors

Human-friendly JSON Schema validation for APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

better-json-schema-errors

Human-friendly JSON Schema validation for APIs

  • Readable and helpful JSON Schema errors
  • API-friendly format
  • Suggestions for spelling mistakes (using Jaro-Winkler, not the more simplistic Levenshtein) distance algorithm
  • Minimal footprint: 1.56 kB (gzip + minified)

better-json-schema-errors output Example

Install

$ npm i better-json-schema-errors

Usage

After validating some data with a compliant JSON Schema validator, pass the errors to betterJsonSchemaErrors

Your JSON Schema Validator should be configured to use the Detailed or Verbose error structure.

import { betterJsonSchemaErrors } from 'better-json-schema-errors';

const valid = validator.validate(schema, data);

if (!valid) {
  const betterErrors = betterJsonSchemaErrors({ schema, data, errors: validator.errors });
}

API

betterJsonSchemaErrors

Function that formats JSON Schema validation errors in a human-friendly format.

Parameters

  • options: BetterJsonSchemaErrorsOptions
    • errors: ErrorObject[] | null | undefined Your errors, you will find these in the errors property of your validator instance (ErrorObject is a type defined by JSON Schema).
    • data: Object The data you passed to the validator.
    • schema: JSONSchema The schema you passed to the validator to validate against.
    • basePath?: string An optional base path to prefix paths returned by betterJsonSchemaErrors. 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 the validator. 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

About

Human-friendly JSON Schema validation for APIs

License:MIT License


Languages

Language:TypeScript 100.0%