Skip to content

Commit 13b6b4b

Browse files
authored
Merge pull request #1184 from joesaunderson/patch-1
Don't validate fields that were not passed
2 parents f3260df + c20063e commit 13b6b4b

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/Validator/InputValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ private function buildValidationTree(ValidationNode $rootObject, iterable $field
140140
$property = $arg['name'] ?? $name;
141141
$config = static::normalizeConfig($arg['validation'] ?? []);
142142

143+
if (!array_key_exists($property, $inputData)) {
144+
// This field was not provided in the inputData. Do not attempt to validate it.
145+
continue;
146+
}
147+
143148
if (isset($config['cascade']) && isset($inputData[$property])) {
144149
$groups = $config['cascade'];
145150
$argType = $this->unclosure($arg['type']);

tests/Functional/App/config/validator/mapping/Mutation.types.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,11 @@ Mutation:
138138
validation:
139139
cascade:
140140
groups: ['group2']
141+
142+
onlyPassedFieldsValidation:
143+
type: Boolean
144+
resolve: '@=m("mutation_mock", args)'
145+
args:
146+
person:
147+
validation: cascade
148+
type: Person!
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Person:
2+
type: input-object
3+
config:
4+
fields:
5+
firstName:
6+
type: String
7+
validation:
8+
- NotBlank: ~
9+
- NotNull: ~
10+
surname:
11+
type: String
12+
validation:
13+
- NotBlank: ~
14+
- NotNull: ~

tests/Functional/Validator/InputValidatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ public function testLinkedConstraintsValidationPasses(): void
8787
$this->assertTrue($result['data']['linkedConstraintsValidation']);
8888
}
8989

90+
public function testOnlyPassedFieldsValidated(): void
91+
{
92+
$query = '
93+
mutation {
94+
onlyPassedFieldsValidation(
95+
person: { firstName: "Joe" }
96+
)
97+
}
98+
';
99+
100+
$result = $this->executeGraphQLRequest($query);
101+
102+
$this->assertTrue(empty($result['errors']));
103+
$this->assertTrue($result['data']['onlyPassedFieldsValidation']);
104+
}
105+
90106
public function testLinkedConstraintsValidationFails(): void
91107
{
92108
$query = '

0 commit comments

Comments
 (0)