Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 166 additions & 101 deletions src/Validator/InputValidator.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ Address:
- Choice:
groups: ['group1']
choices: ['New York', 'Berlin', 'Tokyo']
country:
type: Country
validation: cascade
zipCode:
type: Int!
validation:
- Expression: "service_validator.isZipCodeValid(value)"
period:
type: Period!
type: Period
validation: cascade
13 changes: 13 additions & 0 deletions tests/Functional/App/config/validator/mapping/Country.types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Country:
type: input-object
config:
fields:
name:
type: String
validation:
- NotBlank:
allowNull: true
officialLanguage:
type: String
validation:
- Choice: ['en', 'de', 'fr']
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ Mutation:
cascade:
groups: ['group2']

onlyPassedFieldsValidation:
partialInputObjectsCollectionValidation:
type: Boolean
resolve: '@=m("mutation_mock", args)'
resolve: "@=m('mutation_mock', args)"
args:
person:
addresses:
type: '[Address]'
validation: cascade
type: Person!
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Period:
config:
fields:
startDate:
type: String!
type: String
validation:
- Date: ~
- Overblog\GraphQLBundle\Tests\Functional\App\Validator\AtLeastOneOf:
Expand All @@ -12,7 +12,7 @@ Period:
message: "Year should be GreaterThanOrEqual -100."
includeInternalMessages: false
endDate:
type: String!
type: String
validation:
- Expression: "this.getParent().getName() === 'Address'"
- Date: ~
Expand Down
14 changes: 0 additions & 14 deletions tests/Functional/App/config/validator/mapping/Person.types.yml

This file was deleted.

28 changes: 28 additions & 0 deletions tests/Functional/Validator/ExpectedErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ final class ExpectedErrors
],
];

public const PARTIAL_INPUT_OBJECTS_COLLECTION = [
'message' => 'validation',
'locations' => [['line' => 3, 'column' => 17]],
'path' => ['partialInputObjectsCollectionValidation'],
'extensions' => [
'validation' => [
'addresses[0].country.officialLanguage' => [
0 => [
'message' => 'The value you selected is not a valid choice.',
'code' => '8e179f1b-97aa-4560-a02f-2a8b42e49df7',
],
],
'addresses[1].country.name' => [
0 => [
'message' => 'This value should not be blank.',
'code' => 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
],
],
'addresses[1].period.endDate' => [
0 => [
'message' => 'This value should be greater than "2000-01-01".',
'code' => '778b7ae0-84d3-481a-9dec-35fdb64b1d78',
],
],
],
],
];

public static function simpleValidation(string $fieldName): array
{
return [
Expand Down
64 changes: 48 additions & 16 deletions tests/Functional/Validator/InputValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,6 @@ 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 Expand Up @@ -393,4 +377,52 @@ public function testAutoValidationAutoThrowWithGroupsFails(): void
$this->assertSame(ExpectedErrors::cascadeWithGroups('autoValidationAutoThrowWithGroups'), $result['errors'][0]);
$this->assertNull($result['data']['autoValidationAutoThrowWithGroups']);
}

public function testPartialInputObjectsCollectionValidation(): void
{
$query = '
mutation {
partialInputObjectsCollectionValidation(
addresses: [
{
street: "Washington Street"
city: "Berlin"
zipCode: 10000
# Country is present, but the language is invalid
country: {
name: "Germany"
officialLanguage: "ru"
}
# Period is completely missing, skip validation
},
{
street: "Washington Street"
city: "New York"
zipCode: 10000
# Country is partially present
country: {
name: "" # Name should not be blank
# language is missing
}
period: {
startDate: "2000-01-01"
endDate: "1990-01-01"
}
},
{
street: "Washington Street"
city: "New York"
zipCode: 10000
country: {} # Empty input object, skip validation
period: {} # Empty input object, skip validation
}
]
)
}
';

$result = $this->executeGraphQLRequest($query);
$this->assertSame(ExpectedErrors::PARTIAL_INPUT_OBJECTS_COLLECTION, $result['errors'][0]);
$this->assertNull($result['data']['partialInputObjectsCollectionValidation']);
}
}