Skip to content

Commit

Permalink
Merge pull request #16 from dgraph-io/anurags92/gwlparserfix
Browse files Browse the repository at this point in the history
fix(Parser): Add directive argument validator
  • Loading branch information
all-seeing-code authored Mar 19, 2021
2 parents eb159c9 + f3f3532 commit 7914f56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ func validateArgs(schema *Schema, args ArgumentDefinitionList, currentDirective
return nil
}

func validateDirectiveArgs(dir *Directive, schema *Schema) *gqlerror.Error {
allowedArgs := make(map[string]struct{})
for _, arg := range schema.Directives[dir.Name].Arguments {
allowedArgs[arg.Name] = struct{}{}
}

for _, arg := range dir.Arguments {
if _, ok := allowedArgs[arg.Name]; !ok {
return gqlerror.ErrorPosf(dir.Position, "%s is not supported as an argument for %s directive.", arg.Name, dir.Name)
}
}
return nil

}

func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLocation, currentDirective *DirectiveDefinition) *gqlerror.Error {
for _, dir := range dirs {
if err := validateName(dir.Position, dir.Name); err != nil {
Expand All @@ -301,6 +316,9 @@ func validateDirectives(schema *Schema, dirs DirectiveList, location DirectiveLo
if schema.Directives[dir.Name] == nil {
return gqlerror.ErrorPosf(dir.Position, "Undefined directive %s.", dir.Name)
}
if err := validateDirectiveArgs(dir, schema); err != nil {
return err
}
validKind := false
for _, dirLocation := range schema.Directives[dir.Name].Locations {
if dirLocation == location {
Expand Down
10 changes: 10 additions & 0 deletions validator/schema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,16 @@ directives:
directive @A(a: Input, b: Scalar, c: Enum) on FIELD_DEFINITION
- name: Valid arg for directive
input: |
type User @include(aggregate: false) {
name: String
}
error:
message: 'aggregate is not supported as an argument for include directive.'
locations: [{line: 1, column: 12}]

- name: Objects not allowed
input: |
type Object { id: ID }
Expand Down

0 comments on commit 7914f56

Please sign in to comment.