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 2d48289
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
18 changes: 16 additions & 2 deletions lib/graphql/schema/validator/required_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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
Expand Down Expand Up @@ -73,9 +73,23 @@ def validate(_object, _context, value)
if matched_conditions == 1
nil # OK
else
@message
message
end
end

def message
return @message unless @message.nil?

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
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

0 comments on commit 2d48289

Please sign in to comment.