Skip to content

Commit

Permalink
Merge pull request #5130 from alexcwatt/one-of-validation-error
Browse files Browse the repository at this point in the history
Update validation message for one_of
  • Loading branch information
rmosolgo authored Oct 24, 2024
2 parents 238c4d5 + 8e7cdc3 commit 2ae5393
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
32 changes: 28 additions & 4 deletions lib/graphql/schema/validator/required_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ class Validator
# end
#
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 one_of [Array<Symbol>] A list of arguments, exactly one of which is required for this field
# @param argument [Symbol] An argument that is required 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
Expand All @@ -49,7 +50,7 @@ def initialize(one_of: nil, argument: nil, message: "%{validated} has the wrong
super(**default_options)
end

def validate(_object, _context, value)
def validate(_object, context, value)
matched_conditions = 0

if !value.nil?
Expand All @@ -73,9 +74,32 @@ def validate(_object, _context, value)
if matched_conditions == 1
nil # OK
else
@message
@message || build_message(context)
end
end

def build_message(context)
argument_definitions = @validated.arguments(context).values
required_names = @one_of.map do |arg_keyword|
if arg_keyword.is_a?(Array)
names = arg_keyword.map { |arg| arg_keyword_to_grapqhl_name(argument_definitions, arg) }
"(" + names.join(" and ") + ")"
else
arg_keyword_to_grapqhl_name(argument_definitions, arg_keyword)
end
end

if required_names.size == 1
"%{validated} must include the following argument: #{required_names.first}."
else
"%{validated} must include exactly one of the following arguments: #{required_names.join(", ")}."
end
end

def arg_keyword_to_grapqhl_name(argument_definitions, arg_keyword)
argument_definition = argument_definitions.find { |defn| defn.keyword == arg_keyword }
argument_definition.graphql_name
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 the following argument: 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 2ae5393

Please sign in to comment.