-
-
Notifications
You must be signed in to change notification settings - Fork 616
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
Allow simple extension of custom validators/sanitizers for better readability and reusability #1077
Labels
Milestone
Comments
I completely agree with you about this feature requirement, it is in the v7 roadmap right now, we just need to implement a solution😄 |
@fedeci great 😄 is the road map publicly visible somewhere? |
Yes #990 |
ysfaran
added a commit
to ysfaran/express-validator
that referenced
this issue
Aug 11, 2021
2 tasks
ysfaran
added a commit
to ysfaran/express-validator
that referenced
this issue
Aug 14, 2021
2 tasks
This is now possible in v7.0.0! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is your feature request related to a problem? Please describe.
When I add a custom validator, which I need to reuse, I usually create a function like this:
Usage:
I use the snippet
custom(isToken)
above for a lot of validations and also reuse it for different middlewars likeparam
instead ofbody
. This unfortunatley breaks the cleanness of the fluent api and makes it harder to read. You also need to importisToken
everywhere you use it. ForcustomSanitizer
it's quite similar.Describe the solution you'd like
express-validator
should allow to extend it's validators/sanitizers in an easier way, similiar to howjest
does it withexpect.extend
.A good solution would be to export new functions from the library:
which will allow to add custom validators and sanitizers:
Usage afterwards:
With this solution the API would look much cleaner and it would simplify reusability in contrast to
custom
orcustomSanitizer
.Implementation Idea
The feature could be implemented by extending the
prototype
ofexpress-validator/src/chain/validators-impl.ts
Line 8 in 690cd63
It should also be supported to pass options to custom validators, e.g.:
(I just had a rough look into to the code and there might be better ways to implement it. Feel free to suggest more appropriate solutions.)
The text was updated successfully, but these errors were encountered: