Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-ajv-errors

Package Overview
Dependencies
Maintainers
3
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-ajv-errors

JSON Schema validation for Human

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
125K
decreased by-61.72%
Maintainers
3
Weekly downloads
 
Created

What is better-ajv-errors?

The better-ajv-errors package provides improved error messages for the AJV JSON schema validator. It helps developers understand validation errors more easily by presenting them in a more readable and user-friendly format.

What are better-ajv-errors's main functionalities?

Readable Error Messages

This feature provides more readable error messages when validation fails. The code sample demonstrates how to use better-ajv-errors to display a user-friendly error message when the data does not conform to the schema.

const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors');

const ajv = new Ajv();
const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'integer' }
  },
  required: ['name', 'age']
};

const data = {
  name: 'John Doe',
  age: 'twenty-five'
};

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

if (!valid) {
  const output = betterAjvErrors(schema, data, validate.errors, { format: 'js' });
  console.log(output);
}

Custom Error Formatting

This feature allows for custom formatting of error messages. The code sample shows how to format the error messages for command-line interface (CLI) output with indentation.

const Ajv = require('ajv');
const betterAjvErrors = require('better-ajv-errors');

const ajv = new Ajv();
const schema = {
  type: 'object',
  properties: {
    email: { type: 'string', format: 'email' }
  },
  required: ['email']
};

const data = {
  email: 'not-an-email'
};

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

if (!valid) {
  const output = betterAjvErrors(schema, data, validate.errors, { format: 'cli', indent: 2 });
  console.log(output);
}

Other packages similar to better-ajv-errors

Keywords

FAQs

Package last updated on 12 Apr 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc