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

Fix exception when field name contains all invalid characters #1096

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
6 changes: 3 additions & 3 deletions lib/absinthe/phase/schema/validation/names_must_be_valid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Absinthe.Phase.Schema.Validation.NamesMustBeValid do
alias Absinthe.Blueprint
alias Absinthe.Blueprint.Schema

@valid_name_regex ~r/[_A-Za-z][_0-9A-Za-z]*/
@valid_name_regex ~r/^[_A-Za-z][_0-9A-Za-z]*$/

def run(bp, _) do
bp = Blueprint.prewalk(bp, &validate_names/1)
Expand All @@ -31,8 +31,7 @@ defmodule Absinthe.Phase.Schema.Validation.NamesMustBeValid do
end

defp valid_name?(name) do
[match] = Regex.run(@valid_name_regex, name)
match == name
Regex.match?(@valid_name_regex, name)
end

defp error(object, data) do
Expand All @@ -50,6 +49,7 @@ defmodule Absinthe.Phase.Schema.Validation.NamesMustBeValid do
defp struct_to_kind(Schema.ScalarTypeDefinition), do: "scalar"
defp struct_to_kind(Schema.ObjectTypeDefinition), do: "object"
defp struct_to_kind(Schema.InputObjectTypeDefinition), do: "input object"
defp struct_to_kind(Schema.EnumValueDefinition), do: "enum value"
defp struct_to_kind(_), do: "type"

@description """
Expand Down
3 changes: 2 additions & 1 deletion test/absinthe/schema/rule/names_must_be_valid_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule Absinthe.Schema.Rule.NamesMustBeValidTest do
%{
phase: NamesMustBeValid,
extra: %{artifact: "input object name", value: "bad input name"}
}
},
%{phase: NamesMustBeValid, extra: %{artifact: "enum value name", value: "1"}}
])
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/support/fixtures/dynamic/bad_names_schema.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ defmodule Absinthe.TestSupport.Schema.BadNamesSchema do
arg :foo, :string, name: "bad arg name"
end
end

enum :rating do
value :"1", as: 1
end
end