🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

@stoplight/better-ajv-errors

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/better-ajv-errors

JSON Schema validation for Human

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
1.5M
-10.78%
Maintainers
1
Weekly downloads
 
Created
Source

JSON Schema validation for Human 👨‍🎤

Main goal of this library is to provide relevant AJV error messages. It's a fork of great better-ajv-errors by Atlassian, with focus on being leaner.

Installation

$ yarn add @stoplight/better-ajv-errors

or

$ npm i @stoplight/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 '@stoplight/better-ajv-errors';
// const Ajv = require('ajv');
// const betterAjvErrors = require('better-ajv-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);

if (!valid) {
  const output = betterAjvErrors(schema, validate.errors, {
    propertyPath: [],
    targetValue: data,
  });
  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.

errors

Type: Array

Array of ajv validation errors

options

Type: Object

propertyPath

Type: Array

Property path of a validated object that is a part of a bigger document. Might be empty if the validated object equals the whole document.

targetValue

Type: Object

The JSON payload you validate against using ajv.

Keywords

json-schema

FAQs

Package last updated on 22 Aug 2022

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