Installations
npm install awesome-ajv-errors
Score
91.7
Supply Chain
99.6
Quality
75.6
Maintenance
100
Vulnerability
99.6
License
Developer
grantila
Developer Guide
Module System
ESM, UMD
Min. Node Version
^14.13.1 || >=16.0.0
Typescript Support
Yes
Node Version
18.13.0
NPM Version
8.19.3
Statistics
26 Stars
34 Commits
2 Watching
1 Branches
1 Contributors
Updated on 12 Oct 2024
Languages
TypeScript (95.57%)
JavaScript (4.43%)
Total Downloads
Cumulative downloads
Total Downloads
2,495,688
Last day
-3.6%
7,004
Compared to previous day
Last week
2.6%
36,452
Compared to previous week
Last month
4.3%
143,291
Compared to previous month
Last year
96.6%
1,377,566
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
awesome-ajv-errors pretty-prints ajv errors
It has a gorgeous human-understandable output, predicts human errors and suggests fixes.
Contents
Versions
- Since version 2;
- This is a pure ESM package. It requires Node 12.20 and cannot be used from CommonJS.
- Ajv 6, 7 and 8 are supported.
- Since version 3;
- This package can be used in browsers without special hacks. It will by default not pretty-print codeframes or use colors. If you want this, you have two options:
- Either import from
awesome-ajv-errors/node
explicitly (if you have e.g. webpack DefinePlugin configured to hack around missing things likeprocess is not defined
), or - Import from
awesome-ajv-errors/try-styled
which has a promise-basedprettify
function (prettifyTryStyled
) or a promise which will eventually resolve to a synchronous function (styledPrettify
). - Both of these will fallback to non-colored non-codeframe output if e.g. loading
@babel/code-frame
failed. This will likely be entirely resolved once Babel 8 is released; then awesome output will by default work in browsers too.
- Either import from
- This package can be used in browsers without special hacks. It will by default not pretty-print codeframes or use colors. If you want this, you have two options:
- Since version 4;
- package.json exports field is not support well by e.g. Jest, so v4 reverts the v3 exports.
- Until the exports field get better support, the official way to import will now be from:
awesome-ajv-errors
for auto-detecting node vs browserawesome-ajv-errors/dist/index-node.js
orawesome-ajv-errors/dist/index-browser.js
for explicit importing depending on environmentawesome-ajv-errors/dist/index-try-styled.js
for dynamic trying to load color support (e.g. in browsers)
- Since version 5;
- Re-introduced package exports.
- Replaced
@babel/code-frame
withawesome-code-frame
which uses later versions of upstream dependencies - works out-of-the-box in browsers now. - It will by default pretty-print codeframes and use colors, including in browsers that supports it. This will look bad in the browser window, but good in the developer tools.
- Import from
awesome-ajv-errors/plain
explicitly to have colors and code frame output disabled, or use the options toprettify()
to disable per call.
- Import from
- Now using the latest chalk, which means it requires support for package imports. If this causes problems in Jest for you, see this issue for help.
Examples
Similar property name
Suggest similar properties
JSON Schema and data
schema.json
1{ 2 "title": "Second-level two similar properties", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "object", 7 "properties": { 8 "bar": {}, 9 "bak": {} 10 }, 11 "additionalProperties": false 12 } 13 } 14}
data.json
1{ 2 "foo": { 3 "bar": "42", 4 "baz": "33" 5 } 6}
Multiple similar property names
Suggests multiple valid property names
JSON Schema and data
schema.json
1{ 2 "title": "Second-level three similar properties", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "object", 7 "properties": { 8 "bar": {}, 9 "bak": {}, 10 "bam": {} 11 }, 12 "additionalProperties": false 13 } 14 } 15}
data.json
1{ 2 "foo": { 3 "bar": "42", 4 "baz": "33" 5 } 6}
Type typo
Suggests the valid value type when mistaken
JSON Schema and data
schema.json
1{ 2 "title": "One option (number to string)", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "anyOf": [ 7 { 8 "type": "string" 9 } 10 ] 11 } 12 } 13}
data.json
1{ 2 "foo": 42 3}
Type typo (reverse)
Suggests the valid value type when mistaken
JSON Schema and data
schema.json
1{ 2 "title": "One option (string to number)", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "anyOf": [ 7 { 8 "type": "number" 9 } 10 ] 11 } 12 } 13}
data.json
1{ 2 "foo": "42" 3}
Type typo (one much better option)
When the type mismatch, and one type is much "better" than the rest (as in probably the right solution), it will be suggested for conversion
JSON Schema and data
schema.json
1{ 2 "title": "Two options", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "anyOf": [ 7 { 8 "type": "string" 9 }, 10 { 11 "type": "boolean" 12 } 13 ] 14 } 15 } 16}
data.json
1{ 2 "foo": 42 3}
Type typo (one much better option out of multiple)
JSON Schema and data
schema.json
1{ 2 "title": "Three options", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "anyOf": [ 7 { 8 "type": "string" 9 }, 10 { 11 "type": "boolean" 12 }, 13 { 14 "type": "null" 15 } 16 ] 17 } 18 } 19}
data.json
1{ 2 "foo": 42 3}
Array too small
JSON Schema and data
schema.json
1{ 2 "title": "2 too few", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "array", 7 "minItems": 3 8 } 9 } 10}
data.json
1{ 2 "foo": [ 3 1 4 ] 5}
Number too big
JSON Schema and data
schema.json
1{ 2 "title": "Less than or equal to", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "number", 7 "maximum": 17 8 } 9 } 10}
data.json
1{ 2 "foo": 42 3}
Not in enum set
JSON Schema and data
schema.json
1{ 2 "title": "One value of same type", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "enum": [ 7 41 8 ] 9 } 10 } 11}
data.json
1{ 2 "foo": 42 3}
Almost in enum set (wrong convertible type)
JSON Schema and data
schema.json
1{ 2 "title": "Two options (one of different type)", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "enum": [ 7 41, 8 "42" 9 ] 10 } 11 } 12}
data.json
1{ 2 "foo": 42 3}
Almost in enum set (wrong convertible type), multiple options
JSON Schema and data
schema.json
1{ 2 "title": "Four options (one of different type)", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "enum": [ 7 "falso", 8 "other", 9 "False", 10 false 11 ] 12 } 13 } 14}
data.json
1{ 2 "foo": "false" 3}
Invalid format (time)
JSON Schema and data
schema.json
1{ 2 "title": "time invalid", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "string", 7 "format": "time" 8 } 9 } 10}
data.json
1{ 2 "foo": "11:22:334" 3}
Invalid format (e-mail)
JSON Schema and data
schema.json
1{ 2 "title": "email invalid", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "string", 7 "format": "email" 8 } 9 } 10}
data.json
1{ 2 "foo": "quite@invalid@email.com" 3}
if-then not satisfied
JSON Schema and data
schema.json
1{ 2 "title": "if-then on first-level object", 3 "properties": { 4 "foo": { 5 "if": { 6 "properties": { 7 "firstName": { 8 "const": true 9 } 10 } 11 }, 12 "then": { 13 "required": [ 14 "lastName" 15 ] 16 } 17 } 18 } 19}
data.json
1{ 2 "foo": { 3 "firstName": true 4 } 5}
Multiple of
JSON Schema and data
schema.json
1{ 2 "title": "Multiple of", 3 "type": "object", 4 "properties": { 5 "foo": { 6 "type": "number", 7 "multipleOf": 4 8 } 9 } 10}
data.json
1{ 2 "foo": 17 3}
Required property
JSON Schema and data
schema.json
1{ 2 "title": "Root-level required", 3 "type": "object", 4 "properties": { 5 "foo": {} 6 }, 7 "required": [ 8 "foo" 9 ] 10}
data.json
1{ 2 "bar": 42 3}
Usage
Import the ajv
package, and prettify
from awesome-ajv-errors
:
1import * as Ajv from 'ajv' 2import { prettify } from 'awesome-ajv-errors'
Create an ajv instance and validate objects:
1const ajv = new Ajv( { allErrors: true } ); // allErrors is optional 2 3let data, schema; // Get the JSON schema and the JSON data from somewhere 4 5const validate = ajv.compile( schema ); 6validate( data );
Now, the validation error is stored on the validate
function. Use prettify
to pretty-print the errors, and provide the data so that awesome-ajv-errors can suggest fixes:
1console.log( prettify( validate, { data } ) );
Configure styling
Instead of auto-detecting based on the platform (Node.js or a browser), you can turn on/off colors, location printing (the json-snippet of the error) and whether to print big ascii numbers to the left of each error, if there are more than one error.
With the options object containing data
provided to prettify
you can include colors
, location
and bigNumbers
as booleans, to override the defaults.
Turning colors explicitly on will only enable colors if it's detected to be supported by the platform, but turning them off will always output non-colored text.
Turning location on will also only enable the location printing if colors are detected to be supported by the underlying platform (this is a limitation in the current @babel/code-frame
and will likely be resolved in Babel 8).
bigNumbers
will only be enabled if location printing is enabled, but can be explicitly turned off.
Example:
1const colors = false; 2const location = false; 3const explanation = prettify( validate, { data, colors, location } );
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/27 approved changesets -- score normalized to 0
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
- Warn: no topLevel permission defined: .github/workflows/branches.yml:1
- Warn: no topLevel permission defined: .github/workflows/master.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:47: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:49: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/master.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/master.yml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-ajv-errors/master.yml/master?enable=pin
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Reason
12 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.5
/10
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