php-crud-api icon indicating copy to clipboard operation
php-crud-api copied to clipboard

Mandatory / required filter in requests

Open bastians opened this issue 5 years ago • 10 comments

Is it possible to mark a filter as required that the API throws an error when the filter is used but no parameter is given in the request?

bastians avatar Jun 23 '20 09:06 bastians

Thank you for creating this issue.

The answer is "No" as this is not (yet) implemented. You may implement such behavior in a 'beforeHandler' of the customization middleware. You could also make a new middleware for this functionality.

Maybe you can explain a bit more about your use-case and motivation? It could even be a candidate for core functionality if it seems essential.

mevdschee avatar Jun 23 '20 09:06 mevdschee

Wow, this answer was quick, thank you :-)

I have a date input field on my app and this input field is actually required. I can make this field required on the app side, of course, but somehow for me it feels more correct to have the validation also on backend side. Maybe this is just a personal feeling, but my approach is that the "app should be dumb". :-)

bastians avatar Jun 23 '20 10:06 bastians

Ah.. I understand. I agree that validation should be done on the backend, but this is a different kind of validation, as you limit the way people ask for data, right? I would instead limit the size of the response, as that is a more universal problem. You can try the "pageLimits" middleware and see whether or not that fits your needs.

mevdschee avatar Jun 23 '20 10:06 mevdschee

...as you limit the way people ask for data, right?

Yes, but limiting the response does not solve my problem. The input is from type "date" and based on the date, the user gets a result. But without the date, the request does not make sense and should fail because currently, it just shows all data in the result.

As I wrote, I can also do it on app side but anyway, to be able to define some filter parameters as required would be a nice feature :-)

By the way: Great job what you have done with this API :-) Thanks a lot!

bastians avatar Jun 25 '20 06:06 bastians

You can write a custom "beforeFilter" using the customization middleware and give an error when the date is not given as a filter parameter.

mevdschee avatar Jun 25 '20 07:06 mevdschee

Ok, perfect, then I will try this :-) Thank you!

bastians avatar Jun 25 '20 07:06 bastians