Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support for Array of Functions in Custom Property #328

Open
fernandodevelon opened this issue Jun 21, 2023 · 2 comments
Open

Comments

@fernandodevelon
Copy link

fernandodevelon commented Jun 21, 2023

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]
    }	
};
@FerX
Copy link

FerX commented Jul 13, 2023

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

@icebob
Copy link
Owner

icebob commented Jul 14, 2023

Yeah, it looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants