Skip to content

Commit

Permalink
Enforce EnumValue name uniqueness
Browse files Browse the repository at this point in the history
Raises an exception when trying to add a value that already exists with
the same name.
  • Loading branch information
swalkinshaw committed Jul 10, 2017
1 parent db45cdb commit 1e8538e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/graphql/enum_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def values=(new_values)

# @param enum_value [EnumValue] A value to add to this type's set of values
def add_value(enum_value)
if @values_by_name.key?(enum_value.name)
raise "Enum value names must be unique. `#{enum_value.name}` already exists."
end

@values_by_name[enum_value.name] = enum_value
end

Expand Down
8 changes: 8 additions & 0 deletions spec/graphql/enum_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@
assert_equal(7, enum_2.values.size)
end
end

describe "validates enum value name uniqueness" do
it "raises an exception when adding a duplicate enum value name" do
assert_raises "Enum value names must be unique. `COW` already exists." do
enum.add_value(GraphQL::EnumType::EnumValue.define(name: "COW"))
end
end
end
end

0 comments on commit 1e8538e

Please sign in to comment.