New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

better-avj-errors

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-avj-errors

JSON Schema validation for Human

0.4.0-alpha.3
latest
npm
Version published
Weekly downloads
5
150%
Maintainers
2
Weekly downloads
 
Created
Source

better-avj-errors

JSON Schema validation for Human

Main goal of this library is to provide relevant error messages like the following:

Enum Validation Error

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.

import Ajv from 'ajv';
// const Ajv = require('ajv');
import betterAjvErrors from 'better-avj-errors';
// const betterAjvErrors = require('better-avj-errors');

// You need to pass `jsonPointers: true`
const ajv = new Ajv({ jsonPointers: true });

// Load schema and data
const schema = ...;
const data = ...;

const validate = ajv.compile(schema);
const valid = validate(data);

"Print" mode

// ...validate data first
const print = betterAjvErrors({ schema, mode: 'print', indent: 2 });

if (!valid) {
  print(data, validate.errors);
}

"Return" mode

// ...validate data first
const getHumanErrors = betterAjvErrors({ schema, mode: 'return', indent: 2 });

if (!valid) {
  const errors = getHumanErrors(data, validate.errors);

  /*
  errors is array: [
    {
      "error": "You're using invalid field FOO",
      "line": 14,
      "column": 75,
      "suggestion": "Maybe you meant BAR?"
    }
  ]
  */
}

FAQs

Package last updated on 19 Nov 2017

Did you know?

Socket

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