fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

Feature Request: Support for Array of Functions in Custom Property

Open fernandodevelon opened this issue 2 years ago • 2 comments

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"]
    }	
};

fernandodevelon avatar Jun 21 '23 07:06 fernandodevelon

@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));


FerX avatar Jul 13 '23 09:07 FerX

Yeah, it looks good.

icebob avatar Jul 14 '23 17:07 icebob

Published

icebob avatar Jul 28 '24 10:07 icebob