8000 [Feature] Add validation data to rules method signature (#529) · CodingSeo/laravel-json-api@291ddee · GitHub
[go: up one dir, main page]

Skip to content

Commit 291ddee

Browse files
authored
[Feature] Add validation data to rules method signature (cloudcreativity#529)
Ensures the validator `rules()` method receives the data that is to be validated. Closes cloudcreativity#497
1 parent e1fc46a commit 291ddee

File tree

16 files changed

+38
-26
lines changed

16 files changed

+38
-26
lines changed

src/Validation/AbstractValidators.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ abstract class AbstractValidators implements ValidatorFactoryInterface
176176
*
177177
* @param mixed|null $record
178178
* the record being updated, or null if creating a resource.
179+
* @param array $data
180+
* the data that is being validated.
179181
* @return mixed
180182
*/
181-
abstract protected function rules($record = null): array;
183+
abstract protected function rules($record, array $data): array;
182184

183185
/**
184186
* Get query parameter validation rules.
@@ -208,7 +210,8 @@ public function supportsClientIds(): bool
208210
return $this->clientIds;
209211
}
210212

211-
return $this->clientIds = collect($this->rules())->has('id');
213+
return $this->clientIds = collect($this->rules(null, []))
214+
->has('id');
212215
}
213216

214217
/**
@@ -217,8 +220,8 @@ public function supportsClientIds(): bool
217220
public function create(array $document): ValidatorInterface
218221
{
219222
return $this->validatorForResource(
220-
$this->dataForCreate($document),
221-
$this->rules(),
223+
$data = $this->dataForCreate($document),
224+
$this->rules(null, $data),
222225
$this->messages(),
223226
$this->attributes()
224227
);
@@ -230,8 +233,8 @@ public function create(array $document): ValidatorInterface
230233
public function update($record, array $document): ValidatorInterface
231234
{
232235
return $this->validatorForResource(
233-
$this->dataForUpdate($record, $document),
234-
$this->rules($record),
236+
$data = $this->dataForUpdate($record, $document),
237+
$this->rules($record, $data),
235238
$this->messages($record),
236239
$this->attributes($record)
237240
);
@@ -259,11 +262,11 @@ public function delete($record): ?ValidatorInterface
259262
*/
260263
public function modifyRelationship($record, string $field, array $document): ValidatorInterface
261264
{
262-
$data = $this->dataForRelationship($record, $field, $document);
265+
$resource = ResourceObject::create($this->dataForRelationship($record, $field, $document));
263266

264267
return $this->factory->createRelationshipValidator(
265-
ResourceObject::create($data),
266-
$this->relationshipRules($record, $field),
268+
$resource,
269+
$this->relationshipRules($record, $field, $resource->all()),
267270
$this->messages(),
268271
$this->attributes()
269272
);
@@ -552,11 +555,12 @@ protected function dataForRelationship($record, string $field, array $document):
552555
*
553556
* @param mixed $record
554557
* @param string $field
558+
* @param array $data
555559
* @return array
556560
*/
557-
protected function relationshipRules($record, string $field): array
561+
protected function relationshipRules($record, string $field, array $data): array
558562
{
559-
return collect($this->rules($record))->filter(function ($v, $key) use ($field) {
563+
return collect($this->rules($record, $data))->filter(function ($v, $key) use ($field) {
560564
return Str::startsWith($key, $field);
561565
})->all();
562566
}

stubs/independent/validators.stub

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class DummyClass extends AbstractValidators
3636
*
3737
* @param mixed|null $record
3838
* the record being updated, or null if creating a resource.
39+
* @param array $data
40+
* the data being validated
3941
* @return mixed
4042
*/
41-
protected function rules($record = null): array
43+
protected function rules($record, array $data): array
4244
{
4345
return [
4446
//

tests/dummy/app/JsonApi/Avatars/Validators.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ public function update($record, array $document): ValidatorInterface
6767
*
6868
* @param mixed|null $record
6969
* the record being updated, or null if creating a resource.
70+
* @param array $data
71+
* the data being validated.
7072
* @return mixed
7173
*/
72-
protected function rules($record = null): array
74+
protected function rules($record, array $data): array
7375
{
7476
return [
7577
//

tests/dummy/app/JsonApi/Comments/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Validators extends AbstractValidators
5151
/**
5252
* @inheritdoc
5353
*/
54-
protected function rules($record = null): array
54+
protected function rules($record, array $data): array
5555
{
5656
return [
5757
'content' => "required|string|min:1",

tests/dummy/app/JsonApi/Countries/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Validators extends AbstractValidators
4141
/**
4242
* @inheritDoc
4343
*/
44-
protected function rules($record = null): array
44+
protected function rules($record, array $data): array
4545
{
4646
return [
4747
'name' => "required|string",

tests/dummy/app/JsonApi/Downloads/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Validators extends AbstractValidators
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected function rules($record = null): array
28+
protected function rules($record, array $data): array
2929
{
3030
return [
3131
'id' => 'nullable|string|min:1',

tests/dummy/app/JsonApi/Histories/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Validators extends AbstractValidators
3838
/**
3939
* @inheritDoc
4040
*/
41-
protected function rules($record = null): array
41+
protected function rules($record, array $data): array
4242
{
4343
return [
4444
'detail' => ['required', 'string'],

tests/dummy/app/JsonApi/Phones/Validators.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ class Validators extends AbstractValidators
3232
*
3333
* @param object|null $record
3434
* the record being updated, or null if it is a create request.
35+
* @param array $data
36+
* the data being validated.
3537
* @return array
3638
*/
37-
protected function rules($record = null): array
39+
protected function rules($record, array $data): array
3840
{
3941
return [
4042
//

tests/dummy/app/JsonApi/Posts/Validators.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ class Validators extends AbstractValidators
9999

100100
/**
101101
* @param Post|null $record
102+
* @param array $data
102103
* @return array|mixed
103104
*/
104-
protected function rules($record = null): array
105+
protected function rules($record, array $data): array
105106
{
106107
$slugUnique = 'unique:posts,slug';
107108

tests/dummy/app/JsonApi/QueueJobs/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Validators extends AbstractValidators
3131
/**
3232
* @inheritDoc
3333
*/
34-
protected function rules($record = null): array
34+
protected function rules($record, array $data): array
3535
{
3636
throw new RuntimeException('Not implemented.');
3737
}

tests/dummy/app/JsonApi/Sites/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Validators extends AbstractValidators
3232
/**
3333
* @inheritdoc
3434
*/
35-
protected function rules($record = null): array
35+
protected function rules($record, array $data): array
3636
{
3737
return [
3838
'id' => 'required|string',

tests/dummy/app/JsonApi/Suppliers/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Validators extends AbstractValidators
3838
/**
3939
* @inheritDoc
4040
*/
41-
protected function rules($record = null): array
41+
protected function rules($record, array $data): array
4242
{
4343
return [
4444
'name' => ['required', 'string'],

tests/dummy/app/JsonApi/Tags/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Validators extends AbstractValidators
3030
/**
3131
* @inheritDoc
3232
*/
33-
protected function rules($record = null): array
33+
protected function rules($record, array $data): array
3434
{
3535
return [
3636
'name' => "required|string|between:1,250",

tests/dummy/app/JsonApi/Users/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function update($record, array $document): ValidatorInterface
5757
/**
5858
* @inheritDoc
5959
*/
60-
protected function rules($record = null): array
60+
protected function rules($record, array $data): array
6161
{
6262
$rules = [
6363
'name' => 'required|string',

tests/dummy/app/JsonApi/Videos/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Validators extends AbstractValidators
3939
/**
4040
* @inheritDoc
4141
*/
42-
protected function rules($record = null): array
42+
protected function rules($record, array $data): array
4343
{
4444
return [
4545
'id' => 'required|regex:/' . Uuid::VALID_PATTERN . '/',

tests/lib/Integration/Eloquent/BelongsToTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ public function testReplaceNullRelationshipWithRelatedResource()
263263

264264
$data = ['type' => 'users', 'id' => (string) $user->getKey()];
265265

266-
$this->doReplaceRelationship($post, 'author', $data)
266+
$this->withoutExceptionHandling()
267+
->doReplaceRelationship($post, 'author', $data)
267268
->assertStatus(204);
268269

269270
$this->assertDatabaseHas('posts', [

0 commit comments

Comments
 (0)
0