Skip to content

Commit

Permalink
Update validation message for one_of
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcwatt committed Oct 21, 2024
1 parent 56265cd commit a4a1db2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
16 changes: 14 additions & 2 deletions lib/graphql/schema/validator/required_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Validator
class RequiredValidator < Validator
# @param one_of [Symbol, Array<Symbol>] An argument, or a list of arguments, that represents a valid set of inputs for this field
# @param message [String]
def initialize(one_of: nil, argument: nil, message: "%{validated} has the wrong arguments", **default_options)
def initialize(one_of: nil, argument: nil, message: nil, **default_options)
@one_of = if one_of
one_of
elsif argument
[argument]
else
raise ArgumentError, "`one_of:` or `argument:` must be given in `validates required: {...}`"
end
@message = message
@message = message || build_message
super(**default_options)
end

Expand Down Expand Up @@ -76,6 +76,18 @@ def validate(_object, _context, value)
@message
end
end

def build_message
formatted_arguments = @one_of.map do |arg|
if arg.is_a?(Array)
"(" + arg.map { |a| Schema::Member::BuildType.camelize(a.to_s) }.join(" and ") + ")"
else
Schema::Member::BuildType.camelize(arg.to_s)
end
end

"%{validated} must include exactly one of the following arguments: #{formatted_arguments.join(", ")}."
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/schema/argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ def echo(str:)
res = RequiredNullableSchema.execute('{ echo(str: null) }')
assert_nil res["data"].fetch("echo")
res = RequiredNullableSchema.execute('{ echo }')
assert_equal ["echo has the wrong arguments"], res["errors"].map { |e| e["message"] }
assert_equal ["echo must include exactly one of the following arguments: str."], res["errors"].map { |e| e["message"] }
end
end

Expand Down
30 changes: 15 additions & 15 deletions spec/graphql/schema/validator/required_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
{
config: { one_of: [:a, :b] },
cases: [
{ query: "{ validated: multiValidated(a: 1, b: 2) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(c: 3) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(a: 1, b: 2) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, b."] },
{ query: "{ validated: multiValidated(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, b."] },
{ query: "{ validated: multiValidated }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, b."] },
{ query: "{ validated: multiValidated(c: 3) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, b."] },
{ query: "{ validated: multiValidated(a: 1) }", result: 1, error_messages: [] },
{ query: "{ validated: multiValidated(a: 1, c: 3) }", result: 4, error_messages: [] },
{ query: "{ validated: multiValidated(b: 2) }", result: 2, error_messages: [] },
Expand All @@ -24,10 +24,10 @@
cases: [
{ query: "{ validated: multiValidated(a: 1) }", result: 1, error_messages: [] },
{ query: "{ validated: multiValidated(b: 2, c: 3) }", result: 5, error_messages: [] },
{ query: "{ validated: multiValidated }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(c: 3) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated(b: 2) }", result: nil, error_messages: ["multiValidated has the wrong arguments"] },
{ query: "{ validated: multiValidated }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: multiValidated(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: multiValidated(c: 3) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: multiValidated(b: 2) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, (b and c)."] },
]
},
{
Expand All @@ -36,9 +36,9 @@
cases: [
{ query: "{ validated: validatedInput(input: { a: 1 }) }", result: 1, error_messages: [] },
{ query: "{ validated: validatedInput(input: { b: 2, c: 3 }) }", result: 5, error_messages: [] },
{ query: "{ validated: validatedInput(input: { a: 1, b: 2, c: 3 }) }", result: nil, error_messages: ["ValidatedInput has the wrong arguments"] },
{ query: "{ validated: validatedInput(input: { c: 3 }) }", result: nil, error_messages: ["ValidatedInput has the wrong arguments"] },
{ query: "{ validated: validatedInput(input: { b: 2 }) }", result: nil, error_messages: ["ValidatedInput has the wrong arguments"] },
{ query: "{ validated: validatedInput(input: { a: 1, b: 2, c: 3 }) }", result: nil, error_messages: ["ValidatedInput must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: validatedInput(input: { c: 3 }) }", result: nil, error_messages: ["ValidatedInput must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: validatedInput(input: { b: 2 }) }", result: nil, error_messages: ["ValidatedInput must include exactly one of the following arguments: a, (b and c)."] },
]
},
{
Expand All @@ -47,10 +47,10 @@
cases: [
{ query: "{ validated: validatedResolver(a: 1) }", result: 1, error_messages: [] },
{ query: "{ validated: validatedResolver(b: 2, c: 3) }", result: 5, error_messages: [] },
{ query: "{ validated: validatedResolver(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["validatedResolver has the wrong arguments"] },
{ query: "{ validated: validatedResolver(c: 3) }", result: nil, error_messages: ["validatedResolver has the wrong arguments"] },
{ query: "{ validated: validatedResolver(b: 2) }", result: nil, error_messages: ["validatedResolver has the wrong arguments"] },
{ query: "{ validated: validatedResolver }", result: nil, error_messages: ["validatedResolver has the wrong arguments"] },
{ query: "{ validated: validatedResolver(a: 1, b: 2, c: 3) }", result: nil, error_messages: ["validatedResolver must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: validatedResolver(c: 3) }", result: nil, error_messages: ["validatedResolver must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: validatedResolver(b: 2) }", result: nil, error_messages: ["validatedResolver must include exactly one of the following arguments: a, (b and c)."] },
{ query: "{ validated: validatedResolver }", result: nil, error_messages: ["validatedResolver must include exactly one of the following arguments: a, (b and c)."] },
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions spec/graphql/schema/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def int_input(input:)

res = ValidationInheritanceSchema.execute("{ intInput(input: { int: 1, otherInt: 2 }) }")
assert_nil res["data"]["intInput"]
assert_equal ["IntInput has the wrong arguments"], res["errors"].map { |e| e["message"] }
assert_equal ["IntInput must include exactly one of the following arguments: int, otherInt."], res["errors"].map { |e| e["message"] }
end

it "works with resolvers" do
Expand All @@ -204,7 +204,7 @@ def int_input(input:)

res = ValidationInheritanceSchema.execute("{ int(int: 1, otherInt: 2) }")
assert_nil res["data"]["int"]
assert_equal ["int has the wrong arguments"], res["errors"].map { |e| e["message"] }
assert_equal ["int must include exactly one of the following arguments: int, otherInt."], res["errors"].map { |e| e["message"] }
end
end
end

0 comments on commit a4a1db2

Please sign in to comment.