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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate directive arguments during schema parsing #258

Merged
merged 3 commits into from
Jun 10, 2023
Merged
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
17 changes: 15 additions & 2 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,12 @@ func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLo
if currentDirective != nil && dir.Name == currentDirective.Name {
return gqlerror.ErrorPosf(dir.Position, "Directive %s cannot refer to itself.", currentDirective.Name)
}
if schema.Directives[dir.Name] == nil {
dirDefinition := schema.Directives[dir.Name]
if dirDefinition == nil {
return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name)
}
validKind := false
for _, dirLocation := range schema.Directives[dir.Name].Locations {
for _, dirLocation := range dirDefinition.Locations {
if dirLocation == location {
validKind = true
break
Expand All @@ -372,6 +373,18 @@ func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLo
if !validKind {
return gqlerror.ErrorPosf(dir.Position, "Directive %s is not applicable on %s.", dir.Name, location)
}
for _, arg := range dir.Arguments {
if dirDefinition.Arguments.ForName(arg.Name) == nil {
return gqlerror.ErrorPosf(arg.Position, "Undefined argument %s for directive %s.", arg.Name, dir.Name)
}
}
for _, schemaArg := range dirDefinition.Arguments {
if schemaArg.Type.NonNull {
if arg := dir.Arguments.ForName(schemaArg.Name); arg == nil || arg.Value.Kind == NullValue {
return gqlerror.ErrorPosf(dir.Position, "Argument %s for directive %s cannot be null.", schemaArg.Name, dir.Name)
}
}
}
dir.Definition = schema.Directives[dir.Name]
}
return nil
Expand Down
26 changes: 26 additions & 0 deletions validator/schema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,32 @@ directives:
type P { name: String @testField }
interface I { id: ID @testField }

- name: Invalid directive argument not allowed
input: |
directive @foo(bla: Int!) on FIELD_DEFINITION
type P {f: Int @foo(foobla: 11)}

error:
message: 'Undefined argument foobla for directive foo.'
locations: [{line: 2, column: 21}]

- name: non-null argument must be provided
input: |
directive @foo(bla: Int!) on FIELD_DEFINITION
type P {f: Int @foo }

error:
message: 'Argument bla for directive foo cannot be null.'
locations: [{line: 2, column: 17}]

- name: non-null argument must not be null
input: |
directive @foo(bla: Int!) on FIELD_DEFINITION
type P {f: Int @foo(bla: null) }

error:
message: 'Argument bla for directive foo cannot be null.'
locations: [{line: 2, column: 17}]

entry points:
- name: multiple schema entry points
Expand Down