fastest-validator
fastest-validator copied to clipboard
Feature Request: Support for Array of Functions in Custom Property
I suggest adding support for an array of functions in the custom property of Fastest Validator.
This enhancement would enable users to apply multiple filters efficiently, simplifying complex validation scenarios and enhancing code readability.
example:
const schema = {
name: { type: "string", min: 3, max: 255 },
phone: { type: "string", length: 15, custom: ["noSpace","isInternational","otherCheck"]
}
};
@icebob I am working on a pull request that can execute code like this, before implementing the tests I wanted to know if you like it
let Validator = require("../index");
let v = new Validator({
debug: true,
useNewCustomCheckerFunction: true,
messages: {
// Register our new error message text
evenNumber: "The '{field}' field must be an even number! Actual: {actual}",
realNumber: "The '{field}' field must be a real number! Actual: {actual}",
notPermitNumber: "The '{field}' cannot have the value {actual}",
},
customFunctions:{
even: (value, errors)=>{
if(value % 2 != 0 ){
errors.push({ type: "evenNumber", actual: value });
}
return value;
},
real: (value, errors)=>{
if(value <0 ){
errors.push({ type: "realNumber", actual: value });
}
return value;
}
}
});
const schema = {
people:{
type: "number",
custom: [
"even",
"real",
function (value, errors){
if(value === "3" ){
errors.push({ type: "notPermitNumber", actual: value });
}
return value;
}
]
}
};
console.log(v.validate({people:5}, schema));
console.log(v.validate({people:-5}, schema));
console.log(v.validate({people:3}, schema));
Yeah, it looks good.
Published