Skip to content

Commit 1e99051

Browse files
authored
Merge pull request #901 from jacobdekeizer/validation-attributes
Validation attributes
2 parents 56f42b9 + c0a199b commit 1e99051

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
### Added
88
- Add support to use array in `controller` param in config [\#906 / viktorruskai](https://github.com/rebing/graphql-laravel/pull/906)
9+
- Add support for laravel validation attributes [\#901 / jacobdekeizer](https://github.com/rebing/graphql-laravel/pull/901)
910

1011
### Fixed
1112
- Allow 'always' to work on object types [\#473 / tinyoverflow \#369 / zjbarg](https://github.com/rebing/graphql-laravel/pull/892)

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The default GraphiQL view makes use of the global `csrf_token()` helper function
8787
- [Example using Laravel's validator directly](#example-using-laravels-validator-directly)
8888
- [Handling validation errors](#handling-validation-errors)
8989
- [Customizing error messages](#customizing-error-messages)
90+
- [Customizing attributes](#customizing-attributes)
9091
- [Misc notes](#misc-notes)
9192
- [Resolve method](#resolve-method)
9293
- [Resolver middleware](#resolver-middleware)
@@ -993,6 +994,21 @@ public function validationErrorMessages(array $args = []): array
993994
}
994995
```
995996

997+
#### Customizing attributes
998+
999+
The validation attributes can be customised by overriding the
1000+
`validationAttributes` method. This method should return an array of custom
1001+
attributes in the same way documented by Laravel's validation.
1002+
1003+
```php
1004+
public function validationAttributes(array $args = []): array
1005+
{
1006+
return [
1007+
'email' => 'email address',
1008+
];
1009+
}
1010+
```
1011+
9961012
#### Misc notes
9971013

9981014
Certain type declarations of GraphQL may cancel our or render certain validations

phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,11 @@ parameters:
13951395
count: 14
13961396
path: tests/Unit/MutationTest.php
13971397

1398+
-
1399+
message: "#^Relation 'test_validation_custom_attributes' is not found in Illuminate\\\\Contracts\\\\Support\\\\MessageBag model\\.$#"
1400+
count: 7
1401+
path: tests/Unit/MutationTest.php
1402+
13981403
-
13991404
message: "#^Relation 'test_with_rules_non_nullable_list_of_non_nullable_input_object' is not found in Illuminate\\\\Contracts\\\\Support\\\\MessageBag model\\.$#"
14001405
count: 3

src/Support/Field.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ public function validationErrorMessages(array $args = []): array
6363
return [];
6464
}
6565

66+
/**
67+
* Define custom Laravel Validator attributes as per Laravel 'custom attributes'.
68+
*
69+
* @param array<string,mixed> $args submitted arguments
70+
* @return array<string,string>
71+
*/
72+
public function validationAttributes(array $args = []): array
73+
{
74+
return [];
75+
}
76+
6677
/**
6778
* @param array<string,mixed> $args
6879
* @return array<string,mixed>
@@ -120,7 +131,10 @@ public function getValidator(array $args, array $rules): ValidatorContract
120131
// allow our error messages to be customised
121132
$messages = $this->validationErrorMessages($args);
122133

123-
return Validator::make($args, $rules, $messages);
134+
// allow our attributes to be customized
135+
$attributes = $this->validationAttributes($args);
136+
137+
return Validator::make($args, $rules, $messages, $attributes);
124138
}
125139

126140
/**

tests/Support/Objects/UpdateExampleMutationWithInputType.php

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public function args(): array
6262
'name' => 'test',
6363
'type' => Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('ExampleValidationInputObject')))),
6464
],
65+
66+
'test_validation_custom_attributes' => [
67+
'name' => 'custom validation attributes',
68+
'type' => Type::string(),
69+
'rules' => ['required'],
70+
],
71+
];
72+
}
73+
74+
public function validationAttributes(array $args = []): array
75+
{
76+
return [
77+
'test_validation_custom_attributes' => 'custom attribute',
6578
];
6679
}
6780

tests/Unit/MutationTest.php

+35-7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function testResolve(): void
7474
['email' => '[email protected]'],
7575
],
7676
],
77+
'test_validation_custom_attributes' => 'test',
7778
], [], $this->resolveInfoMock());
7879
}
7980

@@ -117,7 +118,8 @@ public function testValidationError(): void
117118
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.otherValue'));
118119
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.nest'));
119120
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.list'));
120-
self::assertCount(6, $messages->all());
121+
self::assertTrue($messages->has('test_validation_custom_attributes'));
122+
self::assertCount(7, $messages->all());
121123
}
122124

123125
public function testWithInput(): void
@@ -154,7 +156,8 @@ public function testWithInput(): void
154156
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.otherValue'));
155157
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.nest'));
156158
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.list'));
157-
self::assertCount(6, $messages->all());
159+
self::assertTrue($messages->has('test_validation_custom_attributes'));
160+
self::assertCount(7, $messages->all());
158161
}
159162

160163
public function testWithEmptyInput(): void
@@ -182,7 +185,8 @@ public function testWithEmptyInput(): void
182185
self::assertTrue($messages->has('test_with_rules'));
183186
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object'));
184187
self::assertTrue($messages->has('test_with_rules_closure'));
185-
self::assertCount(4, $messages->all());
188+
self::assertTrue($messages->has('test_validation_custom_attributes'));
189+
self::assertCount(5, $messages->all());
186190
}
187191

188192
public function testWithInputDepthOne(): void
@@ -212,7 +216,8 @@ public function testWithInputDepthOne(): void
212216

213217
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object'));
214218
self::assertTrue($messages->has('test_with_rules_closure'));
215-
self::assertCount(3, $messages->all());
219+
self::assertTrue($messages->has('test_validation_custom_attributes'));
220+
self::assertCount(4, $messages->all());
216221
}
217222

218223
public function testWithInputWithEmptyInputObjects(): void
@@ -245,6 +250,8 @@ public function testWithInputWithEmptyInputObjects(): void
245250

246251
self::assertTrue($messages->has('test_with_rules'));
247252

253+
self::assertTrue($messages->has('test_validation_custom_attributes'));
254+
248255
self::assertTrue($messages->has('test_with_rules_nullable_input_object.otherValue'));
249256
self::assertTrue($messages->has('test_with_rules_nullable_input_object.val'));
250257
self::assertTrue($messages->has('test_with_rules_nullable_input_object.nest'));
@@ -253,7 +260,7 @@ public function testWithInputWithEmptyInputObjects(): void
253260
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.val'));
254261
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.nest'));
255262
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object.list'));
256-
self::assertCount(12, $messages->all());
263+
self::assertCount(13, $messages->all());
257264
}
258265

259266
public function testWithEmptyArrayOfInputsObjects(): void
@@ -282,7 +289,8 @@ public function testWithEmptyArrayOfInputsObjects(): void
282289
self::assertTrue($messages->has('test_with_rules'));
283290
self::assertTrue($messages->has('test_with_rules_non_nullable_input_object'));
284291
self::assertTrue($messages->has('test_with_rules_closure'));
285-
self::assertCount(4, $messages->all());
292+
self::assertTrue($messages->has('test_validation_custom_attributes'));
293+
self::assertCount(5, $messages->all());
286294
}
287295

288296
public function testWithArrayOfInputsObjects(): void
@@ -319,7 +327,8 @@ public function testWithArrayOfInputsObjects(): void
319327
self::assertTrue($messages->has('test_with_rules_non_nullable_list_of_non_nullable_input_object.0.otherValue'));
320328
self::assertTrue($messages->has('test_with_rules_non_nullable_list_of_non_nullable_input_object.0.nest'));
321329
self::assertTrue($messages->has('test_with_rules_non_nullable_list_of_non_nullable_input_object.0.list'));
322-
self::assertCount(7, $messages->all());
330+
self::assertTrue($messages->has('test_validation_custom_attributes'));
331+
self::assertCount(8, $messages->all());
323332
}
324333

325334
public function testCustomValidationErrorMessages(): void
@@ -356,6 +365,25 @@ public function testCustomValidationErrorMessages(): void
356365
);
357366
}
358367

368+
public function testCustomValidationAttributes(): void
369+
{
370+
$class = $this->getFieldClass();
371+
$field = new $class();
372+
$attributes = $field->getAttributes();
373+
374+
/** @var ValidationError $exception */
375+
$exception = null;
376+
377+
try {
378+
$attributes['resolve'](null, [], [], $this->resolveInfoMock());
379+
} catch (ValidationError $exception) {
380+
}
381+
382+
$messages = $exception->getValidatorMessages();
383+
384+
self::assertEquals('The custom attribute field is required.', $messages->first('test_validation_custom_attributes'));
385+
}
386+
359387
public function testRuleCallbackArgumentsMatchesTheInput(): void
360388
{
361389
$this->expectException(ValidationError::class);

0 commit comments

Comments
 (0)