1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::ast;
use crate::coordinate::TypeAttributeCoordinate;
use crate::parser::SourceSpan;
use crate::schema;
use crate::validation::diagnostics::DiagnosticData;
use crate::validation::variable::is_variable_usage_allowed;
use crate::validation::DiagnosticList;
use crate::Name;
use crate::Node;
/// The Argument, ObjectField, or other position into which a value (or nested value)
/// is being written. Threaded through recursive value validation so the Variable arm
/// can apply spec rule 5.8.5 against the *innermost* position, and emit a
/// `DisallowedVariableUsage` diagnostic that names that position.
#[derive(Clone, Copy)]
pub(crate) struct VariablePosition<'a> {
/// Position name — argument name at the top level, field name when recursing into
/// an input object.
name: &'a Name,
/// Source location of the position's definition.
location: Option<SourceSpan>,
/// Whether the position declares a default value, per spec rule 5.8.5 step 3.b.
has_default: bool,
}
fn unsupported_type(
diagnostics: &mut DiagnosticList,
value: &Node<ast::Value>,
declared_type: &Node<ast::Type>,
) {
diagnostics.push(
value.location(),
DiagnosticData::UnsupportedValueType {
ty: declared_type.clone(),
value: value.clone(),
definition_location: declared_type.location(),
},
)
}
pub(crate) fn validate_values(
diagnostics: &mut DiagnosticList,
schema: &crate::Schema,
arg_definition: &Node<ast::InputValueDefinition>,
argument: &Node<ast::Argument>,
var_defs: &[Node<ast::VariableDefinition>],
) {
let position = VariablePosition {
name: &arg_definition.name,
location: arg_definition.location(),
has_default: arg_definition.default_value.is_some(),
};
value_of_correct_type(
diagnostics,
schema,
&arg_definition.ty,
&argument.value,
var_defs,
Some(position),
);
}
pub(crate) fn value_of_correct_type(
diagnostics: &mut DiagnosticList,
schema: &crate::Schema,
ty: &Node<ast::Type>,
arg_value: &Node<ast::Value>,
var_defs: &[Node<ast::VariableDefinition>],
position: Option<VariablePosition<'_>>,
) {
let Some(type_definition) = schema.types.get(ty.inner_named_type()) else {
return;
};
match &**arg_value {
// When expected as an input type, only integer input values are
// accepted. All other input values, including strings with numeric
// content, must raise a request error indicating an incorrect
// type. If the integer input value represents a value less than
// -2^31 or greater than or equal to 2^31, a request error should be
// raised.
// When expected as an input type, any string (such as "4") or
// integer (such as 4 or -4) input value should be coerced to ID
ast::Value::Int(int) => match &type_definition {
// Any value is valid for a custom scalar.
schema::ExtendedType::Scalar(scalar) if !scalar.is_built_in() => {}
schema::ExtendedType::Scalar(scalar) => match scalar.name.as_str() {
// Any integer sequence is valid for an ID.
"ID" => {}
"Int" => {
if int.try_to_i32().is_err() {
diagnostics.push(
arg_value.location(),
DiagnosticData::IntCoercionError {
value: int.as_str().to_owned(),
},
)
}
}
"Float" => {
if int.try_to_f64().is_err() {
diagnostics.push(
arg_value.location(),
DiagnosticData::FloatCoercionError {
value: int.as_str().to_owned(),
},
)
}
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
_ => unsupported_type(diagnostics, arg_value, ty),
},
// When expected as an input type, both integer and float input
// values are accepted. All other input values, including strings
// with numeric content, must raise a request error indicating an
// incorrect type.
ast::Value::Float(float) => match &type_definition {
// Any value is valid for a custom scalar.
schema::ExtendedType::Scalar(scalar) if !scalar.is_built_in() => {}
schema::ExtendedType::Scalar(scalar) if scalar.name == "Float" => {
if float.try_to_f64().is_err() {
diagnostics.push(
arg_value.location(),
DiagnosticData::FloatCoercionError {
value: float.as_str().to_owned(),
},
)
}
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
// When expected as an input type, only valid Unicode string input
// values are accepted. All other input values must raise a request
// error indicating an incorrect type.
// When expected as an input type, any string (such as "4") or
// integer (such as 4 or -4) input value should be coerced to ID
ast::Value::String(_) => match &type_definition {
schema::ExtendedType::Scalar(scalar) => {
// specifically return diagnostics for ints, floats, and
// booleans.
// string, ids and custom scalars are ok, and
// don't need a diagnostic.
if scalar.is_built_in() && !matches!(scalar.name.as_str(), "String" | "ID") {
unsupported_type(diagnostics, arg_value, ty);
}
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
// When expected as an input type, only boolean input values are
// accepted. All other input values must raise a request error
// indicating an incorrect type.
ast::Value::Boolean(_) => match &type_definition {
schema::ExtendedType::Scalar(scalar) => {
if scalar.is_built_in() && scalar.name.as_str() != "Boolean" {
unsupported_type(diagnostics, arg_value, ty);
}
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
ast::Value::Null => {
if ty.is_non_null() {
unsupported_type(diagnostics, arg_value, ty);
}
}
ast::Value::Variable(var_name) => {
if let Some(var_def) = var_defs.iter().find(|v| v.name == *var_name) {
let location_has_default = position.is_some_and(|p| p.has_default);
if !is_variable_usage_allowed(var_def, ty, location_has_default) {
if let Some(pos) = position {
diagnostics.push(
arg_value.location(),
DiagnosticData::DisallowedVariableUsage {
variable: var_name.clone(),
variable_type: (*var_def.ty).clone(),
variable_location: var_def.location(),
argument: pos.name.clone(),
argument_type: (**ty).clone(),
argument_location: pos.location,
},
);
} else {
unsupported_type(diagnostics, arg_value, ty);
}
}
} else {
diagnostics.push(
arg_value.location(),
DiagnosticData::UndefinedVariable {
name: var_name.clone(),
},
);
}
}
ast::Value::Enum(value) => match &type_definition {
schema::ExtendedType::Scalar(scalar) if !scalar.is_built_in() => {
// Accept enum values as input for custom scalars
}
schema::ExtendedType::Enum(enum_) => {
if !enum_.values.contains_key(value) {
diagnostics.push(
value.location(),
DiagnosticData::UndefinedEnumValue {
value: value.clone(),
definition: enum_.name.clone(),
definition_location: enum_.location(),
},
);
}
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
// When expected as an input, list values are accepted only when
// each item in the list can be accepted by the list’s item type.
//
// If the value passed as an input to a list type is not a list and
// not the null value, then the result of input coercion is a list
// of size one, where the single item value is the result of input
// coercion for the list’s item type on the provided value (note
// this may apply recursively for nested lists).
ast::Value::List(li) => {
let accepts_list = ty.is_list()
// A named type can still accept a list if it is a custom scalar.
|| matches!(type_definition, schema::ExtendedType::Scalar(scalar) if !scalar.is_built_in());
if !accepts_list {
unsupported_type(diagnostics, arg_value, ty)
} else {
let item_type = ty.same_location(ty.item_type().clone());
if type_definition.is_input_type() {
for v in li {
// Per spec rule 5.8.5, a list value entry's location_ty is the
// item type — but it carries no default of its own. Inherit the
// enclosing position's name/location so a nested variable error
// still names the surrounding argument or field.
let item_position = position.map(|p| VariablePosition {
has_default: false,
..p
});
value_of_correct_type(
diagnostics,
schema,
&item_type,
v,
var_defs,
item_position,
);
}
} else {
unsupported_type(diagnostics, arg_value, &item_type);
}
}
}
ast::Value::Object(obj) => match &type_definition {
schema::ExtendedType::Scalar(scalar) if !scalar.is_built_in() => {}
schema::ExtendedType::InputObject(input_obj) => {
let undefined_field = obj
.iter()
.find(|(name, ..)| !input_obj.fields.contains_key(name));
// Add a diagnostic if a value does not exist on the input
// object type
if let Some((name, value)) = undefined_field {
diagnostics.push(
value.location(),
DiagnosticData::UndefinedInputValue {
value: name.clone(),
definition: input_obj.name.clone(),
definition_location: input_obj.location(),
},
);
}
// @oneOf: exactly one key must be provided.
// https://spec.graphql.org/September2025/#sec-OneOf-Input-Objects
//
// Per-field non-null / nullable-variable checks fall out of the recursion
// below — @oneOf field positions are treated as non-null (coerced when
// recursing), so the generic Null and Variable arms catch the violations.
if input_obj.is_one_of() && obj.len() != 1 {
diagnostics.push(
arg_value.location(),
DiagnosticData::OneOfInputObjectFieldCount {
name: input_obj.name.clone(),
provided_fields: obj
.iter()
.map(|(name, _)| (name.clone(), name.location()))
.collect(),
},
);
}
input_obj.fields.iter().for_each(|(input_name, f)| {
let ty = &f.ty;
let is_missing = !obj.iter().any(|(value_name, ..)| input_name == value_name);
let is_null = obj
.iter()
.any(|(name, value)| input_name == name && value.is_null());
// If the input object field type is non_null, and no
// default value is provided, or if the value provided
// is null or missing entirely, an error should be
// raised.
if (ty.is_non_null() && f.default_value.is_none()) && (is_missing || is_null) {
diagnostics.push(
arg_value.location(),
DiagnosticData::RequiredField {
name: input_name.clone(),
coordinate: TypeAttributeCoordinate {
ty: input_obj.name.clone(),
attribute: input_name.clone(),
},
expected_type: ty.clone(),
definition_location: f.location(),
},
);
}
let used_val = obj.iter().find(|(obj_name, ..)| obj_name == input_name);
if let Some((_, v)) = used_val {
let field_position = VariablePosition {
name: input_name,
location: f.location(),
has_default: f.default_value.is_some(),
};
// @oneOf: coerce the field's declared nullable type to non-null
// so the generic Null and Variable arms reject `null` literals
// and nullable variables at this position.
let coerced;
let effective_ty = if input_obj.is_one_of() && !ty.is_non_null() {
coerced = ty.same_location(ty.as_ref().clone().non_null());
&coerced
} else {
ty
};
value_of_correct_type(
diagnostics,
schema,
effective_ty,
v,
var_defs,
Some(field_position),
);
}
})
}
_ => unsupported_type(diagnostics, arg_value, ty),
},
}
}