MCPcopy Index your code
hub / github.com/atlassian/better-ajv-errors

github.com/atlassian/better-ajv-errors @v2.0.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.3 ↗ · + Follow
49 symbols 128 edges 29 files 1 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Core symbols most depended-on inside this repo

getMetaFromPath
called by 11
src/json/get-meta-from-path.js
getSchemaAndData
called by 9
src/test-helpers.js
print
called by 8
src/validation-errors/enum.js
filterRedundantErrors
called by 7
src/helpers.js
makeTree
called by 6
src/helpers.js
getErrors
called by 5
src/utils.js
getLocation
called by 4
src/validation-errors/base.js
getDecoratedPath
called by 4
src/validation-errors/base.js

Shape

Function 21
Method 18
Class 10

Languages

TypeScript100%

Modules by API surface

src/validation-errors/base.js9 symbols
src/utils.js9 symbols
src/validation-errors/required.js5 symbols
src/validation-errors/enum.js5 symbols
src/validation-errors/additional-prop.js5 symbols
src/validation-errors/default.js4 symbols
src/helpers.js3 symbols
src/json/get-decorated-data-path.js2 symbols
src/index.js2 symbols
src/test-helpers.js1 symbols
src/json/utils.js1 symbols
src/json/get-meta-from-path.js1 symbols

For agents

$ claude mcp add better-ajv-errors \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact