Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7030485

Browse files
committed
Added basic extends support. forthcoming (I hope)
1 parent e169504 commit 7030485

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

jsonschema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
--------------
1313
1414
* ``format``
15-
* ``extends``
16-
* ``$ref``
15+
* ``$ref`` (and, ergo, using ``extends`` with a ``$ref``)
1716
* ``$schema``
1817
1918
"""
@@ -411,6 +410,12 @@ def validate_disallow(self, disallow, instance, schema):
411410
u"%r is disallowed for %r" % (_delist(disallow), instance)
412411
)
413412

413+
def validate_extends(self, extends, instance, schema):
414+
if self._is_type(extends, "object"):
415+
extends = [extends]
416+
for subschema in extends:
417+
self._validate(instance, subschema)
418+
414419

415420
def _extras_msg(extras):
416421
"""

tests.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,56 @@ def divisibleBy(self, expect, instance, dB):
528528
disallow=[u"string", {"properties" : {u"foo" : {u"type" : u"string"}}}]
529529
))
530530

531+
@parametrized(
532+
("", "valid", {"foo" : "baz", "bar" : 2}),
533+
("mismatch_extends", "invalid", {"foo" : "baz"}),
534+
("mismatch_extended", "invalid", {"bar" : 2}),
535+
("wrong_type", "invalid", {"foo" : "baz", "bar" : "quux"}),
536+
)
537+
def extends(self, expect, instance):
538+
schema = {
539+
"properties" : {"bar" : {"type" : "integer", "required" : True}},
540+
"extends" : {
541+
"properties" : {
542+
"foo" : {"type" : "string", "required" : True},
543+
}
544+
},
545+
}
546+
547+
test = validation_test(**schema)
548+
test(self, expect, instance)
549+
550+
@parametrized(
551+
("", "valid", {"foo" : "quux", "bar" : 2, "baz" : None}),
552+
("mismatch_first_extends", "invalid", {"bar" : 2, "baz" : None}),
553+
("mismatch_second_extends", "invalid", {"foo" : "quux", "bar" : 2}),
554+
("mismatch_both", "invalid", {"bar" : 2}),
555+
)
556+
def multiple_extends(self, expect, instance):
557+
schema = {
558+
"properties" : {"bar" : {"type" : "integer", "required" : True}},
559+
"extends" : [
560+
{
561+
"properties" : {
562+
"foo" : {"type" : "string", "required" : True},
563+
}
564+
},
565+
{
566+
"properties" : {
567+
"baz" : {"type" : "null", "required" : True},
568+
}
569+
},
570+
],
571+
}
572+
573+
test = validation_test(**schema)
574+
test(self, expect, instance)
575+
576+
extends_simple_types = parametrized(
577+
("", "valid", 25),
578+
("mismatch_extends", "invalid", 35)
579+
)(validation_test(minimum=20, extends={"maximum" : 30}))
580+
531581
def test_stop_on_error(self):
532582
instance = [1, 2]
533583

0 commit comments

Comments
 (0)