1
1
local types = require (' graphql.core.types' )
2
2
local graphql_utils = require (' graphql.utils' )
3
+ local graphql_error_codes = require (' graphql.error_codes' )
3
4
4
5
local check = graphql_utils .check
6
+ local e = graphql_error_codes
5
7
6
8
local validate_variables = {}
7
9
@@ -14,8 +16,10 @@ local function checkVariableValue(variableName, value, variableType)
14
16
15
17
if isNonNull then
16
18
variableType = types .nullable (variableType )
17
- assert (value ~= nil ,
18
- (' Variable "%s" expected to be non-null' ):format (variableName ))
19
+ if value == nil then
20
+ error (e .wrong_value ((' Variable "%s" expected to be non-null' ):format (
21
+ variableName )))
22
+ end
19
23
end
20
24
21
25
local isList = variableType .__type == ' List'
@@ -29,12 +33,14 @@ local function checkVariableValue(variableName, value, variableType)
29
33
if value == nil then return end
30
34
31
35
if isList then
32
- assert (type (value ) == ' table' ,
33
- (' Variable "%s" for a List must be a Lua table, got %s' )
34
- :format (variableName , type (value )))
35
- assert (graphql_utils .is_array (value ),
36
- (' Variable "%s" for a List must be an array, got map' )
37
- :format (variableName ))
36
+ if type (value ) ~= ' table' then
37
+ error (e .wrong_value ((' Variable "%s" for a List must be a Lua ' ..
38
+ ' table, got %s' ):format (variableName , type (value ))))
39
+ end
40
+ if not graphql_utils .is_array (value ) then
41
+ error (e .wrong_value ((' Variable "%s" for a List must be an array, ' ..
42
+ ' got map' ):format (variableName )))
43
+ end
38
44
assert (variableType .ofType ~= nil , ' variableType.ofType must not be nil' )
39
45
for i , item in ipairs (value ) do
40
46
local itemName = variableName .. ' [' .. tostring (i ) .. ' ]'
@@ -44,9 +50,11 @@ local function checkVariableValue(variableName, value, variableType)
44
50
end
45
51
46
52
if isInputObject then
47
- assert (type (value ) == ' table' ,
48
- (' Variable "%s" for the InputObject "%s" must be a Lua table, ' ..
49
- ' got %s' ):format (variableName , variableType .name , type (value )))
53
+ if type (value ) ~= ' table' then
54
+ error (e .wrong_value ((' Variable "%s" for the InputObject "%s" must ' ..
55
+ ' be a Lua table, got %s' ):format (variableName , variableType .name ,
56
+ type (value ))))
57
+ end
50
58
51
59
-- check all fields: as from value as well as from schema
52
60
local fieldNameSet = {}
@@ -59,13 +67,16 @@ local function checkVariableValue(variableName, value, variableType)
59
67
60
68
for fieldName , _ in pairs (fieldNameSet ) do
61
69
local fieldValue = value [fieldName ]
62
- assert (type (fieldName ) == ' string' ,
63
- (' Field key of the variable "%s" for the InputObject "%s" ' ..
64
- ' must be a string, got %s' ):format (variableName , variableType .name ,
65
- type (fieldName )))
66
- assert (type (variableType .fields [fieldName ]) ~= ' nil' ,
67
- (' Unknown field "%s" of the variable "%s" for the ' ..
68
- ' InputObject "%s"' ):format (fieldName , variableName , variableType .name ))
70
+ if type (fieldName ) ~= ' string' then
71
+ error (e .wrong_value ((' Field key of the variable "%s" for the ' ..
72
+ ' InputObject "%s" must be a string, got %s' ):format (variableName ,
73
+ variableType .name , type (fieldName ))))
74
+ end
75
+ if type (variableType .fields [fieldName ]) == ' nil' then
76
+ error (e .wrong_value ((' Unknown field "%s" of the variable "%s" ' ..
77
+ ' for the InputObject "%s"' ):format (fieldName , variableName ,
78
+ variableType .name )))
79
+ end
69
80
70
81
local childType = variableType .fields [fieldName ].kind
71
82
local childName = variableName .. ' .' .. fieldName
@@ -76,15 +87,18 @@ local function checkVariableValue(variableName, value, variableType)
76
87
end
77
88
78
89
if isInputMap then
79
- assert (type (value ) == ' table' ,
80
- (' Variable "%s" for the InputMap "%s" must be a Lua table, got %s' )
81
- :format (variableName , variableType .name , type (value )))
90
+ if type (value ) ~= ' table' then
91
+ error (e .wrong_value ((' Variable "%s" for the InputMap "%s" must be a ' ..
92
+ ' Lua table, got %s' ):format (variableName , variableType .name ,
93
+ type (value ))))
94
+ end
82
95
83
96
for fieldName , fieldValue in pairs (value ) do
84
- assert (type (fieldName ) == ' string' ,
85
- (' Field key of the variable "%s" for the InputMap "%s" must be a ' ..
86
- ' string, got %s' ):format (variableName , variableType .name ,
87
- type (fieldName )))
97
+ if type (fieldName ) ~= ' string' then
98
+ error (e .wrong_value ((' Field key of the variable "%s" for the ' ..
99
+ ' InputMap "%s" must be a string, got %s' ):format (variableName ,
100
+ variableType .name , type (fieldName ))))
101
+ end
88
102
local childType = variableType .values
89
103
local childName = variableName .. ' .' .. fieldName
90
104
checkVariableValue (childName , fieldValue , childType )
@@ -103,9 +117,10 @@ local function checkVariableValue(variableName, value, variableType)
103
117
104
118
if isScalar then
105
119
check (variableType .isValueOfTheType , ' isValueOfTheType' , ' function' )
106
- assert (variableType .isValueOfTheType (value ),
107
- (' Wrong variable "%s" for the Scalar "%s"' ):format (
108
- variableName , variableType .name ))
120
+ if not variableType .isValueOfTheType (value ) then
121
+ error (e .wrong_value ((' Wrong variable "%s" for the Scalar "%s"' ):format (
122
+ variableName , variableType .name )))
123
+ end
109
124
return
110
125
end
111
126
115
130
function validate_variables .validate_variables (context )
116
131
-- check that all variable values have corresponding variable declaration
117
132
for variableName , _ in pairs (context .variables or {}) do
118
- assert (context .variableTypes [variableName ] ~= nil ,
119
- (' There is no declaration for the variable "%s"' ):format (variableName ))
133
+ if context .variableTypes [variableName ] == nil then
134
+ error (e .wrong_value ((' There is no declaration for the variable "%s"' )
135
+ :format (variableName )))
136
+ end
120
137
end
121
138
122
139
-- check that variable values have correct type
0 commit comments