Custom Validation Rules
The @rexlabs/validator library exports the original Validator constructor from the validatorjs library, allowing you to add your own custom validation rules that can be used within the configuration passed to the createValidationRules function.
These rules can be shared across many forms and must be added before the createValidationRules function is run, so it's best to abstract them all out into their own initialisation file and execute it at the entrypoint of the application.
Read more about custom validation rules in the validatorjs docs
import Validator, { createValidationRules } from '@rexlabs/validator';
Validator.register(
'rule_name',
function (val) {
// return truthy to pass validation
return val < 5;
},
'Message to return if validation fails :attribute'
);
// You can also create a validator that will run every change
// without the field being required
// This is specifically used to create custom required validation rules
Validator.registerImplicit(
'rule_name',
function (val) {
// return truthy to pass validation
return val && val === 'set';
},
'Message to return if validation fails :attribute'
);