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

@apideck/better-ajv-errors

v0.3.6

Published

Human-friendly JSON Schema validation for APIs

Downloads

13,090,842

Readme

npm (scoped) npm GitHub Workflow Status

@apideck/better-ajv-errors 👮‍♀️

Human-friendly JSON Schema validation for APIs

  • Readable and helpful ajv errors
  • API-friendly format
  • Suggestions for spelling mistakes
  • Minimal footprint: 1.56 kB (gzip + minified)

better-ajv-errors output Example

Install

$ yarn add @apideck/better-ajv-errors

or

$ npm i @apideck/better-ajv-errors

Also make sure that you've installed ajv at version 8 or higher.

Usage

After validating some data with ajv, pass the errors to betterAjvErrors

import Ajv from 'ajv';
import { betterAjvErrors } from '@apideck/better-ajv-errors';

// Without allErrors: true, ajv will only return the first error
const ajv = new Ajv({ allErrors: true });

const valid = ajv.validate(schema, data);

if (!valid) {
  const betterErrors = betterAjvErrors({ schema, data, errors: ajv.errors });
}

API

betterAjvErrors

Function that formats ajv validation errors in a human-friendly format.

Parameters

  • options: BetterAjvErrorsOptions
    • errors: ErrorObject[] | null | undefined Your ajv errors, you will find these in the errors property of your ajv instance (ErrorObject is a type from the ajv package).
    • data: Object The data you passed to ajv to be validated.
    • schema: JSONSchema The schema you passed to ajv to validate against.
    • basePath?: string An optional base path to prefix paths returned by betterAjvErrors. For example, in APIs, it could be useful to use '{requestBody}' or '{queryParemeters}' as a basePath. This will make it clear to users where exactly the error occurred.

Return Value

  • ValidationError[] Array of formatted errors (properties of ValidationError below)
    • message: string Formatted error message
    • suggestion?: string Optional suggestion based on provided data and schema
    • path: string Object path where the error occurred (example: .foo.bar.0.quz)
    • context: { errorType: DefinedError['keyword']; [additionalContext: string]: unknown } errorType is error.keyword proxied from ajv. errorType can be used as a key for i18n if needed. There might be additional properties on context, based on the type of error.

Related

  • atlassian/better-ajv-errors was the inspiration for this library. Atlassian's library is more focused on CLI errors, this library is focused on developer-friendly API error messages.