-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Enhancement Suggestion - Text filters or sanitization rules #14467
Comments
Thanks for getting in touch @kamranahmedse. Ping @taylorotwell. |
I discussed a similar proposal with @taylorotwell yesterday. My specific example was in a My proposal looks like the following (inside a public function filters()
{
return [
'pre' => [
'first_name' => ['trim', 'strip_spaces'],
'reference_number' => ['trim'],
],
'post' => [
'reference_number' => ['uppercase'],
]
];
} In the above example, before validation, After validation passes, the data available on the One thing to mention is whether or not the For example, this might look like the following: public function filters()
{
return [
'pre' => [
'first_name' => ['trim', 'uppercase'],
],
'post' => [
'first_name' => ['trim', 'titlecase'],
]
];
}
// User enters: 'mc dougall '
// Validated value is: 'MC DOUGALL'
// Returned value after validation is: 'Mc Dougall' I don't mind the repetition of the rules in both the I think this separation of these filters makes it obvious what they do, and separates them out from the |
Hmm... I like the general idea of filters. However, I don't believe validation should modify the output. Now, the |
I like the idea of a laravel-y way of adding sanitizers (probably integrated into Request objects), but I don't think they belong in validators. Sanitizing and validating are different concepts and should be kept apart. I agree with having the separate Arrays should probably be an acceptable input, so we'd need to have the
From a UX perspective, I'm a heavy proponent of sanitizing + sensible defaults over validation, so I'd love to see this pass. |
Agreed with @phroggyy and @rizqidjamaluddin. Validation and sanitation are separate concepts and should have separate APIs. But having a sanitation option for input would be quite awesome (especially if you can extend it like you can the validators so you could write your own re-usable sanitizers). |
I agree, too... I didn't know if there was an API parity requirement between the Validator and the I think this is an ideal opportunity to apply filters to data - but I would like to see it 'baked in' to the |
Funny enough, I'm not usually one to enjoy shortcuts (and I certainly am not a fan of pseudo-magic strings) - but I think a laravel-style sanitizer fits right into many workflows and I do find myself doing a lot of One thing to carefully note is that this practice mutates data before it gets validated, which means validation error messages may not match the provided input from the user. |
@rizqidjamaluddin ^^What I had in my mind was passing the request object. And then it will automatically mutate the data in the request object. But yeah, it could be made as to mutate both ways i.e. if the passed argument is a request object, items in request will be mutated and set in the request. Or if the passed parameter is an array, the given array will be mutated and returned. (Updated the original description) |
I think binding sanitizing to the request mechanism is too far interlocked - it's not uncommon to want to sanitize other data, like incoming information from an API request, parsing an inbound email webhook or just people who still do |
@rizqidjamaluddin What do you think about using a middleware in case of filtering? |
At first view, middleware might be a thing. |
Hey everyone, Anyone willing to make a PR for this? |
@themsaid I'll look into it this weekend! |
@phroggyy I'm going to close this issue since it's a feature request, but looking forward to your PR when you have time :) |
@themsaid yep, started on it this morning!
|
Any plans on adding text filters or a similar way of sanitization?
Sometimes you repeatedly have to do some same kind of sanitization or formatting on the received input. For example, trimming the input, making it lowercase etc.
What if we enhance the validator to accept filters to apply i.e. something like
Or we may implement something different altogether.
A different service may be e.g. if we pass it the request object, it will mutate the data in the request itself.
If array is passed, items will be mutated and returned i.e.
I am in the last phases of releasing a Laravel package for this (using second method), thought I should ask if I may create a PR with this.
The text was updated successfully, but these errors were encountered: