Skip to content

Commit 688420f

Browse files
committed
Merge branch 'master' into additionalItems_fix
Conflicts: tests.py
2 parents 9a4a4ef + e44b0b2 commit 688420f

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

README.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,9 @@ Schema Versioning
8989
-----------------
9090

9191
JSON Schema is, at the time of this writing, seemingly at Draft 3, with
92-
preparations for Draft 4 underway. The ``Validator`` class and ``validate``
93-
function take a ``version`` argument that you can use to specify what version
94-
of the Schema you are validating under.
95-
96-
As of right now, Draft 3 (``jsonschema.DRAFT_3``) is the only supported
97-
version, and the default when validating. Whether it will remain the default
98-
version in the future when it is superceeded is undecided, so if you want to be
99-
safe, *explicitly* declare which version to use when validating.
100-
92+
preparations for Draft 4 underway. As of right now, Draft 3 is the only
93+
supported version, and the default when validating. Preparations for
94+
implementing some Draft 4 support are in progress.
10195

10296
Release Notes
10397
-------------

jsonschema.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ def validate_additionalProperties(self, aP, instance, schema):
391391
if not self.is_type(instance, "object"):
392392
return
393393

394-
# no viewkeys in <2.7, and pypy seems to fail on vk - vk anyhow, so...
395-
extras = set(instance) - set(schema.get("properties", {}))
394+
extras = set(_find_additional_properties(instance, schema))
396395

397396
if self.is_type(aP, "object"):
398397
for extra in extras:
@@ -598,6 +597,26 @@ def __repr__(self):
598597
return "<%s (%s errors)>" % (self.__class__.__name__, len(self))
599598

600599

600+
def _find_additional_properties(instance, schema):
601+
"""
602+
Return the set of additional properties for the given ``instance``.
603+
604+
Weeds out properties that should have been validated by ``properties`` and
605+
/ or ``patternProperties``.
606+
607+
Assumes ``instance`` is dict-like already.
608+
609+
"""
610+
611+
properties = schema.get("properties", {})
612+
patterns = "|".join(schema.get("patternProperties", {}))
613+
for property in instance:
614+
if property not in properties:
615+
if patterns and re.search(patterns, property):
616+
continue
617+
yield property
618+
619+
601620
def _extras_msg(extras):
602621
"""
603622
Create an error message for extra items or properties.

tests.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,25 @@ def additionalProperties(self, aP):
282282
with self.assertRaises(ValidationError):
283283
validate({"foo" : 1, "bar" : "baz", "quux" : "boom"}, schema)
284284

285-
def test_additionalProperties_ignores_nonobjects(self):
286-
validate(None, {"additionalProperties" : False})
285+
# A horrible name and horrible test. I will repent.
286+
# See https://groups.google.com/d/msg/json-schema/Q5F30CZtMb4/yk_niLnsdQ0J
287+
p_pP_aP = parametrized(
288+
("property_validates_property", "valid", {"foo" : [1, 2]}),
289+
("property_invalidates_property", "invalid", {"foo" : [1, 2, 3, 4]}),
290+
("patternProperty_invalidates_property", "invalid", {"foo" : []}),
291+
("patternProperty_validates_nonproperty", "valid", {"fxo" : [1, 2]}),
292+
("patternProperty_invalidates_nonproperty", "invalid", {"fxo" : []}),
293+
("additionalProperty_ignores_property", "valid", {"bar" : []}),
294+
("additionalProperty_validates_others", "valid", {"quux" : 3}),
295+
("additionalProperty_invalidates_others", "invalid", {"quux" : "foo"}),
296+
)(validation_test(
297+
properties={
298+
"foo" : {"type" : "array", "maxItems" : 3},
299+
"bar" : {"type" : "array"}
300+
},
301+
patternProperties={"f.o" : {"minItems" : 2}},
302+
additionalProperties={"type" : "integer"},
303+
))
287304

288305
items = parametrized(
289306
("", "valid", [1, 2, 3]),

0 commit comments

Comments
 (0)