0.0.4 • Published 8 years ago
scheme-validator v0.0.4
scheme-validator
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
}
};