Custom Validator
Sometimes, certain fields require custom validations that are too specialized to generalize in a standard validator. For this purpose, a custom function may be provided in order to manually process the field’s value.
Usage
const schema = new PerfectSchema({
password: {
type: String,
custom(value, self, context) {
const field = self.getField('confirmPassword');
return !field.exists || (field.value !== value) ? 'mismatch' : undefined;
}
},
confirmPassword: {
type: String
}
});
Custom validators
The custom validator function receives three (3) arguments and is expected to return
undefined or a String, whether the field is valid or not. If the field value is
not valid, the returned value is the error message to set in the validator.
The arguments represents the value, the field’s self information, and the actual
validation context.
The field’s self information is an object providing extra functionality when validating
the value.
-
self.fieldName :
StringThe field name being validated (not canonized). -
self.options :
ObjectAn object having all the options provided to the validation method of the context. -
self.getField(fieldName) :
ObjectGet the current model’s field. Useful when validating against other fields. The return value will specify if the given fieldexistson the model, and the actualvaluefrom the model (orundefinedotherwise).The argument
fieldNamemay contain dots, which will make the function drill down into the model.
Extended schema field options
-
custom :
functionThe custom function. The field value will be validated against the specified function. The validation function should returnundefinedif the value is valid, or a string representing the error message.-
:small_blue_diamond: value :
mixedThe value to validate. -
:small_blue_diamond: self :
objectThe current model’s field information and validation options. -
:small_blue_diamond: context :
ValidationContextThe validation context.
-