@stoplight/better-ajv-errors

WebJar for @stoplight/better-ajv-errors

License

License

Apache-2.0
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

stoplight__better-ajv-errors
Last Version

Last Version

0.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

@stoplight/better-ajv-errors
WebJar for @stoplight/better-ajv-errors
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/stoplightio/better-ajv-errors

Download stoplight__better-ajv-errors

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/stoplight__better-ajv-errors/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>stoplight__better-ajv-errors</artifactId>
    <version>0.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/stoplight__better-ajv-errors/
implementation 'org.webjars.npm:stoplight__better-ajv-errors:0.0.3'
// https://jarcasting.com/artifacts/org.webjars.npm/stoplight__better-ajv-errors/
implementation ("org.webjars.npm:stoplight__better-ajv-errors:0.0.3")
'org.webjars.npm:stoplight__better-ajv-errors:jar:0.0.3'
<dependency org="org.webjars.npm" name="stoplight__better-ajv-errors" rev="0.0.3">
  <artifact name="stoplight__better-ajv-errors" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='stoplight__better-ajv-errors', version='0.0.3')
)
libraryDependencies += "org.webjars.npm" % "stoplight__better-ajv-errors" % "0.0.3"
[org.webjars.npm/stoplight__better-ajv-errors "0.0.3"]

Dependencies

compile (2)

Group / Artifact Type Version
org.webjars.npm : jsonpointer jar [4.0.1,5)
org.webjars.npm : leven jar [3.1.0,4)

Project Modules

There are no modules declared in this project.

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.

org.webjars.npm

Stoplight

Stoplight leverages your OpenAPI documents to drive the entire API development process

Versions

Version
0.0.3