33 * accepted for that type. This is primarily useful for validating the
44 * runtime values of query variables.
55 */
6- func isValidValue ( value: Map , type: GraphQLInputType ) throws -> [ String ] {
6+ func validate ( value: Map , forType type: GraphQLInputType ) throws -> [ String ] {
77 // 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 {
1414 return [ " Expected non-null value, found null. " ]
1515 }
16+ if value == . undefined {
17+ return [ " Expected non-null value was not provided. " ]
18+ }
1619
17- return try isValidValue ( value: value, type : type . ofType as! GraphQLInputType )
20+ return try validate ( value: value, forType : wrappedType )
1821 }
19-
20- guard value != . null else {
22+
23+ // If nullable, either null or undefined are allowed
24+ guard value != . null && value != . undefined else {
2125 return [ ]
2226 }
2327
2428 // 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+ }
2733
2834 if case . array( let values) = value {
2935 var errors : [ String ] = [ ]
3036
3137 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 {
3339 " In element # \( index) : \( $0) "
3440 }
3541 errors. append ( contentsOf: e)
@@ -38,16 +44,16 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
3844 return errors
3945 }
4046
41- return try isValidValue ( value: value, type : itemType as! GraphQLInputType )
47+ return try validate ( value: value, forType : itemType)
4248 }
4349
4450 // Input objects check each defined field.
45- if let type = type as? GraphQLInputObjectType {
51+ if let objectType = type as? GraphQLInputObjectType {
4652 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. " ]
4854 }
4955
50- let fields = type . fields
56+ let fields = objectType . fields
5157 var errors : [ String ] = [ ]
5258
5359 // Ensure every provided field is defined.
@@ -59,7 +65,7 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
5965
6066 // Ensure every defined field is valid.
6167 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 {
6369 " In field \" \( fieldName) \" : \( $0) "
6470 }
6571
@@ -69,20 +75,20 @@ func isValidValue(value: Map, type: GraphQLInputType) throws -> [String] {
6975 return errors
7076 }
7177
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) . " ]
8288 }
83- } catch {
84- return [ " Expected type \" \( type . name ) \" , found \( value ) . " ]
89+
90+ return [ ]
8591 }
8692
87- return [ ]
93+ throw GraphQLError ( message : " Provided type was not provided " )
8894}
0 commit comments