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
Custom error messages in JSON Schemas for Ajv validator
npm install ajv-errors
98
Supply Chain
100
Quality
75.6
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
285 Stars
99 Commits
18 Forks
6 Watching
9 Branches
5 Contributors
Updated on 16 Nov 2024
Minified
Minified + Gzipped
TypeScript (97.27%)
JavaScript (2.73%)
Cumulative downloads
Total Downloads
Last day
-7.6%
1,318,886
Compared to previous day
Last week
2%
7,120,855
Compared to previous week
Last month
19.5%
28,843,417
Compared to previous month
Last year
-27.5%
326,999,814
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.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 2/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More