Socket
Socket
Sign inDemoInstall

better-ajv-errors

Package Overview
Dependencies
21
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    better-ajv-errors

JSON Schema validation for Human


Version published
Weekly downloads
305K
decreased by-1.25%
Maintainers
3
Install size
416 kB
Created
Weekly downloads
 

Changelog

Source

1.2.0

Minor Changes

  • 3918d58: Add integration with ajv-errors

Patch Changes

  • 6120105: Remove for...in loop to prevent possible enumeration errors

Readme

Source

better-ajv-errors

JSON Schema validation for Human 👨‍🎤

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

Installation

$ npm i better-ajv-errors
$ # Or
$ yarn add better-ajv-errors

Also make sure that you installed ajv package to validate data against JSON schemas.

Usage

First, you need to validate your payload with ajv. If it's invalid then you can pass validate.errors object into better-ajv-errors.

import Ajv from 'ajv';
import betterAjvErrors from 'better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('better-ajv-errors').default;
// Or
// const { default: betterAjvErrors } = require('better-ajv-errors');

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

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

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

if (!valid) {
  const output = betterAjvErrors(schema, data, validate.errors);
  console.log(output);
}

API

betterAjvErrors(schema, data, errors, [options])

Returns formatted validation error to print in console. See options.format for further details.

schema

Type: Object

The JSON Schema you used for validation with ajv

data

Type: Object

The JSON payload you validate against using ajv

errors

Type: Array

Array of ajv validation errors

options

Type: Object

format

Type: string
Default: cli
Values: cli js

Use default cli output format if you want to print beautiful validation errors like following:

Or, use js if you are planning to use this with some API. Your output will look like following:

[
  {
    start: { line: 6, column: 15, offset: 70 },
    end: { line: 6, column: 26, offset: 81 },
    error:
      '/content/0/type should be equal to one of the allowed values: panel, paragraph, ...',
    suggestion: 'Did you mean paragraph?',
  },
];
indent

Type: number null
Default: null

If you have an unindented JSON payload and you want the error output indented.

This option have no effect when using the json option.

json

Type: string null
Default: null

Raw JSON payload used when formatting codeframe. Gives accurate line and column listings.

Keywords

FAQs

Last updated on 12 Apr 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