Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate default values match argument type #843

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 10 additions & 14 deletions src/Type/SchemaValidationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,25 +508,21 @@ private function validateFields(Type $type): void
}

// Ensure argument definition directives are valid
if (! isset($arg->astNode, $arg->astNode->directives)) {
continue;
if (isset($arg->astNode, $arg->astNode->directives)) {
$this->validateDirectivesAtLocation(
$arg->astNode->directives,
DirectiveLocation::ARGUMENT_DEFINITION
);
}

$this->validateDirectivesAtLocation(
$arg->astNode->directives,
DirectiveLocation::ARGUMENT_DEFINITION
);
}

// Ensure any directives are valid
if (! isset($field->astNode, $field->astNode->directives)) {
continue;
if (isset($field->astNode, $field->astNode->directives)) {
$this->validateDirectivesAtLocation(
$field->astNode->directives,
DirectiveLocation::FIELD_DEFINITION
);
}

$this->validateDirectivesAtLocation(
$field->astNode->directives,
DirectiveLocation::FIELD_DEFINITION
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Utils/ASTDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ function (InputValueDefinitionNode $value): array {
'astNode' => $value,
];
if (isset($value->defaultValue)) {
// Might result in the defaultValue being Utils::undefined() if the type does not match.
// Again, we do not throw immediately but rather defer this check to schema validation.
$config['defaultValue'] = AST::valueFromAST($value->defaultValue, $type);
}

Expand Down
1 change: 1 addition & 0 deletions src/Validator/DocumentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public static function securityRules()
public static function sdlRules()
{
if (self::$sdlRules === null) {
// TODO add rule DefaultValuesMatchArgumentType
self::$sdlRules = [
LoneSchemaDefinition::class => new LoneSchemaDefinition(),
KnownDirectives::class => new KnownDirectives(),
Expand Down
26 changes: 26 additions & 0 deletions tests/Type/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,32 @@ public function invalidEnumValueName(): array
];
}

public function testRejectsFieldArgumentsWithIncorrectDefaultType(): void
{
$schema = BuildSchema::build('
type Query {
foo(
int: Int = "not an int"
string: String = 42
): ID
}
');

$this->assertMatchesValidationMessage(
$schema->validate(),
[
[
'message' => 'Expected foo.int to have a default value of type Int, got "not an int".',
'locations' => [['line' => 7, 'column' => 16], ['line' => 11, 'column' => 16]],
],
[
'message' => 'Expected foo.string to have a default value of type String, got 42.',
'locations' => [['line' => 7, 'column' => 16], ['line' => 11, 'column' => 16]],
],
]
);
}

/**
* @see it('rejects an Enum type with incorrectly named values')
*
Expand Down