Skip to main content

Basic Validation

For a comprehensive list of all validations available see the available rules in the validatorjs docs.

Most simple validation rules are just a string describing the rule, however some rules are formatted differently and acheive more complex behaviour.

The general format is <rule>:<some>,<args>. The rule can be thought of like a function and everything following the colon are it's arguments, with the values for the arguments being different for each rule.

Common validations

There are a few validations that are common and will be used very frequently.

NameDescription
requiredThe field under validation must have a value, fails on empty string
dateThe field under validation must be a valid date format which is acceptable by Javascript's Date object
emailThe field under validation must be formatted as an e-mail address
stringThe field under validation must be a string.
integerThe field under validation must have an integer value
numericValidate that an attribute is numeric. The string representation of a number will pass.

Examples

import { createValidationRules } from '@rexlabs/validator';

// Object of values to be validated
const obj = {
first_name: 'Obi-wan',
address: {
galaxy: '',
sector: '55672.76835',
planet: 'Tatooine'
},
age: 74,
dob: '2 April 1914',
phone: ['555-0123', '555-9999']
};

const definitions = {
first_name: {
rules: 'required'
},
'address.galaxy': {
name: 'galaxy',
rules: 'required'
},
'address.sector': {
name: 'sector',
rules: 'numeric'
},
'address.planet': {
name: 'planet',
rules: 'required'
},
age: {
rules: ['required', 'integer']
},
dob: {
name: 'date of birth',
rules: 'required|date'
},
'phone.*': {
name: 'phone number',
rules: 'required'
}
};

const validator = createValidationRules({ definitions });
const results = validator(obj);

/*
results = {
address: {
galaxy: {
type: 'inline',
message: 'The galaxy field is required'
}
}
}
*/

Uncommon validations

While these are needed less often, some are almost certainly going to appear as the forms in the application become more complex.

NameDescription
in:value1,value2The field under validation must be included in the given list of values. The field can be an array or string.
required_if:another_field,valueThe field under validation must be present and not empty if the another_field field is equal to any value
required_with:foo,bar,...The field under validation must be present and not empty only if any of the other specified fields are present
regex:patternThe field under validation must match the given regular expression

Examples

const obj = {
firstName: 'Obi-wan',
lastName: 'Kenobi',
order: 'Jedi',
rank: 'Master',
creed: null
};

const definitions = {
firstName: {
rules: 'required_with:lastName'
},
lastName: {
rules: 'required_with:firstName'
},
order: {
rules: ['required', 'in:Jedi,Sith']
},
rank: {
rules: 'required_if:order,Jedi'
},
creed: {
rules: 'required_if:order,Sith'
},
email: {
rules:
// Probably just use the `email` rule
'regex:(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])'
}
};

const validator = createValidationRules({ definitions });
const results = validator(obj);

/*
results = {}
*/