mobitel-zschema-readable-error

NodeJs module for transforn errors of zSchema module to user friendly and readable string

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
mobitel-zschema-readable-error
101.0.27 years ago7 years agoMinified + gzip package size for mobitel-zschema-readable-error in KB

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

up to navigation

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

up to navigation

Test

npm run test

up to navigation

License

MIT License. Copyright (c) 2017 Mobitel Ltd

up to navigation