Gathering detailed insights and metrics for ajv-errors
Gathering detailed insights and metrics for ajv-errors
Gathering detailed insights and metrics for ajv-errors
Gathering detailed insights and metrics for ajv-errors
npm install ajv-errors
Typescript
Module System
Node Version
NPM Version
98.9
Supply Chain
100
Quality
75.4
Maintenance
100
Vulnerability
99.6
License
TypeScript (97.27%)
JavaScript (2.73%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
2,705,316,857
Last Day
1,170,211
Last Week
5,975,158
Last Month
26,168,141
Last Year
330,055,193
MIT License
290 Stars
99 Commits
20 Forks
5 Watchers
9 Branches
5 Contributors
Updated on Sep 01, 2025
Latest Version
3.0.0
Package Id
ajv-errors@3.0.0
Size
12.75 kB
NPM Version
6.14.11
Node Version
14.16.0
Published on
Mar 27, 2021
Cumulative downloads
Total Downloads
Last Day
9.9%
1,170,211
Compared to previous day
Last Week
-1.2%
5,975,158
Compared to previous week
Last Month
-18.8%
26,168,141
Compared to previous month
Last Year
-3.5%
330,055,193
Compared to previous year
Custom error messages in JSON-Schema for Ajv validator
Please note
ajv-errors v3 supports ajv v8.
If you are using ajv v6, you should use ajv-errors v1
npm install ajv-errors
Add the keyword errorMessages
to Ajv instance:
1const Ajv = require("ajv").default 2const ajv = new Ajv({allErrors: true}) 3// Ajv option allErrors is required 4require("ajv-errors")(ajv /*, {singleError: true} */)
See Options below.
Replace all errors in the current schema and subschemas with a single message:
1const schema = { 2 type: "object", 3 required: ["foo"], 4 properties: { 5 foo: {type: "integer"}, 6 }, 7 additionalProperties: false, 8 errorMessage: "should be an object with an integer property foo only", 9} 10 11const validate = ajv.compile(schema) 12console.log(validate({foo: "a", bar: 2})) // false 13console.log(validate.errors) // processed errors
Processed errors:
1[ 2 { 3 keyword: "errorMessage", 4 message: "should be an object with an integer property foo only", 5 // ... 6 params: { 7 errors: [ 8 {keyword: "additionalProperties", instancePath: "" /* , ... */}, 9 {keyword: "type", instancePath: ".foo" /* , ... */}, 10 ], 11 }, 12 }, 13]
Replace errors for certain keywords in the current schema only:
1const schema = { 2 type: "object", 3 required: ["foo"], 4 properties: { 5 foo: {type: "integer"}, 6 }, 7 additionalProperties: false, 8 errorMessage: { 9 type: "should be an object", // will not replace internal "type" error for the property "foo" 10 required: "should have property foo", 11 additionalProperties: "should not have properties other than foo", 12 }, 13} 14 15const validate = ajv.compile(schema) 16console.log(validate({foo: "a", bar: 2})) // false 17console.log(validate.errors) // processed errors
Processed errors:
1[ 2 { 3 // original error 4 keyword: type, 5 instancePath: "/foo", 6 // ... 7 message: "should be integer", 8 }, 9 { 10 // generated error 11 keyword: "errorMessage", 12 message: "should not have properties other than foo", 13 // ... 14 params: { 15 errors: [{keyword: "additionalProperties" /* , ... */}], 16 }, 17 }, 18]
For keywords "required" and "dependencies" it is possible to specify different messages for different properties:
1const schema = { 2 type: "object", 3 required: ["foo", "bar"], 4 properties: { 5 foo: {type: "integer"}, 6 bar: {type: "string"}, 7 }, 8 errorMessage: { 9 type: "should be an object", // will not replace internal "type" error for the property "foo" 10 required: { 11 foo: 'should have an integer property "foo"', 12 bar: 'should have a string property "bar"', 13 }, 14 }, 15}
Replace errors for properties / items (and deeper), regardless where in schema they were created:
1const schema = { 2 type: "object", 3 required: ["foo", "bar"], 4 allOf: [ 5 { 6 properties: { 7 foo: {type: "integer", minimum: 2}, 8 bar: {type: "string", minLength: 2}, 9 }, 10 additionalProperties: false, 11 }, 12 ], 13 errorMessage: { 14 properties: { 15 foo: "data.foo should be integer >= 2", 16 bar: "data.bar should be string with length >= 2", 17 }, 18 }, 19} 20 21const validate = ajv.compile(schema) 22console.log(validate({foo: 1, bar: "a"})) // false 23console.log(validate.errors) // processed errors
Processed errors:
1[ 2 { 3 keyword: "errorMessage", 4 message: "data.foo should be integer >= 2", 5 instancePath: "/foo", 6 // ... 7 params: { 8 errors: [{keyword: "minimum" /* , ... */}], 9 }, 10 }, 11 { 12 keyword: "errorMessage", 13 message: "data.bar should be string with length >= 2", 14 instancePath: "/bar", 15 // ... 16 params: { 17 errors: [{keyword: "minLength" /* , ... */}], 18 }, 19 }, 20]
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:
1const schema = { 2 type: "object", 3 required: ["foo", "bar"], 4 allOf: [ 5 { 6 properties: { 7 foo: {type: "integer", minimum: 2}, 8 bar: {type: "string", minLength: 2}, 9 }, 10 additionalProperties: false, 11 }, 12 ], 13 errorMessage: { 14 type: "data should be an object", 15 properties: { 16 foo: "data.foo should be integer >= 2", 17 bar: "data.bar should be string with length >= 2", 18 }, 19 _: 'data should have properties "foo" and "bar" only', 20 }, 21} 22 23const validate = ajv.compile(schema) 24console.log(validate({})) // false 25console.log(validate.errors) // processed errors
Processed errors:
1[ 2 { 3 keyword: "errorMessage", 4 message: 'data should be an object with properties "foo" and "bar" only', 5 instancePath: "", 6 // ... 7 params: { 8 errors: [{keyword: "required" /* , ... */}, {keyword: "required" /* , ... */}], 9 }, 10 }, 11]
The message in property _
of errorMessage
replaces the same errors that would have been replaced if errorMessage
were a string.
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:
false
and "false"
, etc.Example:
1const schema = { 2 type: "object", 3 properties: { 4 size: { 5 type: "number", 6 minimum: 4, 7 }, 8 }, 9 errorMessage: { 10 properties: { 11 size: "size should be a number bigger or equal to 4, current value is ${/size}", 12 }, 13 }, 14}
Property names can be used in error messages with the relative JSON-pointer (e.g. 0#
).
Example:
1const schema = { 2 type: "object", 3 properties: { 4 size: { 5 type: "number", 6 }, 7 }, 8 additionalProperties: { 9 not: true, 10 errorMessage: “extra property is ${0#}” 11 } 12}
Defaults:
1{ 2 keepErrors: false, 3 singleError: false, 4}
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
.errorMessage
keyword (error messages defined for properties and items are not merged because they have different instancePaths). Multiple error messages are concatenated. Option values:
false
(default): create multiple errors, one for each messagetrue
: create single error, messages are concatenated using "; "
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.
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.
No vulnerabilities found.