Skip to content

Commit f571a54

Browse files
committed
Fixed any to accept any type, including unknown ones.
Closes #47
1 parent 59cfa19 commit f571a54

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

jsonschema.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ def __init__(self, schema, types=(), resolver=None):
138138

139139
self._types = dict(self.DEFAULT_TYPES)
140140
self._types.update(types)
141-
self._types["any"] = tuple(self._types.values())
142141

143142
if resolver is None:
144143
resolver = RefResolver.from_schema(schema)
@@ -152,7 +151,9 @@ def is_type(self, instance, type):
152151
153152
"""
154153

155-
if type not in self._types:
154+
if type == "any":
155+
return True
156+
elif type not in self._types:
156157
raise UnknownType(type)
157158
type = self._types[type]
158159

@@ -232,11 +233,12 @@ def validate_type(self, types, instance, schema):
232233
types = _list(types)
233234

234235
for type in types:
236+
if type == "any" or (
237+
235238
# Ouch. Brain hurts. Two paths here, either we have a schema, then
236239
# check if the instance is valid under it
237-
if ((
238-
self.is_type(type, "object") and
239-
self.is_valid(instance, type)
240+
241+
self.is_type(type, "object") and self.is_valid(instance, type)
240242

241243
# Or we have a type as a string, just check if the instance is that
242244
# type. Also, HACK: we can reach the `or` here if skip_types is
@@ -245,7 +247,7 @@ def validate_type(self, types, instance, schema):
245247
) or (
246248
self.is_type(type, "string") and
247249
(self.is_type(instance, type) or type not in self._types)
248-
)):
250+
):
249251
return
250252
else:
251253
yield ValidationError(_types_msg(instance, types))

tests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ def test_it_can_validate_with_decimals(self):
8686
validator.validate(invalid)
8787

8888

89+
class AnyTypeMixin(object):
90+
def test_any_type_is_valid_for_type_any(self):
91+
validator = self.validator_class({"type" : "any"})
92+
validator.validate(mock.Mock())
93+
94+
8995
@load_json_cases(os.path.join(os.path.dirname(__file__), "json/tests/draft3/"))
90-
class TestDraft3(TestCase, ByteStringMixin, DecimalMixin):
96+
class TestDraft3(TestCase, ByteStringMixin, DecimalMixin, AnyTypeMixin):
9197
validator_class = Draft3Validator
9298

9399
# TODO: we're in need of more meta schema tests
@@ -355,6 +361,9 @@ def test_is_type_is_true_for_valid_type(self):
355361
def test_is_type_is_false_for_invalid_type(self):
356362
self.assertFalse(self.validator.is_type("foo", "array"))
357363

364+
def test_is_type_is_true_for_any_type(self):
365+
self.assertTrue(self.validator.is_type(mock.Mock(), "any"))
366+
358367
def test_is_type_evades_bool_inheriting_from_int(self):
359368
self.assertFalse(self.validator.is_type(True, "integer"))
360369
self.assertFalse(self.validator.is_type(True, "number"))

0 commit comments

Comments
 (0)