web-obj-validator
v1.1.1
Published
Validator for javascript object (Web version)
Downloads
7
Maintainers
Readme
web-obj-validator
Validator for javascript object (Web version)
What's New
v1.1.0
- Add new rules: email, logic
Install
npm install web-obj-validator
Include
<script src="node_modules/web-module/web-module.js"/>
<script src="node_modules/web-obj-validator/web-obj-validator.js"/>
Usage
const Validator = module.require('web-obj-validator');
let validator = new Validator(validation_rule);
let rs = validator.validate(obj, (errors)=>{
// Do something on error
}, ()=>{
// Do something on ok
});
Example
let validator = new Validator({
required: true,
typeof : 'object',
child : {
name : {
required: true,
typeof : 'string',
length : {
max : 32
message: 'Name must be less than or equal 32 characters'
}
},
age : {
required: true,
typeof : 'number',
value : {
min : 1,
max : 100,
message : {
min : 'Age must be greater than or equal 1',
max : 'Age must be less then or equal 100'
}
}
}
}
});
let rs = validator.validate({
name: 'John',
age : 32
});
Methods
.validate(obj, onErrorCallback, onOkCallback)
Parameters:
obj : object Object to be validated
onErrorCallback : function (optional) function(errors)
errors : Array
[ { id: obj.subobj , value: object value , message: error message } , ... ]
onOkCallback : function (optional) function()
return : boolean true if validation is ok
Rules
required
Validate if the field value is not values listed as below:
- undefined
- null
- empty array ( [ ] )
- empty string ( '' )
{
required: true // or false
}
{
required: 'Error message' // Equivalent to { required: true } with custom error message
}
{
required: {
message: 'Error message' // Custom message
}
}
typeof
Validate the data type of field
{
typeof: 'some type' // undefined | string | number | object | function
}
{
typeof: [ 'type 1', 'type 2' ]
}
{
typeof: {
types: 'some type'
}
}
{
typeof: {
types: [ 'type 1', 'type 2' ]
}
}
{
typeof: {
types: 'some type',
message: 'Error message' // Custom error message
}
}
length
Validate string length
{
length: 8 // or '8'. Fixed length (8 characters)
}
{
length: {
length: 8 // or '8'. Fixed length (8 characters)
}
}
{
length: {
min: 8 // or '8'. Mininum length (8 characters)
}
}
{
length: {
max: 8 // or '8'. Maxinum length (8 characters)
}
}
{
length: {
length: 8,
message: 'Error message' // Custom error message (apply for all fixed length, minimum length, maximum length error)
}
}
{
length: {
length: 8,
message: {
length: 'Error message' // Custom error message (apply for fixed length error)
}
}
}
{
length: {
min: 8,
max: 16,
message: {
min: 'Error message 1', // Custom error message (apply for minimun length error)
max: 'Error message 2' // Custom error message (apply for maxinum length error)
}
}
}
value
Validate the value of string or number
{
value: 'string' // or number. Fixed value
}
{
value: [ value_1, value_2, ... ]
}
{
value: {
min: 'string', // or number. Minimum value
}
}
{
value: {
max: 'string', // or number. Maximum value
}
}
{
value: {
value: 'string', // or number. Fixed value
}
}
{
value: {
min: 18,
max: 100,
message: 'Error message' // Custom error message (apply for all fixed value, mininum value, maxinum value)
}
}
{
value: {
value: [1, 2, 3],
message: {
value: 'Error message' // Custom error message (apply for fixed value)
}
}
}
{
value: {
min: 50,
max: 100,
message: {
min: 'Error message 1', // Custom error message (apply for minimum value)
max: 'Error message 2' // Custom error message (apply for maxinum value)
}
}
}
match
Validate if the string matchs specified pattern
{
match: 'pattern' // String
}
{
match: /^\d+$/ // or new RegExp('pattern'). Regular expression object
}
{
match: {
pattern: 'pattern' // String
}
}
{
match: {
pattern: new RegExp('^\\d+$'),
message: 'Error message' // Custom error message
}
}
Validate the email address format. The email address format is validated by the regular expression below (from http://emailregex.com):
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
{
email: true
}
{
email: 'Error message' // Custom error message
}
{
email: {
message: 'Error message' // Custom error message
}
}
logic
Validate the custom logic
{
logic: function(value){ // Some logic. Return true | false }
}
{
logic: {
logic: function(value){ // Some logic. Return true | false },
message: 'Error message' // Custom error message
}
}
{
logic: {
logic: function(value){ // Some logic. Return true | false },
context: obj, // Custom context
message: 'Error message' // Custom error message
}
}
child
Validate specified chillren fields
{
child: {
field_1: {
... // rules for field_1
},
field_2: {
... // rules for field_2
},
...
}
}
children
Validate all children fields
{
children: {
... // rules for all children fields
}
}
Callbacks
on_ok
let validator = new Validator({
required: true,
typeof : 'object',
child : {
name : {
required: {
message: 'Name is required',
on_ok : (id, value)=>{
console.log(id); // name
console.log(value); // undefined
}
},
length: {
max: 32
}
}
}
});
validator.validate({
name: 'John'
});
on_error
let validator = new Validator({
required: true,
typeof : 'object',
child : {
name : {
required: {
message: 'Name is required',
on_error : (id, value, message)=>{
console.log(id); // name
console.log(value); // undefined
console.log(message); // Name is required
}
},
length: {
max: 32
}
}
}
});
validator.validate({
name: undefined
});
Bugs or feature requests
Please contact to email