Skip to content

Commit 1a90cec

Browse files
authored
Merge pull request #3928 from rmosolgo/validation-clean-up
Validation clean up
2 parents faeef68 + f70b89f commit 1a90cec

File tree

10 files changed

+33
-249
lines changed

10 files changed

+33
-249
lines changed

guides/type_definitions/scalars.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,4 @@ Your class must define two class methods:
9797

9898
When incoming data is incorrect, the method may raise {{ "GraphQL::CoercionError" | api_doc }}, which will be returned to the client in the `"errors"` key.
9999

100-
101100
Scalar classes are never initialized; only their `.coerce_*` methods are called at runtime.

lib/graphql/query.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22
require "graphql/query/context"
33
require "graphql/query/fingerprint"
4-
require "graphql/query/literal_input"
54
require "graphql/query/null_context"
65
require "graphql/query/result"
76
require "graphql/query/variables"

lib/graphql/query/input_validation_result.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ class Query
44
class InputValidationResult
55
attr_accessor :problems
66

7+
def self.from_problem(explanation, path = nil, extensions: nil, message: nil)
8+
result = self.new
9+
result.add_problem(explanation, path, extensions: extensions, message: message)
10+
result
11+
end
12+
713
def initialize(valid: true, problems: nil)
814
@valid = valid
915
@problems = problems
@@ -27,7 +33,7 @@ def add_problem(explanation, path = nil, extensions: nil, message: nil)
2733
end
2834

2935
def merge_result!(path, inner_result)
30-
return if inner_result.valid?
36+
return if inner_result.nil? || inner_result.valid?
3137

3238
if inner_result.problems
3339
inner_result.problems.each do |p|
@@ -38,6 +44,9 @@ def merge_result!(path, inner_result)
3844
# It could have been explicitly set on inner_result (if it had no problems)
3945
@valid = false
4046
end
47+
48+
VALID = self.new
49+
VALID.freeze
4150
end
4251
end
4352
end

lib/graphql/query/literal_input.rb

Lines changed: 0 additions & 131 deletions
This file was deleted.

lib/graphql/schema/enum.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,14 @@ def kind
123123
end
124124

125125
def validate_non_null_input(value_name, ctx)
126-
result = GraphQL::Query::InputValidationResult.new
127-
128126
allowed_values = ctx.warden.enum_values(self)
129127
matching_value = allowed_values.find { |v| v.graphql_name == value_name }
130128

131129
if matching_value.nil?
132-
result.add_problem("Expected #{GraphQL::Language.serialize(value_name)} to be one of: #{allowed_values.map(&:graphql_name).join(', ')}")
130+
GraphQL::Query::InputValidationResult.from_problem("Expected #{GraphQL::Language.serialize(value_name)} to be one of: #{allowed_values.map(&:graphql_name).join(', ')}")
131+
else
132+
nil
133133
end
134-
135-
result
136134
end
137135

138136
def coerce_result(value, ctx)

lib/graphql/schema/input_object.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,15 @@ def kind
127127
INVALID_OBJECT_MESSAGE = "Expected %{object} to be a key-value object responding to `to_h` or `to_unsafe_h`."
128128

129129
def validate_non_null_input(input, ctx)
130-
result = GraphQL::Query::InputValidationResult.new
131-
132130
warden = ctx.warden
133131

134132
if input.is_a?(Array)
135-
result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
136-
return result
133+
return GraphQL::Query::InputValidationResult.from_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
137134
end
138135

139136
if !(input.respond_to?(:to_h) || input.respond_to?(:to_unsafe_h))
140137
# We're not sure it'll act like a hash, so reject it:
141-
result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
142-
return result
138+
return GraphQL::Query::InputValidationResult.from_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) })
143139
end
144140

145141
# Inject missing required arguments
@@ -151,18 +147,22 @@ def validate_non_null_input(input, ctx)
151147
m
152148
end
153149

154-
150+
result = nil
155151
[input, missing_required_inputs].each do |args_to_validate|
156152
args_to_validate.each do |argument_name, value|
157153
argument = warden.get_argument(self, argument_name)
158154
# Items in the input that are unexpected
159-
unless argument
155+
if argument.nil?
156+
result ||= Query::InputValidationResult.new
160157
result.add_problem("Field is not defined on #{self.graphql_name}", [argument_name])
161-
next
158+
else
159+
# Items in the input that are expected, but have invalid values
160+
argument_result = argument.type.validate_input(value, ctx)
161+
result ||= Query::InputValidationResult.new
162+
if !argument_result.valid?
163+
result.merge_result!(argument_name, argument_result)
164+
end
162165
end
163-
# Items in the input that are expected, but have invalid values
164-
argument_result = argument.type.validate_input(value, ctx)
165-
result.merge_result!(argument_name, argument_result) unless argument_result.valid?
166166
end
167167
end
168168

lib/graphql/schema/list.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ def coerce_input(value, ctx)
4646
end
4747

4848
def validate_non_null_input(value, ctx)
49-
result = GraphQL::Query::InputValidationResult.new
49+
result = nil
5050
ensure_array(value).each_with_index do |item, index|
5151
item_result = of_type.validate_input(item, ctx)
5252
if !item_result.valid?
53+
result ||= GraphQL::Query::InputValidationResult.new
5354
result.merge_result!(index, item_result)
5455
end
5556
end

lib/graphql/schema/member/validates_input.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ def valid_input?(val, ctx)
1010

1111
def validate_input(val, ctx)
1212
if val.nil?
13-
GraphQL::Query::InputValidationResult.new
13+
Query::InputValidationResult::VALID
1414
else
15-
validate_non_null_input(val, ctx)
15+
validate_non_null_input(val, ctx) || Query::InputValidationResult::VALID
1616
end
1717
end
1818

lib/graphql/schema/scalar.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ def default_scalar?
4141
end
4242

4343
def validate_non_null_input(value, ctx)
44-
result = Query::InputValidationResult.new
4544
coerced_result = begin
4645
ctx.query.with_error_handling do
4746
coerce_input(value, ctx)
@@ -56,11 +55,12 @@ def validate_non_null_input(value, ctx)
5655
else
5756
" #{GraphQL::Language.serialize(value)}"
5857
end
59-
result.add_problem("Could not coerce value#{str_value} to #{graphql_name}")
58+
Query::InputValidationResult.from_problem("Could not coerce value#{str_value} to #{graphql_name}")
6059
elsif coerced_result.is_a?(GraphQL::CoercionError)
61-
result.add_problem(coerced_result.message, message: coerced_result.message, extensions: coerced_result.extensions)
60+
Query::InputValidationResult.from_problem(coerced_result.message, message: coerced_result.message, extensions: coerced_result.extensions)
61+
else
62+
nil
6263
end
63-
result
6464
end
6565
end
6666
end

spec/graphql/query/literal_input_spec.rb

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)