@@ -16,6 +16,64 @@ This package is an specialisation of Laravel's `JsonResource` class.
1616All the underlying API's are still there, thus in your controller you can still interact
1717with ` JsonApiResource ` classes as you would with the base ` JsonResource ` class
1818
19+ ## Request
20+ This package allows the reading and dynamic inclusion of resources that will be requested in the requests via the "include" parameter.
21+ ** @see ** _ [ {json: api } fetching-includes] ( https://jsonapi.org/format/#fetching-includes ) _
22+
23+ Resource attributes will also be filtered according to the "fields" parameter.
24+ ** @see ** _ [ {json: api } fetching-fields] ( https://jsonapi.org/format/#fetching-sparse-fieldsets ) _
25+
26+ You can also very simply validate your requests for a given resource via the rules ` Rules\Includes ` and ` Rules\Fields ` .
27+
28+ ### Include validation
29+
30+ ``` php
31+ use \Ark4ne\JsonApi\Requests\Rules\Includes;
32+ use \Illuminate\Foundation\Http\FormRequest;
33+
34+ class UserFetchRequest extends FormRequest
35+ {
36+ public function rules()
37+ {
38+ return [
39+ 'include' => [new Includes(UserResource::class)],
40+ ]
41+ }
42+ }
43+ ```
44+
45+ ` Rules\Includes ` will validate the include to exactly match the UserResource schema (determined by the relationships).
46+
47+
48+ ### Fields validation
49+
50+ ``` php
51+ use \Ark4ne\JsonApi\Requests\Rules\Fields;
52+ use \Illuminate\Foundation\Http\FormRequest;
53+
54+ class UserFetchRequest extends FormRequest
55+ {
56+ public function rules()
57+ {
58+ return [
59+ 'include' => [new Fields(UserResource::class)],
60+ ]
61+ }
62+ }
63+ ```
64+
65+ ` Rules\Fields ` will validate the fields to exactly match the UserResource schema (determined by the attributes and relationships).
66+
67+
68+ ### Customize validation message
69+ | Trans key | default |
70+ | -------------------------------------------------------| ------------------------------------------------------|
71+ | ` validation.custom.jsonapi.fields.invalid ` | The selected : attribute is invalid. |
72+ | ` validation.custom.jsonapi.fields.invalid_fields ` | ": resource " doesn \' t have fields ": fields ". |
73+ | ` validation.custom.jsonapi.fields.invalid_resource ` | ": resource " doesn \' t exists. |
74+ | ` validation.custom.jsonapi.includes.invalid ` | The selected : attribute is invalid. |
75+ | ` validation.custom.jsonapi.includes.invalid_includes ` | ": include " doesn \' t have relationship ": relation ". |
76+
1977## Resource
2078** @see ** _ [ {json: api } resource-type] ( https://jsonapi.org/format/#document-resource-objects ) _
2179
@@ -145,6 +203,13 @@ _**@see** [{json:api} resources-relationships](https://jsonapi.org/format/#docum
145203
146204Returns resource relationships.
147205
206+ All relationships ** must** be created with ` ModelResource::relationship ` .
207+ This allows the generation of the schema representing the resource and thus the validation of request includes.
208+
209+ If your relation should have been a collection created via the ` ::collection(...) ` method, you can simply use ` ->asCollection() ` .
210+
211+ If you want the relation data to be loaded only when it is present in the request include, you can use the ` ->whenIncluded() ` method.
212+
148213``` php
149214protected function toRelationships(Request $request): array
150215{
0 commit comments