A data validator with an idiomatic API
var discriminate = require('discriminate');
var { Required, Custom, Maybe, And, Or } = discriminate;
const Email = discriminate(discriminate.And(
String,
Custom(([path, name], value, callback) =>
value.match(/^.+@[^.].*?\..*[^.]$/) ? callbacl(null, value) : callback(`${path[1]} must be an email`)
)
));
const User = discriminate({
firstName: String,
surname: String,
email: Maybe(Email),
nickname: Maybe(String, 'Buddy'),
age: Number
});
function greetUser(maybeNotAValidUser){
User(maybeNotAValidUser, function(error, user){
if(error){
/*
error will look like:
{
message: ...
errors: [
{ path: 'path.in.schema', message: ... }
]
}
*/
return;
}
// user will be valid at this point.
});
}
value type constructors are valid, eg:
String
, Number
, Boolean
Extra types provided by discriminate:
Ensure a value is either Type or null/undefined
Ensures a value is every one of Types
.
Ensures a value is any one of Types
.
Ensures fn(value)
does not throw.
The value returned from fn
will be the result of the type instansiation.
Prints a nicer error if a value is null or undefined, eg: 'age is required.'
Allow any value
Ensures a value is an array where each item matches Type
.
Casts a value to a BaseType
.