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
Use a Function to generate validators for each field #1235
Labels
Comments
Hi, On another note, you can make use of const stringValidate = (source, text, optional) => {
const validateFn = buildCheckFunction([source]);
const chain = validateFn(text);
if (optional) {
chain.optional();
} else {
chain.notEmpty().withMessage(`${text} required`).bail();
}
return chain
.isString()
.withMessage(`${text} must be String`)
.trim();
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, So I am working in a large project and I'm tired of writing the same validator code everytime so i created a few functions which will return the validation logic when i call them and i can re-use them wherever i want.
For example, suppose this is my function :
This function will return a validation logic for any string based validation. The function takes 3 arguments :
And this function I am trying to use in a route call like this
But the above code gives me an error
So My question is if its possible what I am trying to do? Or do i have to look for a different method
The text was updated successfully, but these errors were encountered: