@segment/ajv-human-errors
Advanced tools
Comparing version 1.6.0 to 2.0.0-canary.56
{ | ||
"name": "@segment/ajv-human-errors", | ||
"version": "1.6.0", | ||
"version": "2.0.0-canary.56+95f7c933", | ||
"description": "Human-readable error messages for Ajv (Another JSON Schema Validator).", | ||
@@ -10,11 +10,28 @@ "repository": { | ||
}, | ||
"main": "index.js", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/cjs/index.d.ts", | ||
"scripts": { | ||
"test": "jest" | ||
"prepack": "yarn build", | ||
"prebuild": "yarn clean", | ||
"build": "yarn build:cjs && yarn build:esm", | ||
"build:cjs": "tsc -p tsconfig.build.json -m commonjs --outDir dist/cjs", | ||
"build:esm": "tsc -p tsconfig.build.json -m es2015 --outDir dist/esm", | ||
"clean": "tsc --build tsconfig.build.json --clean", | ||
"postclean": "rm -rf dist", | ||
"prepublishOnly": "yarn build", | ||
"test": "jest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"ajv": "^6.12.3" | ||
"ajv": "^8.6.3", | ||
"ajv-formats": "^2.1.1", | ||
"jest": "^26.6.3", | ||
"typescript": "^4.2.3" | ||
}, | ||
"gitHead": "6a3532ba6c061cd6e57d0f309857074f310cdb80" | ||
"peerDependencies": { | ||
"ajv": "^8.0.0" | ||
}, | ||
"gitHead": "95f7c933bf79ef5eddcfb94ad4eba8ffb1f07d83" | ||
} |
137
README.md
@@ -5,20 +5,12 @@ # @segment/ajv-human-errors | ||
By default, Ajv error messages leave a little bit to be desired. ajv-human-errors provides an | ||
aggregate Error object that holds Ajv validation errors that are more easily readable by humans. For | ||
example, ajv-human-errors changes "should NOT have additional properties" to "The root value has an | ||
unexpected property, c, which is not in the list of allowed properties (a, d)." | ||
By default, Ajv error messages leave a little bit to be desired. ajv-human-errors provides an aggregate Error object that holds Ajv validation errors that are more easily readable by humans. For example, ajv-human-errors changes "must NOT have additional properties" to "The root value has an unexpected property, c, which is not in the list of allowed properties (a, d)." | ||
You can also override the error message entirely using the "errorMessage" schema keyword, like you | ||
can with [ajv-errors](https://github.com/ajv-validator/ajv-errors) (see ["Schema | ||
Options"](#schema-options)). | ||
You can also override the error message entirely using the "errorMessage" schema keyword, like you can with [ajv-errors](https://github.com/ajv-validator/ajv-errors) (see ["Schema Options"](#schema-options)). | ||
The following Ajv options must be set to `true` for `ajv-human-errors` to work with the errors returned by | ||
Ajv: | ||
The following Ajv options must be set to `true` for `ajv-human-errors` to work with the errors returned by Ajv: | ||
- `allErrors` | ||
- `verbose` | ||
- `jsonPointers` | ||
The following features of JSON Schema are not yet implemented (but will return their "raw" Ajv error | ||
messages): | ||
The following features of JSON Schema are not yet implemented (but will return their "raw" Ajv error messages): | ||
@@ -35,3 +27,9 @@ - patternProperties | ||
```console | ||
$ yarn add @segment/ajv-human-error | ||
``` | ||
or | ||
```console | ||
$ npm install @segment/ajv-human-error | ||
@@ -42,5 +40,5 @@ ``` | ||
```js | ||
const Ajv = require('ajv') | ||
const { AggregateAjvError } = require('@segment/ajv-human-errors') | ||
```ts | ||
import Ajv from 'ajv'; | ||
import { AggregateAjvError } from '@segment/ajv-human-errors'; | ||
@@ -50,11 +48,10 @@ const ajv = new Ajv({ | ||
verbose: true, | ||
jsonPointers: true | ||
}) | ||
}); | ||
ajv.validate({ title: 'Bag of Bytes', type: 'string' }, 1234) | ||
ajv.validate({ title: 'Bag of Bytes', type: 'string' }, 1234); | ||
const err = new AggregateAjvError(ajv.errors) | ||
console.log(err.message) | ||
const errors = new AggregateAjvError(ajv.errors); | ||
console.log(errors.message); | ||
// 'Bag of Bytes should be a string but it was a number.' | ||
console.log([...err].map((e) => e.message)) | ||
console.log(errors.map(({ message }) => message)); | ||
// ['Bag of Bytes should be a string but it was a number.'] | ||
@@ -81,12 +78,14 @@ ``` | ||
```js | ||
const { AggregateAjvError } = require('@segment/ajv-human-errors') | ||
```ts | ||
import { AggregateAjvError } from '@segment/ajv-human-errors'; | ||
const err = new AggregateAjvError(ajv.errors) | ||
const errors = new AggregateAjvError(ajv.errors); | ||
const messages = [...err].map((e) => e.message) | ||
const messages = errors.map(({ message }) => message); | ||
// or | ||
const messages = [] | ||
for (const e of err) { | ||
messages.push(e.message) | ||
const messages = []; | ||
for (const error of errors) { | ||
messages.push(error.message); | ||
} | ||
@@ -105,22 +104,26 @@ ``` | ||
- `data`: (Only if `includeData` option is set) Value that value that failed validation. Useful for | ||
showing users what, exactly, was wrong without embedding entire values in the error message. | ||
- `data`: (Only if `includeData` option is set) Value that value that failed validation. Useful for showing users what, exactly, was wrong without embedding entire values in the error message. | ||
These fields are also available in the JSON form: | ||
```js | ||
const { AggregateAjvError } = require('@segment/ajv-human-errors') | ||
```ts | ||
const { AggregateAjvError } = require('@segment/ajv-human-errors'); | ||
const err = new AggregateAjvError(ajv.errors) | ||
const errors = new AggregateAjvError(ajv.errors); | ||
console.log([...err][0].toJSON()) | ||
// { | ||
// message: 'The value at /arr should be unique but elements 1 and 4 are the same.', | ||
// path: '$.arr', | ||
// pointer: '/arr', | ||
// original: { ... }, | ||
// data: [0, 1, 2, 0, 1] | ||
// } | ||
console.log(errors[0].toJSON()); | ||
``` | ||
which will log this: | ||
```ts | ||
{ | ||
message: 'The value at /arr should be unique but elements 1 and 4 are the same.', | ||
path: '$.arr', | ||
pointer: '/arr', | ||
original: { ... }, | ||
data: [0, 1, 2, 0, 1] | ||
} | ||
``` | ||
# Options | ||
@@ -144,23 +147,28 @@ | ||
{ | ||
"title": "Bag of values", | ||
"type": "object" | ||
} | ||
```json | ||
{ | ||
"title": "Bag of values", | ||
"type": "object" | ||
} | ||
``` | ||
Then the resulting error message would look like: "Bag of values should be an object but it | ||
was an array." | ||
Then the resulting error message would look like: "Bag of values should be an object but it was an array." | ||
- `includeOriginalError` (default: false) Include the original Ajv error object on the `data` | ||
property of each error in the `AggregateAjvError` instance: | ||
- `includeOriginalError` (default: false) Include the original Ajv error object on the `data` property of each error in the `AggregateAjvError` instance: | ||
```ts | ||
const errors = new AggregateAjvError(ajv.errors, { includeOriginalError: true }); | ||
errors.forEach(({ original }) => console.log(original)); | ||
``` | ||
const err = new AggregateAjvError(ajv.errors, { includeOriginalError: true }) | ||
[...err].forEach(e => console.log(e.original)) | ||
// { | ||
// params: { ... }, | ||
// parentSchema: { ... }, | ||
// schema: '...', | ||
// schemaPath: '...', | ||
// ... | ||
// } | ||
output: | ||
```ts | ||
{ | ||
params: { ... }, | ||
parentSchema: { ... }, | ||
schema: '...', | ||
schemaPath: '...', | ||
... | ||
} | ||
``` | ||
@@ -171,6 +179,11 @@ | ||
```ts | ||
const errors = new AggregateAjvError(ajv.errors, { includeOriginalError: true }); | ||
errors.forEach(({ data }) => console.log(data)); | ||
``` | ||
const err = new AggregateAjvError(ajv.errors, { includeOriginalError: true }) | ||
[...err].forEach(e => console.log(e.data)) | ||
// 'foobar' | ||
output: | ||
```ts | ||
'foobar' | ||
``` | ||
@@ -192,3 +205,3 @@ | ||
```js | ||
```ts | ||
'The root value should be a bag of bytes.' | ||
@@ -195,0 +208,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124191
43
1629
228
1
4
2
2