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

Implement UniqueArgumentNames validator #9

Merged
merged 1 commit into from
Jul 11, 2018
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
4 changes: 2 additions & 2 deletions validator/known_argument_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func init() {
fieldVisitors = append(fieldVisitors, knownFieldArgumentNames)
directiveVisitors = append(directiveVisitors, knowDirectiveArgumentNames)
directiveVisitors = append(directiveVisitors, knownDirectiveArgumentNames)
}

// A GraphQL field is only valid if all supplied arguments are defined by that field.
Expand All @@ -33,7 +33,7 @@ func knownFieldArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, fieldDe
}
}

func knowDirectiveArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation) {
func knownDirectiveArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation) {
if directiveDef == nil {
return
}
Expand Down
34 changes: 34 additions & 0 deletions validator/unique_argument_names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package validator

import (
"github.com/vektah/gqlparser"
)

func init() {
fieldVisitors = append(fieldVisitors, uniqueFieldArgumentNames)
directiveVisitors = append(directiveVisitors, uniqueDirectiveArgumentNames)
}

func checkUniqueArgs(ctx *vctx, args []gqlparser.Argument) {
knownArgNames := map[string]bool{}

for _, arg := range args {
if knownArgNames[arg.Name] {
ctx.errors = append(ctx.errors, Error(
Rule("UniqueArgumentNames"),
Message(`There can be only one argument named "%s".`, arg.Name),
))
}

knownArgNames[arg.Name] = true
}
}

// A GraphQL field is only valid if all supplied arguments are defined by that field.
func uniqueFieldArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, fieldDef *gqlparser.FieldDefinition, field *gqlparser.Field) {
checkUniqueArgs(ctx, field.Arguments)
}

func uniqueDirectiveArgumentNames(ctx *vctx, parentDef *gqlparser.Definition, directiveDef *gqlparser.DirectiveDefinition, directive *gqlparser.Directive, location gqlparser.DirectiveLocation) {
checkUniqueArgs(ctx, directive.Arguments)
}
1 change: 1 addition & 0 deletions validator/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestSpec(t *testing.T) {

t.Run("ScalarLeafs", runSpec(schemas, deviations, "../spec/validation/ScalarLeafs.yml"))
t.Run("SingleFieldSubscriptions", runSpec(schemas, deviations, "../spec/validation/SingleFieldSubscriptions.yml"))
t.Run("UniqueArgumentNames", runSpec(schemas, deviations, "../spec/validation/UniqueArgumentNames.yml"))
}

func runSpec(schemas []*gqlparser.Schema, deviations map[string]*Spec, filename string) func(t *testing.T) {
Expand Down