cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
780 stars 109 forks source link

Question: How to Resolve a Validators Class by Resource Type? #241

Closed dimitvassilev closed 5 years ago

dimitvassilev commented 6 years ago

Hello again @lindyhopchris,

In a test, I am trying to get the validators class of json-api resource type with json_api('v1')->getContainer()->getValidatorsByResourceType($resourceType);, however an exception is thrown "CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException : JSON API 'default' does not exist.", as far as I could track due to the bindInboundRequest() method of CloudCreativity\LaravelJsonApi\ServiceProvider calling the json_api() helper with no argument and it looks for a 'default' api whereas I have only defined a 'v1' api, but at the point of calling it I haven't even yet made a request, so I am quite lost what exactly is going on. Am I not supposed to try resolving the validator outside a request or am I doing it wrong? I guess I can always get it via the laravel container using an explicit mapping between resource types and validator class names, but was wondering if there was a cleaner way to do it.

As always, your guidlines are much appreciated!

lindyhopchris commented 6 years ago

So this should in theory be possible though I've never encountered it myself. I'm surprised it's having to look up the inbound request when trying to get the validators class. I'll be honest that I don't think this is a missive priority for 1.0.0 but if I look into it and it's a simple fix, I'll get it fixed.

Btw, if you've only got one API and it's not called default, you should set the default API name as per this bit of the docs: https://github.com/cloudcreativity/laravel-json-api/blob/develop/docs/basics/api.md#introduction

dimitvassilev commented 6 years ago

I have indeed missed this configuration and it actually solved my problem with the exception. Thank you!