-
Notifications
You must be signed in to change notification settings - Fork 1
Schema definition
- Validating primitive data (strings & numbers)
- Validating Hash table (Object) structures
- Validating Arrays
- Defining required data
- Error messages TODO
- Conditional data validation TODO
Please look at the full list of Assertions and their options.
var inspector = require('json-inspector');
//validator definition
var validator = inspector.define('stirng-validator', {
$is: String,
$hasLengthOf: {min: 1, max: 255}
});
validator.validate('this is valid string');
console.log(validator.success); //true
validator.validate('');
console.log(validator.success); //false
console.log(validator.error); //ValidationError
//validator definition
var validator = inspector.define('user', {
username: {
$isAlphanumeric: 'en-US',
$hasLengthOf: {min:1, max: 32}
},
email: {
$isEmail: null //if you dont want to pass options object to $isEmail assertion, you can provide null value (even `undefined` would work). In the case, default options will be applied
},
address: {
country_code: {
$isAlpha: 'en-US'
$hasLengthOf: {min: 2, max: 2}
},
street: {
$is: String,
$hasLengthOf: {max: 125}
},
zip: {
$isInt: null, //again, null value means that no options for the $isInt assertion are specified. Default options will be applied
}
}
});
validator.validate({
username: 'happie',
email: 'test@test.com',
address: {
country: 'US',
street: 'Second',
zip: 96701
}
});
console.log(validator.success); //true
//validator definition
var validator = inspector.define('cart', {
items: {
$forEach: { // each item of the array must pass following validation rules
id: {
$isInt: {min:1}
},
name: {
$is: String
}
},
$hasLengthOf: {max: 5} // `items` collection can have maximum of 5 items, no more
}
});
validator.validate({
items: [
{
id: 1,
name: 'Supper-looking T-shirt'
},
{
id: 29,
name: 'Supper-looking Jumper'
}
]
});
console.log(validator.success); //true
By default all properties are optional. That means if you don't provide any data, the validation process will pass.
You can specify which properties are required in validator schema definition or by setting all properties to required
by default (see Validator options)
null
values are treated as any other value by default - they are not threated as "empty" value by default. The means if a property can have null
value, you can specify that either by [OR condition] or by explicitly telling a validator that null
values should be treated as "empty" value - as data would not be sent at all.
//validator definition
var validator = inspector.define('user', {
username: {
$isAlphanumeric: 'en-US',
$hasLengthOf: {min:1, max: 32}
},
email: {
$isEmail: {allow_display_name: true}
}
});
validator.validate();
console.log(validator.success); //true - because `username` and `email` properties are not required by default neither is the data object value the `username`, `email` properties are at.
validator.validate({});
console.log(validator.success); //true - because `username` and `email` properties are not required by default
To specify required
properties use $required: true
option:
//validator definition
var validator = inspector.define('user', {
$required: true, // all properties within the validated object are set to required (they inherit this special `$required` option)
username: {
$isAlphanumeric: 'en-US',
$hasLengthOf: {min:1, max: 32}
},
email: {
$isEmail: {allow_display_name: true}
}
});
validator.validate({});
console.log(validator.success); //false
The above example of specifying one $required
option that other properties inherit is same as defining $required
option individualy.
This is equivalent schema definition:
{
username: {
$required: true, // defining required properties individualy
$isAlphanumeric: 'en-US',
$hasLengthOf: {min:1, max: 32}
},
email: {
$required: true,
$isEmail: {allow_display_name: true}
}
}
To futher explain inheritance concept, look at the following example of schema definition:
{
$required: true,
username: {
$required: false, // overwrites inherited `$required: true` option
$isAlphanumeric: 'en-US',
$hasLengthOf: {min:1, max: 32}
},
email: {//email property has inherited the `$required: true` option
$isEmail: {allow_display_name: true}
}
}
Note: The same inheritance concept applies for the error messages definitions (the $message
option).