better-avj-errors
JSON Schema validation for Human
Main goal of this library is to provide relevant error messages like the following:
You can also use it in "return" mode when library returns structured errors.
Installation
$ yarn add better-avj-errors$ # Or $ npm i better-avj-errors
Also make sure that you installed ajv package to validate data against JSON schemas.
Usage
You need to validate data first with ajv. Then you can pass validate.errors
object into better-ajv-errors
.
;// const Ajv = require('ajv');;// const betterAjvErrors = require('better-avj-errors'); // You need to pass `jsonPointers: true`const ajv = jsonPointers: true ; // Load schema and dataconst schema = ...;const data = ...; const validate = ajv;const valid = ;
"Print" mode
// ...validate data firstconst print = ; if !valid ;
"Return" mode
// ...validate data firstconst getHumanErrors = ; if !valid const errors = ; /* errors is array: [ { "error": "You're using invalid field FOO", "line": 14, "column": 75, "suggestion": "Maybe you meant BAR?" } ] */