Advanced Object Validation
import validate, {
objectProps, required, length, range, every
} from 'strickland';
// Define the rules for first name, last name, and birthYear
const personPropsValidator = objectProps({
firstName: every([
required(),
length(2, 20)
]),
lastName: every([
required(),
length(2, 20)
]),
birthYear: range(1900, 2018)
});
function stanfordStricklandBornIn1925(person) {
if (!person) {
// If there's no person provided, return valid and
// rely on `required` to ensure a person exists
return true;
}
const {firstName, lastName} = person;
if (firstName === 'Stanford' && lastName === 'Strickland') {
return (person.birthYear === 1925);
}
return true;
}
const personValidator = every([
required(),
personPropsValidator,
stanfordStricklandBornIn1925
]);
// Create a person
const person = {
firstName: 'Stanford',
lastName: 'Strickland',
birthYear: 1925
};
const result = validate(personValidator, person);Last updated