Socket
Socket
Sign inDemoInstall

ajv-errors

Package Overview
Dependencies
6
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ajv-errors

Custom error messages in JSON-Schema for Ajv validator


Version published
Maintainers
1
Install size
14.8 kB
Created

Package description

What is ajv-errors?

The ajv-errors package allows you to customize error messages in JSON Schema validation using the Ajv validator. It provides a way to enhance the default error messages that Ajv produces, making them more informative and user-friendly. This can be particularly useful in applications where precise and clear validation errors are necessary, such as form validation in web applications.

What are ajv-errors's main functionalities?

Custom error messages for specific validation keywords

This feature allows you to specify custom error messages for specific validation keywords, such as 'required', 'type', etc. In the provided code sample, a custom error message is defined for when the 'name' field is missing.

{ "type": "object", "properties": { "name": { "type": "string" } }, "required": ["name"], "errorMessage": { "required": { "name": "Name is required." } } }

Conditional error messages

This feature enables conditional error messages based on the validation outcome. In the example, a custom error message is set for the 'passwordConfirm' field to ensure it matches the 'password' field.

{ "type": "object", "properties": { "password": { "type": "string" }, "passwordConfirm": { "type": "string", "const": { "$data": "1/password" } } }, "errorMessage": { "properties": { "passwordConfirm": "Passwords must match." } } }

Top-level error messages

Allows setting a top-level error message for the entire schema. This is useful for providing a general error message instead of or in addition to specific field errors. The example sets a general error message regarding the 'age' field.

{ "type": "object", "properties": { "age": { "type": "number" } }, "required": ["age"], "errorMessage": "An age is required and must be a number." }

Other packages similar to ajv-errors

Readme

Source

ajv-errors

Custom error messages in JSON-Schema for Ajv validator

Build Status npm version Coverage Status Gitter

Install

npm install ajv-errors

Usage

Add the keyword errorMessages to Ajv instance:

var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true}); // option allErrors is required
require('ajv-errors')(ajv);
Replace all errors in the current schema and subschemas with a single message:
var schema = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: { type: 'integer' }
  },
  additionalProperties: false,
  errorMessage: 'should be an object with an integer property foo only'
};

var validate = ajv.compile(schema);
console.log(validate({foo: 'a', bar: 2})); // false
console.log(validate.errors); // processed errors

Processed errors:

[
  {
    keyword: 'errorMessage',
    message: 'should be an object with an integer property foo only',
    // ...
    params: {
      errors: [
        { keyword: 'additionalProperties', dataPath: '' /* , ... */ },
        { keyword: 'type', dataPath: '.foo' /* , ... */ }
      ]
    }
  }
]
Replace errors for certain keywords in the current schema only:
var schema = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: { type: 'integer' }
  },
  additionalProperties: false,
  errorMessage: {
    type: 'should be an object', // will not replace internal "type" error for the property "foo"
    required: 'should have property foo',
    additionalProperties: 'should not have properties other than foo'
  }
};

var validate = ajv.compile(schema);
console.log(validate({foo: 'a', bar: 2})); // false
console.log(validate.errors); // processed errors

Processed errors:

[
  {
    // original error
    keyword: type,
    dataPath: '.foo',
    // ...
    message: 'should be integer'
  },
  {
    // generated error
    keyword: 'errorMessage',
    message: 'should not have properties other than foo',
    // ...
    params: {
      errors: [
        { keyword: 'additionalProperties' /* , ... */ }
      ]
    },
  }
]

License

MIT

Keywords

FAQs

Last updated on 30 Apr 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc