diff --git a/lib/graphql/schema/validator/required_validator.rb b/lib/graphql/schema/validator/required_validator.rb index f939b15c1c..7daffda3ff 100644 --- a/lib/graphql/schema/validator/required_validator.rb +++ b/lib/graphql/schema/validator/required_validator.rb @@ -37,7 +37,7 @@ class Validator class RequiredValidator < Validator # @param one_of [Symbol, Array] 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 @@ -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 diff --git a/spec/graphql/schema/validator/required_validator_spec.rb b/spec/graphql/schema/validator/required_validator_spec.rb index b59813ac84..ea896ddcb3 100644 --- a/spec/graphql/schema/validator/required_validator_spec.rb +++ b/spec/graphql/schema/validator/required_validator_spec.rb @@ -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: [] }, @@ -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)."] }, ] }, { @@ -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)."] }, ] }, { @@ -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)."] }, ] }, {