scheme-validator

0.0.4 • Public • Published

scheme-validator

Build Status Code Climate Coverage Status

Simple lib for object validation by scheme for node.js

install

npm install scheme-validation

usage

Use scheme

var schemeLib = require('scheme-validator');
var validationScheme = schemeLib(validators[method]);
var validationResult = validationScheme(<object scheme>).validate(data);

Scheme validators

var scheme = {
    name : {
        required : true, // requiredField,
        type : "string"  // type of string
    },
    age : {
        type : "number" // type of number
    },
    sex : {
        enum : [ "male", "female" ] //limited array of accepted values 
    },
    licenseAggrement : {
      value : true  // fixed value for parameter
    },
    version : {
        validator : (value) => { // custom function for user validation
          return !!value; 
        }
    },
    address : {
        structure : { // address field is an object with some structure, that recursively validated too
            city : {
                type : "string",
                required : true
            },
            street : {
                type : "string"
            },
            houseNumber : {
                type : "number"
            }
        }
    }
}

If field is not required and have no value in validated data - result will be successful!

Validation result structure

//successfull result
var validationResult = { success : true };
 
//unsuccessfull result
var validationResult = { 
    success : false,
    error : {
        key : "name", // field name from scheme with validation error,
                      // "address.city" if error in strucrute (inner object)
        text : "..."  // some error text
    }
};

Dependencies (0)

    Dev Dependencies (4)

    Package Sidebar

    Install

    npm i scheme-validator

    Weekly Downloads

    4

    Version

    0.0.4

    License

    ISC

    Last publish

    Collaborators

    • ryzii74