All Projects → ajv-validator → Ajv Errors

ajv-validator / Ajv Errors

Licence: mit
Custom error messages in JSON-Schema for Ajv

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ajv Errors

Maat
Validation and transformation library powered by deductive ascending parser. Made to be extended for any kind of project.
Stars: ✭ 27 (-85.41%)
Mutual labels:  json-schema, validator
Swagger Cli
Swagger 2.0 and OpenAPI 3.0 command-line tool
Stars: ✭ 321 (+73.51%)
Mutual labels:  json-schema, validator
ty
Here is a schema checker which can return well typed results. Tell your friends!
Stars: ✭ 21 (-88.65%)
Mutual labels:  json-schema, validator
finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-90.27%)
Mutual labels:  json-schema, validator
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (-63.78%)
Mutual labels:  json-schema, validator
schema
SpaceAPI JSON schema files.
Stars: ✭ 20 (-89.19%)
Mutual labels:  json-schema, validator
Djv
Dynamic JSON Schema Validator - Supports draft-04/06
Stars: ✭ 281 (+51.89%)
Mutual labels:  json-schema, validator
Jsonschema
JSONSchema (draft04, draft06, draft07) Validation using Go
Stars: ✭ 261 (+41.08%)
Mutual labels:  json-schema, validator
Tram Policy
Policy Object Pattern
Stars: ✭ 16 (-91.35%)
Mutual labels:  validator, errors
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+283.78%)
Mutual labels:  json-schema, validator
another-json-schema
Another JSON Schema validator, simple & flexible & intuitive.
Stars: ✭ 48 (-74.05%)
Mutual labels:  json-schema, validator
Ajv I18n
Internationalised error messages for Ajv JSON-Schema validator
Stars: ✭ 98 (-47.03%)
Mutual labels:  json-schema, validator
Validr
A simple, fast, extensible python library for data validation.
Stars: ✭ 205 (+10.81%)
Mutual labels:  json-schema, validator
openui5-validator
A library to validate OpenUI5 fields
Stars: ✭ 17 (-90.81%)
Mutual labels:  json-schema, validator
Ajv Keywords
Custom JSON-Schema keywords for Ajv validator
Stars: ✭ 186 (+0.54%)
Mutual labels:  json-schema, validator
Formily
Alibaba Group Unified Form Solution -- Support React/ReactNative/Vue2/Vue3
Stars: ✭ 6,554 (+3442.7%)
Mutual labels:  json-schema, validator
Ajv
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Stars: ✭ 10,340 (+5489.19%)
Mutual labels:  json-schema, validator
Ajv Cli
Use 'Another Json Validator' (ajv) from the command line
Stars: ✭ 148 (-20%)
Mutual labels:  json-schema, validator
Go Jsonschema
A tool to generate Go data types from JSON Schema definitions.
Stars: ✭ 164 (-11.35%)
Mutual labels:  json-schema
Whoops
PHP errors for cool kids
Stars: ✭ 12,646 (+6735.68%)
Mutual labels:  errors

ajv-errors

Custom error messages in JSON-Schema for Ajv validator

build npm coverage gitter

Please note

ajv-errors v2 supports ajv v7.

If you are using ajv v6, you should use ajv-errors v1

Contents

Install

npm install ajv-errors

Usage

Add the keyword errorMessages to Ajv instance:

const Ajv = require("ajv").default
const ajv = new Ajv({allErrors: true})
// Ajv option allErrors is required
require("ajv-errors")(ajv /*, {singleError: true} */)

See Options below.

Single message

Replace all errors in the current schema and subschemas with a single message:

const schema = {
  type: "object",
  required: ["foo"],
  properties: {
    foo: {type: "integer"},
  },
  additionalProperties: false,
  errorMessage: "should be an object with an integer property foo only",
}

const 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" /* , ... */},
      ],
    },
  },
]

Messages for keywords

Replace errors for certain keywords in the current schema only:

const 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",
  },
}

const 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" /* , ... */}],
    },
  },
]

For keywords "required" and "dependencies" it is possible to specify different messages for different properties:

const schema = {
  type: "object",
  required: ["foo", "bar"],
  properties: {
    foo: {type: "integer"},
    bar: {type: "string"},
  },
  errorMessage: {
    type: "should be an object", // will not replace internal "type" error for the property "foo"
    required: {
      foo: 'should have an integer property "foo"',
      bar: 'should have a string property "bar"',
    },
  },
}

Messages for properties and items

Replace errors for properties / items (and deeper), regardless where in schema they were created:

const schema = {
  type: "object",
  required: ["foo", "bar"],
  allOf: [
    {
      properties: {
        foo: {type: "integer", minimum: 2},
        bar: {type: "string", minLength: 2},
      },
      additionalProperties: false,
    },
  ],
  errorMessage: {
    properties: {
      foo: "data.foo should be integer >= 2",
      bar: "data.bar should be string with length >= 2",
    },
  },
}

const validate = ajv.compile(schema)
console.log(validate({foo: 1, bar: "a"})) // false
console.log(validate.errors) // processed errors

Processed errors:

[
  {
    keyword: "errorMessage",
    message: "data.foo should be integer >= 2",
    dataPath: "/foo",
    // ...
    params: {
      errors: [{keyword: "minimum" /* , ... */}],
    },
  },
  {
    keyword: "errorMessage",
    message: "data.bar should be string with length >= 2",
    dataPath: "/bar",
    // ...
    params: {
      errors: [{keyword: "minLength" /* , ... */}],
    },
  },
]

Default message

When the value of keyword errorMessage is an object you can specify a message that will be used if any error appears that is not specified by keywords/properties/items using _ property:

const schema = {
  type: "object",
  required: ["foo", "bar"],
  allOf: [
    {
      properties: {
        foo: {type: "integer", minimum: 2},
        bar: {type: "string", minLength: 2},
      },
      additionalProperties: false,
    },
  ],
  errorMessage: {
    type: "data should be an object",
    properties: {
      foo: "data.foo should be integer >= 2",
      bar: "data.bar should be string with length >= 2",
    },
    _: 'data should have properties "foo" and "bar" only',
  },
}

const validate = ajv.compile(schema)
console.log(validate({})) // false
console.log(validate.errors) // processed errors

Processed errors:

[
  {
    keyword: "errorMessage",
    message: 'data should be an object with properties "foo" and "bar" only',
    dataPath: "",
    // ...
    params: {
      errors: [{keyword: "required" /* , ... */}, {keyword: "required" /* , ... */}],
    },
  },
]

The message in property _ of errorMessage replaces the same errors that would have been replaced if errorMessage were a string.

Templates

Custom error messages used in errorMessage keyword can be templates using JSON-pointers or relative JSON-pointers to data being validated, in which case the value will be interpolated. Also see examples of relative JSON-pointers.

The syntax to interpolate a value is ${<pointer>}.

The values used in messages will be JSON-stringified:

  • to differentiate between false and "false", etc.
  • to support structured values.

Example:

const schema = {
  type: "object",
  properties: {
    size: {
      type: "number",
      minimum: 4,
    },
  },
  errorMessage: {
    properties: {
      size: "size should be a number bigger or equal to 4, current value is ${/size}",
    },
  },
}

Using property names in error messages

Property names can be used in error messages with the relative JSON-pointer (e.g. 0#).

Example:

const schema = {
  type: "object",
  properties: {
    size: {
      type: "number",
    },
  },
  additionalProperties: {
    not: true,
    errorMessage: extra property is ${0#}
  }
}

Options

Defaults:

{
  keepErrors: false,
  singleError: false,
}
  • keepErrors: keep original errors. Default is to remove matched errors (they will still be available in params.errors property of generated error). If an error was matched and included in the error generated by errorMessage keyword it will have property emUsed: true.
  • singleError: create one error for all keywords used in errorMessage keyword (error messages defined for properties and items are not merged because they have different dataPaths). Multiple error messages are concatenated. Option values:
    • false (default): create multiple errors, one for each message
    • true: create single error, messages are concatenated using "; "
    • non-empty string: this string is used as a separator to concatenate messages

Supporters

Roger Kepler

Enterprise support

ajv-errors package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.

Security contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].