Skip to content

Commit 70dbdb0

Browse files
GIANTCRABlindyhopchris
authored andcommitted
[DOCS] Add basic rule example (#330)
Add a basic validation rule example, to show how attributes and relationships are validated.
1 parent 44b58c8 commit 70dbdb0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/basics/validators.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ Your validator will be provided with the following array of data:
203203
];
204204
```
205205

206+
### Basic Rule Example
207+
208+
The following is an example query rule for the above-mentioned data:
209+
210+
```php
211+
protected function rules($record = null): array
212+
{
213+
return [
214+
'title' => 'required|string|min:1|max:255',
215+
'content' => 'required|string|min:1',
216+
'author.type' => 'in:users',
217+
'tags.*.type' => 'in:tags',
218+
];
219+
}
220+
221+
```
222+
223+
You'll notice that 'exists' is not used in the validation. This is because the package complies to the JSON API spec and validates if the record exists. Hence the following should **NOT** be used:
224+
225+
```php
226+
protected function rules($record = null): array
227+
{
228+
return [
229+
'author.id' => 'exists:users,id',
230+
'tags.*.id' => 'exists:tags,id'
231+
];
232+
}
233+
```
234+
206235
### Updating Resources
207236

208237
When updating resources, the JSON API specification says:

0 commit comments

Comments
 (0)