3
3
* accepted for that type. This is primarily useful for validating the
4
4
* runtime values of query variables.
5
5
*/
6
- func isValidValue ( value: Map , type: GraphQLInputType ) throws -> [ String ] {
6
+ func validate ( value: Map , forType type: GraphQLInputType ) throws -> [ String ] {
7
7
// A value must be provided if the type is non-null.
8
- if let type = type as? GraphQLNonNull {
9
- if value == . null {
10
- if let namedType = type. ofType as? GraphQLNamedType {
11
- return [ " Expected \" \( namedType . name ) ! \" , found null. " ]
12
- }
13
-
8
+ if let nonNullType = type as? GraphQLNonNull {
9
+ guard let wrappedType = nonNullType . ofType as? GraphQLInputType else {
10
+ throw GraphQLError ( message : " Input non-null type must wrap another input type " )
11
+ }
12
+
13
+ if value == . null {
14
14
return [ " Expected non-null value, found null. " ]
15
15
}
16
+ if value == . undefined {
17
+ return [ " Expected non-null value was not provided. " ]
18
+ }
16
19
17
- return try isValidValue ( value: value, type : type . ofType as! GraphQLInputType )
20
+ return try validate ( value: value, forType : wrappedType )
18
21
}
19
-
20
- guard value != . null else {
22
+
23
+ // If nullable, either null or undefined are allowed
24
+ guard value != . null && value != . undefined else {
21
25
return [ ]
22
26
}
23
27
24
28
// Lists accept a non-list value as a list of one.
25
- if let type = type as? GraphQLList {
26
- let itemType = type. ofType
29
+ if let listType = type as? GraphQLList {
30
+ guard let itemType = listType. ofType as? GraphQLInputType else {
31
+ throw GraphQLError ( message: " Input list type must wrap another input type " )
32
+ }
27
33
28
34
if case . array( let values) = value {
29
35
var errors : [ String ] = [ ]
30
36
31
37
for (index, item) in values. enumerated ( ) {
32
- let e = try isValidValue ( value: item, type : itemType as! GraphQLInputType ) . map {
38
+ let e = try validate ( value: item, forType : itemType) . map {
33
39
" In element # \( index) : \( $0) "
34
40
}
35
41
errors. append ( contentsOf: e)
@@ -38,16 +44,16 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
38
44
return errors
39
45
}
40
46
41
- return try isValidValue ( value: value, type : itemType as! GraphQLInputType )
47
+ return try validate ( value: value, forType : itemType)
42
48
}
43
49
44
50
// Input objects check each defined field.
45
- if let type = type as? GraphQLInputObjectType {
51
+ if let objectType = type as? GraphQLInputObjectType {
46
52
guard case . dictionary( let dictionary) = value else {
47
- return [ " Expected \" \( type . name) \" , found not an object. " ]
53
+ return [ " Expected \" \( objectType . name) \" , found not an object. " ]
48
54
}
49
55
50
- let fields = type . fields
56
+ let fields = objectType . fields
51
57
var errors : [ String ] = [ ]
52
58
53
59
// Ensure every provided field is defined.
@@ -59,7 +65,7 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
59
65
60
66
// Ensure every defined field is valid.
61
67
for (fieldName, field) in fields {
62
- let newErrors = try isValidValue ( value: value [ fieldName] , type : field. type) . map {
68
+ let newErrors = try validate ( value: value [ fieldName] , forType : field. type) . map {
63
69
" In field \" \( fieldName) \" : \( $0) "
64
70
}
65
71
@@ -69,20 +75,20 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
69
75
return errors
70
76
}
71
77
72
- guard let type = type as? GraphQLLeafType else {
73
- fatalError ( " Must be input type" )
74
- }
75
-
76
- // Scalar/Enum input checks to ensure the type can parse the value to
77
- // a non- null value.
78
- do {
79
- let parseResult = try type . parseValue ( value : value )
80
- if parseResult == . null {
81
- return [ " Expected type \" \( type . name) \" , found \( value) . " ]
78
+ if let leafType = type as? GraphQLLeafType {
79
+ // Scalar/Enum input checks to ensure the type can parse the value to
80
+ // a non-null value.
81
+ do {
82
+ let parseResult = try leafType . parseValue ( value: value )
83
+ if parseResult == . null || parseResult == . undefined {
84
+ return [ " Expected type \" \( leafType . name ) \" , found \( value ) . " ]
85
+ }
86
+ } catch {
87
+ return [ " Expected type \" \( leafType . name) \" , found \( value) . " ]
82
88
}
83
- } catch {
84
- return [ " Expected type \" \( type . name ) \" , found \( value ) . " ]
89
+
90
+ return [ ]
85
91
}
86
92
87
- return [ ]
93
+ throw GraphQLError ( message : " Provided type was not provided " )
88
94
}
0 commit comments