Skip to content

RequiredValidator: remove non-visible definitions from the error message #5396

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

Merged
merged 2 commits into from
Jun 25, 2025
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
15 changes: 12 additions & 3 deletions lib/graphql/schema/validator/required_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,26 @@ def validate(_object, context, value)
end

def build_message(context)
argument_definitions = @validated.arguments(context).values
argument_definitions = context.types.arguments(@validated)

required_names = @one_of.map do |arg_keyword|
if arg_keyword.is_a?(Array)
names = arg_keyword.map { |arg| arg_keyword_to_graphql_name(argument_definitions, arg) }
names.compact! # hidden arguments are `nil`
"(" + names.join(" and ") + ")"
else
arg_keyword_to_graphql_name(argument_definitions, arg_keyword)
end
end
required_names.compact! # remove entries for hidden arguments


if required_names.size == 1
case required_names.size
when 0
# The required definitions were hidden from the client.
# Another option here would be to raise an error in the application....
"%{validated} is missing a required argument."
when 1
"%{validated} must include the following argument: #{required_names.first}."
else
"%{validated} must include exactly one of the following arguments: #{required_names.join(", ")}."
Expand All @@ -115,7 +124,7 @@ def build_message(context)

def arg_keyword_to_graphql_name(argument_definitions, arg_keyword)
argument_definition = argument_definitions.find { |defn| defn.keyword == arg_keyword }
argument_definition.graphql_name
argument_definition&.graphql_name
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/graphql/schema/validator/required_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

expectations = [
{
config: { one_of: [:a, :b] },
config: { one_of: [:a, :b, :secret] },
cases: [
{ 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."] },
Expand All @@ -32,6 +32,13 @@
{ query: "{ validated: multiValidated(b: 2) }", result: nil, error_messages: ["multiValidated must include exactly one of the following arguments: a, (b and c)."] },
]
},
{
name: "All options hidden",
config: { one_of: [:secret, :secret2] },
cases: [
{ query: "{ validated: multiValidated(a: 1, b: 2) }", result: nil, error_messages: ["multiValidated is missing a required argument."] },
],
},
{
name: "Definition order independence",
config: { one_of: [[:a, :b], :c] },
Expand Down
23 changes: 23 additions & 0 deletions spec/graphql/schema/validator/validator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ class NonBlankString < String
def build_schema(arg_type, validates_config)
schema = Class.new(GraphQL::Schema)

base_argument = Class.new(GraphQL::Schema::Argument) do
def initialize(*args, secret: false, **kwargs, &block)
super(*args, **kwargs, &block)
@secret = secret
end

def visible?(_ctx)
!@secret
end
end

base_field = Class.new(GraphQL::Schema::Field) do
argument_class(base_argument)
end

validated_input = Class.new(GraphQL::Schema::InputObject) do
graphql_name "ValidatedInput"
argument :a, arg_type, required: false
Expand Down Expand Up @@ -55,6 +70,7 @@ def resolve(input: :NO_INPUT)

query_type = Class.new(GraphQL::Schema::Object) do
graphql_name "Query"
field_class(base_field)
field :validated, arg_type do
argument :value, arg_type, required: false, validates: validates_config
end
Expand All @@ -67,6 +83,8 @@ def validated(value: nil)
argument :a, arg_type, required: false
argument :b, arg_type, required: false
argument :c, arg_type, required: false
argument :secret, arg_type, required: false, secret: true
argument :secret2, arg_type, required: false, secret: true
end

def multi_validated(a: 0, b: 0, c: 0)
Expand All @@ -91,6 +109,11 @@ def list
end

schema.query(query_type)
if ADD_WARDEN
schema.use(GraphQL::Schema::Warden)
else
schema.use(GraphQL::Schema::Visibility)
end
schema
end

Expand Down