Skip to content

Commit

Permalink
implement duplicated field detections
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Nov 19, 2018
1 parent 6f09700 commit b182c53
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions validator/schema.go
Expand Up @@ -199,6 +199,14 @@ func validateDefinition(schema *Schema, def *Definition) *gqlerror.Error {
}
}

for idx, field1 := range def.Fields {
for _, field2 := range def.Fields[idx+1:] {
if field1.Name == field2.Name {
return gqlerror.ErrorPosf(field2.Position, "Field %s.%s can only be defined once.", def.Name, field2.Name)
}
}
}

return validateDirectives(schema, def.Directives, nil)
}

Expand Down
32 changes: 32 additions & 0 deletions validator/schema_test.yml
Expand Up @@ -10,6 +10,38 @@ types:
error:
message: "Cannot redeclare type A."
locations: [{line: 4, column: 6}]
- name: cannot be duplicated field at same definition 1
input: |
type A {
name: String
name: String
}
error:
message: "Field A.name can only be defined once."
locations: [{line: 3, column: 3}]
- name: cannot be duplicated field at same definition 2
input: |
type A {
name: String
}
extend type A {
name: String
}
error:
message: "Field A.name can only be defined once."
locations: [{line: 5, column: 3}]
- name: cannot be duplicated field at same definition 3
input: |
type A {
name: String
}
extend type A {
age: Int
age: Int
}
error:
message: "Field A.age can only be defined once."
locations: [{line: 6, column: 3}]

object types:
- name: must define one or more fields
Expand Down

0 comments on commit b182c53

Please sign in to comment.