A plugin that provides a bunch of validators for moko.
var Person = moko('Person').attr('name', { required: true }),
validation = require('moko-validators');
Person.use(validation);
Verifies that a field is present.
User.attr('username', {required: true});
Verifies that a field equals another field.
User.attr('password')
.attr('passwordConfirmation', { confirms: 'password' });
Checks that a field is of a given type
User.attr('name', {type: 'string'});
In addition to string support for primitives, you can also pass in a constructor.
User.attr('parent', { type: User });
// Or
User.attr('name', { type: String });
Verify the value of a field against a regex pattern. moko-validators
comes with a few regex strings built in under the formatStrings
object.
Validates the field against the given regular expression
User.attr('name', {format: /\w+ \w+/ });
Validates the field against a (North American) phone number format
User.attr('phone', {format: 'phone' });
Validates the field against a email address format
User.attr('email', {format: 'email' });
Validates the field against a URL format
User.attr('website', {format: 'url' });
Validates the field against a credit card format.
User.attr('cc', {format: 'creditcard' });