ajv-errors
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "ajv-errors", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Custom error messages in JSON-Schema for Ajv validator", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "node node_modules/ajv/scripts/compile-dots.js node_modules/ajv/lib lib", | ||
"eslint": "eslint *.js spec", | ||
"test-spec": "mocha spec/*.spec.js -R spec", | ||
"test-cov": "nyc npm run test-spec", | ||
"test": "npm run eslint && npm run build && npm run test-cov" | ||
}, | ||
@@ -25,3 +29,27 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/epoberezkin/ajv-errors#readme" | ||
"homepage": "https://github.com/epoberezkin/ajv-errors#readme", | ||
"peerDependencies": { | ||
"ajv": ">=5.0.0" | ||
}, | ||
"devDependencies": { | ||
"ajv": "^5.0.0", | ||
"coveralls": "^2.11.16", | ||
"dot": "^1.1.1", | ||
"eslint": "^3.17.0", | ||
"glob": "^7.1.1", | ||
"js-beautify": "^1.6.12", | ||
"mocha": "^3.2.0", | ||
"nyc": "^10.1.2", | ||
"pre-commit": "^1.2.2" | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"**/spec/**", | ||
"node_modules" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text-summary" | ||
] | ||
} | ||
} |
111
README.md
# ajv-errors | ||
Custom error messages in JSON-Schema for Ajv validator | ||
[![Build Status](https://travis-ci.org/epoberezkin/ajv-errors.svg?branch=master)](https://travis-ci.org/epoberezkin/ajv-errors) | ||
[![npm version](https://badge.fury.io/js/ajv-errors.svg)](http://badge.fury.io/js/ajv-errors) | ||
[![Coverage Status](https://coveralls.io/repos/github/epoberezkin/ajv-errors/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/ajv-errors?branch=master) | ||
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv) | ||
## Install | ||
``` | ||
npm install ajv-errors | ||
``` | ||
## Usage | ||
Add the keyword `errorMessages` to Ajv instance: | ||
```javascript | ||
var Ajv = require('ajv'); | ||
var ajv = new Ajv({allErrors: true}); // option allErrors is required | ||
require('ajv-errors')(ajv); | ||
``` | ||
#### Replace all errors in the current schema and subschemas with a single message: | ||
```javascript | ||
var schema = { | ||
type: 'object', | ||
required: ['foo'], | ||
properties: { | ||
foo: { type: 'integer' } | ||
}, | ||
additionalProperties: false, | ||
errorMessage: 'should be an object with an integer property foo only' | ||
}; | ||
var validate = ajv.compile(schema); | ||
console.log(validate({foo: 'a', bar: 2})); // false | ||
console.log(validate.errors); // processed errors | ||
``` | ||
Processed errors: | ||
```javascript | ||
[ | ||
{ | ||
keyword: 'errorMessage', | ||
message: 'should be an object with an integer property foo only', | ||
// ... | ||
params: { | ||
errors: [ | ||
{ keyword: 'additionalProperties', dataPath: '' /* , ... */ }, | ||
{ keyword: 'type', dataPath: '.foo' /* , ... */ } | ||
] | ||
} | ||
} | ||
] | ||
``` | ||
#### Replace errors for certain keywords in the current schema only: | ||
```javascript | ||
var schema = { | ||
type: 'object', | ||
required: ['foo'], | ||
properties: { | ||
foo: { type: 'integer' } | ||
}, | ||
additionalProperties: false, | ||
errorMessage: { | ||
type: 'should be an object', // will not replace internal "type" error for the property "foo" | ||
required: 'should have property foo', | ||
additionalProperties: 'should not have properties other than foo' | ||
} | ||
}; | ||
var validate = ajv.compile(schema); | ||
console.log(validate({foo: 'a', bar: 2})); // false | ||
console.log(validate.errors); // processed errors | ||
``` | ||
Processed errors: | ||
```javascript | ||
[ | ||
{ | ||
// original error | ||
keyword: type, | ||
dataPath: '.foo', | ||
// ... | ||
message: 'should be integer' | ||
}, | ||
{ | ||
// generated error | ||
keyword: 'errorMessage', | ||
message: 'should not have properties other than foo', | ||
// ... | ||
params: { | ||
errors: [ | ||
{ keyword: 'additionalProperties' /* , ... */ } | ||
] | ||
}, | ||
} | ||
] | ||
``` | ||
## License | ||
[MIT](https://github.com/epoberezkin/ajv-errors/blob/master/LICENSE) |
Sorry, the diff of this file is not supported yet
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
19620
14
148
1
114
0
1
9