mobitel-zschema-readable-error
Version:
NodeJs module for transforn errors of zSchema module to user friendly and readable string
70 lines (53 loc) • 2.08 kB
Markdown
NodeJs module for transforn errors of zSchema module to user friendly and readable string
This module writing and testing on **NodeJs v.8+** and **NPM v.5+**.
Using the module in previous versions of NodeJs does not guarantee correct works.
* [Installation](
* [Usage](
* [Testing](
* [License](
```
npm i --save mobitel-zschema-readable-error
```
[<p align="right">up to navigation</p>](
```javascript
const ZSchema = require('z-schema');
const zSchemaError = require('mobitel-zschema-readable-error');
const schema = require('./correct-json-schema.js');
const json = require('./json-for-validation.json');
// this initialization ZSchema only for example
const zSchema = new ZSchema({
noEmptyArrays: true,
noEmptyStrings: true,
noTypeless: true
});
// async
zSchema.validate(json, schema, error => {
if (error) {
console.error(zSchemaError(error)); // => Object didn't pass validation for format hostname: http://some.site in schema 'test' in property 'propStringFormat'
return false;
}
return true;
});
// sync
if (!zSchema.validate(json, schema)) {
let errors = zSchema.getLastErrors();
const readableErrors = zSchemaError(errors)
console.error(readableErrors); // => Object didn't pass validation for format hostname: http://some.site in schema 'test' in property 'propStringFormat'; Expected type array but found type boolean in schema 'test' in property 'propArray'
return false;
}
```
If module can not parsing z-schema error, then return string like:
Can not parsing z-schema error because get: false
[<p align="right">up to navigation</p>](
npm run test
[<p align="right">up to navigation</p>](
MIT License.
Copyright (c) 2017 Mobitel Ltd
[<p align="right">up to navigation</p>](