So I would like to add some custom acrtions for a resource, and would like to apply the validation that the package uses to keep the responses as consistent as possible. Is there any way to do so? Or I need to create a new resource type in order to enforce this kinf of validation?
Thanks for the work on the package :), loving it.
Ah thanks for the feedback!
You could resolve the request class for the similar JSON API request you want out of the service container, by type-hinting it in the controller action method.
The classes are here:
https://github.com/cloudcreativity/laravel-json-api/tree/develop/src/Http/Requests
Note sure if that's what you're after, let me know if it is/isn't!
Worth saying I'm not entirely sure if that will exactly work. I'm quite uncomfortable with custom action stuff because it's a nightmare to maintain when I have no idea what people will try to do with them. The package is trying to support the spec, so it's not really within the scope of the package to support stuff outside of the spec... if that makes sense!
Makes sense, thx for the quick response. Will take a look and see if I can hack it around. :)
Makes sense, thx for the quick response. Will take a look and see if I can
hackit around. :)
Did you find a workaround ? I'm facing the same problem
@lindyhopchris hello! Would you mind provide an example of custom controller action with validation? I cannot quite understand how it can be done. I want to create custom action recognize, I can create new Request class for this, but abstract validator have only standard methods like update, create etc. However, I cannot use standard validation method at this route because it's already in use and validation differs dramatically with custom action route. It'll be very nice of you if you point me how to do it at least or add that example to an example repository. Thanks!
@myfbone sorry for slow reply.
if the validation differs drastically from any of the normal JSON API validation put in place by this package, you're probably best using standard Laravel validation. This package is only really designed to do validation if you're following the JSON API spec... if you have a custom action that deviates from the spec, it's not really possible to use this package - but Laravel gives you plenty of options on how to do validation.
@lindyhopchris is there a neat way to return laravel validation errors as JsonApi validation errors?
Yeah, there's a way... it's not neat or documented at the moment, but it works.
Resolve the Validation\ErrorTranslator class out of the service container, then call either the failedValidator or failedValidatorException method. The former creates JSON API error objects, the latter creates a JSON API validation exception that has the error objects in it.
(That class was meant as an internal-only class, I could probably do with adding a documented class to make it easier to do.)
See #486 for error building
@lindyhopchris sounds not too complicated, but a wrapper would be appreciated, too. Thanks anyway for your fast response!