Skip to content

Commit

Permalink
Merge pull request #1184 from joesaunderson/patch-1
Browse files Browse the repository at this point in the history
Don't validate fields that were not passed
  • Loading branch information
Vincz authored May 10, 2024
2 parents f3260df + c20063e commit 13b6b4b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Validator/InputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ private function buildValidationTree(ValidationNode $rootObject, iterable $field
$property = $arg['name'] ?? $name;
$config = static::normalizeConfig($arg['validation'] ?? []);

if (!array_key_exists($property, $inputData)) {
// This field was not provided in the inputData. Do not attempt to validate it.
continue;
}

if (isset($config['cascade']) && isset($inputData[$property])) {
$groups = $config['cascade'];
$argType = $this->unclosure($arg['type']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ Mutation:
validation:
cascade:
groups: ['group2']

onlyPassedFieldsValidation:
type: Boolean
resolve: '@=m("mutation_mock", args)'
args:
person:
validation: cascade
type: Person!
14 changes: 14 additions & 0 deletions tests/Functional/App/config/validator/mapping/Person.types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Person:
type: input-object
config:
fields:
firstName:
type: String
validation:
- NotBlank: ~
- NotNull: ~
surname:
type: String
validation:
- NotBlank: ~
- NotNull: ~
16 changes: 16 additions & 0 deletions tests/Functional/Validator/InputValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ public function testLinkedConstraintsValidationPasses(): void
$this->assertTrue($result['data']['linkedConstraintsValidation']);
}

public function testOnlyPassedFieldsValidated(): void
{
$query = '
mutation {
onlyPassedFieldsValidation(
person: { firstName: "Joe" }
)
}
';

$result = $this->executeGraphQLRequest($query);

$this->assertTrue(empty($result['errors']));
$this->assertTrue($result['data']['onlyPassedFieldsValidation']);
}

public function testLinkedConstraintsValidationFails(): void
{
$query = '
Expand Down

0 comments on commit 13b6b4b

Please sign in to comment.