README
Description
This module seeks to extend the package of validate.js with additional feature extensions according to our needs.
Array extension
You can validate arrays of type string, number and object. If you validate just an array of strings or number a internal temporary object assinment is used for validation.
NOTE! You cannot mix an objects with strings or numbers in an array!
Validating an array of objects
const constraints = {
purchases: {
array: {
qty: {
presence:true,
numericality: {
onlyInteger: true,
greaterThan: 0,
lessThanOrEqualTo: 5
}
}
goods:{
type:'string'
}
}
}
soldItems:
type:'string'
}
const params = {
purchases: [{ qty:1, goods: 'eggs' }, { qty:2, goods: 'apples' }]
soldItems: 'nothing'
}
const notValid = validate(params, constraints) // will pass
if (notValid) {
throw new Error(JSON.stringify(notValid))
}
Validating an array of numbers or strings
const constraints = {
purchases: {
array: {
numericality: {
onlyInteger: true,
greaterThan: 0,
lessThanOrEqualTo: 5
}
}
}
}
const params = {
purchases: [1,-11]
}
const notValid = validate(params, constraints) // will fail because -11 < 0
if (notValid) {
throw new Error(JSON.stringify(notValid))
}