Socket
Socket
Sign inDemoInstall

@segment/ajv-human-errors

Package Overview
Dependencies
Maintainers
206
Versions
238
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@segment/ajv-human-errors - npm Package Compare versions

Comparing version 1.6.0 to 2.0.0-canary.56

dist/cjs/aggregate-ajv-error.d.ts

27

package.json
{
"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 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc