php-crud-api
php-crud-api copied to clipboard
Mandatory / required filter in requests
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?
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.
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". :-)
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.
...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!
You can write a custom "beforeFilter" using the customization middleware and give an error when the date is not given as a filter parameter.
Ok, perfect, then I will try this :-) Thank you!