mobitel-zschema-readable-error
v1.0.2
Published
NodeJs module for transforn errors of zSchema module to user friendly and readable string
Downloads
1,488
Maintainers
Readme
Mobitel Ltd. zSchema readable error
NodeJs module for transforn errors of zSchema module to user friendly and readable string
Attention
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.
Navigation
Installation
npm i --save mobitel-zschema-readable-error
Usage
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
Test
npm run test
License
MIT License. Copyright (c) 2017 Mobitel Ltd up to navigation