Socket
Socket
Sign inDemoInstall

@stoplight/better-ajv-errors

Package Overview
Dependencies
8
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @stoplight/better-ajv-errors

JSON Schema validation for Human


Version published
Weekly downloads
580K
increased by3.67%
Maintainers
1
Install size
42.9 kB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 22 Aug 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