@@ -28,18 +28,6 @@ def is_object_or_named_tuple(checker, instance):
28
28
return is_namedtuple (instance )
29
29
30
30
31
- def coerce_named_tuple (fn ):
32
- def coerced (validator , value , instance , schema ):
33
- if is_namedtuple (instance ):
34
- instance = instance ._asdict ()
35
- return fn (validator , value , instance , schema )
36
- return coerced
37
-
38
-
39
- required = coerce_named_tuple (_validators .required )
40
- properties = coerce_named_tuple (_validators .properties )
41
-
42
-
43
31
class TestTypeChecker (TestCase ):
44
32
def test_is_type (self ):
45
33
checker = TypeChecker ({"two" : equals_2 })
@@ -139,6 +127,9 @@ def int_or_str_int(checker, instance):
139
127
with self .assertRaises (ValidationError ):
140
128
validator .validate (4.4 )
141
129
130
+ with self .assertRaises (ValidationError ):
131
+ validator .validate ("foo" )
132
+
142
133
def test_object_can_be_extended (self ):
143
134
schema = {"type" : "object" }
144
135
@@ -179,6 +170,16 @@ def test_object_extensions_can_handle_custom_validators(self):
179
170
"object" , is_object_or_named_tuple ,
180
171
)
181
172
173
+ def coerce_named_tuple (fn ):
174
+ def coerced (validator , value , instance , schema ):
175
+ if is_namedtuple (instance ):
176
+ instance = instance ._asdict ()
177
+ return fn (validator , value , instance , schema )
178
+ return coerced
179
+
180
+ required = coerce_named_tuple (_validators .required )
181
+ properties = coerce_named_tuple (_validators .properties )
182
+
182
183
CustomValidator = extend (
183
184
Draft4Validator ,
184
185
type_checker = type_checker ,
@@ -194,6 +195,12 @@ def test_object_extensions_can_handle_custom_validators(self):
194
195
with self .assertRaises (ValidationError ):
195
196
validator .validate (Point (x = "not an integer" , y = 5 ))
196
197
198
+ # As well as still handle objects.
199
+ validator .validate ({"x" : 4 , "y" : 5 })
200
+
201
+ with self .assertRaises (ValidationError ):
202
+ validator .validate ({"x" : "not an integer" , "y" : 5 })
203
+
197
204
def test_unknown_type (self ):
198
205
with self .assertRaises (UnknownType ) as e :
199
206
Draft4Validator ({}).is_type (12 , "some unknown type" )
0 commit comments