npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-ajv-errors

v1.0.1

Published

Parses AJV errors and determines the best message

Downloads

3

Readme

Simple AJV Errors

NPM SIZE SPONSOR

A function that accepts an array of errors that were returned from ajv.validate(), and returns an array of simplified error messages along with some of the information from the original ajv error.

Examples

Example 1

Schema:

// An array of:
//   enum ("tab" or "first")
//   integer
//   object
[
    "tab" | "first" | integer | {   
        //... 
    }
]

Data:

["space", "2"]

ajv errors:

/0 must be equal to one of the allowed values
/0 must be integer
/0 must be object
/0 must match exactly one schema in oneOf

Simple AJV Errors:

data[0] must be one of the following: 'tab', 'first' or integer or object

Example 2

Schema:

{
  //...
  "maxDepth": integer (x ≥ 1) | "∞",   
}

Data:

{ maxDepth: true }

ajv errors:

/maxDepth must be integer
/maxDepth must be string
/maxDepth must be equal to one of the allowed values
/maxDepth must match exactly one schema in oneOf

Simple AJV Errors:

data.maxDepth must be integer or '∞'

Example 3

Schema:

{   
  //...
  "supported": "composition" | "nesting" | {
    (required) "some": "composition" | "nesting"
  } | {   
    (required) "every": "composition" | "nesting"
  },
}

Data:

{
    //...
    supported: {
    all: 'nesting',
    },
}

ajv errors:

/supported must be string
/supported must be equal to one of the allowed values
/supported must have required property 'some'
/supported must have required property 'every'
/supported must match exactly one schema in oneOf

Simple AJV Errors:

data.supported must include property 'some' or include property 'every'

Example 4

Schema:

// An array of unique objects, each requiring properties:
//    either `object` or `property`
//    `message`
[
  {   
    (required) "object": string,   
    "property": string,   
    (required) "message": string   
  } | {   
    "object": string,   
    (required) "property": string,   
    (required) "message": string   
  }   
  # unique   
]

Data:

[
  {
    property: 'callee',
    message: 'arguments.callee is deprecated',
  },
  {
    message: 'Please use Number.isFinite instead',
  },
  {
    object: 'window',
    property: 'isFinite',
    message: 'Please use Number.isFinite instead',
  },
  {
    object: 'window',
    property: 'isFinite',
    message: 'Please use Number.isFinite instead',
  }
]

ajv errors:

/1 must have required property 'object'
/1 must have required property 'property'
/1 must match a schema in anyOf
must NOT have duplicate items (items ## 2 and 3 are identical)

Simple AJV Errors:

data must have all unique items (items 2 and 3 are identical)
data[1] must include property 'object' or include property 'property'

Installation

npm install simple-ajv-errors

Usage

API

getSimpleErrors(errors: ErrorObject[], options?: Options): SimpleError[];

Types

interface Options {
    // Used in error messages as part of a reference path
    //  EX: `data.someProp[0].prop must not be empty`
    dataVar: string; // default: 'data'
}

interface SimpleError {
    // Simplified error message with clear wording
    //  EX: `data[0] must be string or integer`
    message: string;

    // Copied directly from ajv errors (when available)
    instancePath: string;
    schemaPath: string;
    schema?: any;
    parentSchema?: object;
    data?: any;
}

Implementation

import Ajv from 'ajv';
import { getSimpleErrors } from 'simple-ajv-errors';

const ajv = new Ajv({
    // ...
    allErrors: true,
    verbose: true,
});

const schema = { /* ... */ };
const data = { /* ... */ };

const valid = ajv.validate(schema, data);
if (valid === false) {
    const simpleErrors = getSimpleErrors(ajv.errors, {
        dataVar: 'root'
    });
}

ajv options

Simple AJV Errors supports any/all options passed to ajv.validate(). There are two configuration options, however, that would improve your experience. Those are allErrors and verbose.

allErrors

  • Default: false
  • Suggestion: true

The default value of allErrors in ajv is false, which directs it to return after the first error found. Setting this value to true directs it to check all rules and collect all errors. Many might feel this option makes ajv errors too verbose and opt for less information in order to improve their experience, by setting this value to false.

Simple AJV Errors solves this by determining which errors are the most applicable and filtering out the noise.

Read more about this option in the ajv documentation.

verbose

  • Default: false
  • Suggestion: true

Setting this value to true directs ajv to include the relevant part of the schema and data with each error. This gives Simple AJV Errors information to better determine the most applicable errors. Setting this to false will result in less applicable errors, but will still improve the wording.

Read more about this option in the ajv documentation.