Skip to content

Commit e5788a6

Browse files
authored
Merge pull request #155 from cwacek/feature/fix-array-refs
bugfix: Make sure array validations happen for references items too
2 parents 9b2251a + 1975663 commit e5788a6

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

python_jsonschema_objects/classbuilder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ def _build_object(self, nm, clsdata, parents,**kw):
633633
'type': 'array',
634634
'validator': python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
635635
uri,
636-
item_constraint=typ)}
636+
item_constraint=typ,
637+
**detail)}
637638
else:
638639
uri = "{0}/{1}_{2}".format(nm,
639640
prop, "<anonymous_field>")

test/test_array_validation.py

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import pytest
32

43
import python_jsonschema_objects as pjo
@@ -10,27 +9,64 @@ def arrayClass():
109
"title": "ArrayVal",
1110
"type": "object",
1211
"properties": {
13-
"min": {"type": "array", "items": {"type": "string"}, "default": [], "minItems": 1},
14-
"max": {"type": "array", "items": {"type": "string"}, "default": [], "maxItems": 1},
15-
"both": {"type": "array", "items": {"type": "string"}, "default": [], "maxItems": 2, "minItems": 1},
16-
"unique": {"type": "array", "items": {"type": "string"}, "default": [], "uniqueItems": True}
12+
"min": {
13+
"type": "array",
14+
"items": {"type": "string"},
15+
"default": [],
16+
"minItems": 1,
17+
},
18+
"max": {
19+
"type": "array",
20+
"items": {"type": "string"},
21+
"default": [],
22+
"maxItems": 1,
23+
},
24+
"both": {
25+
"type": "array",
26+
"items": {"type": "string"},
27+
"default": [],
28+
"maxItems": 2,
29+
"minItems": 1,
30+
},
31+
"unique": {
32+
"type": "array",
33+
"items": {"type": "string"},
34+
"default": [],
35+
"uniqueItems": True,
36+
},
37+
"reffed": {
38+
"type": "array",
39+
"items": {"$ref": "#/definitions/myref"},
40+
"minItems": 1,
41+
},
1742
},
43+
"definitions": {"myref": {"type": "string"}},
1844
}
1945

2046
ns = pjo.ObjectBuilder(schema).build_classes()
21-
return ns['Arrayval'](min=["1"], both=["1"])
47+
return ns["Arrayval"](min=["1"], both=["1"])
48+
49+
50+
def test_validators_work_with_reference(arrayClass):
51+
arrayClass.reffed = ["foo"]
52+
53+
with pytest.raises(pjo.ValidationError):
54+
arrayClass.reffed = []
2255

2356

2457
def test_array_length_validates(markdown_examples):
2558

2659
builder = pjo.ObjectBuilder(
27-
markdown_examples['Example Schema'],
28-
resolved=markdown_examples)
60+
markdown_examples["Example Schema"], resolved=markdown_examples
61+
)
2962
ns = builder.build_classes()
3063

3164
with pytest.raises(pjo.ValidationError):
32-
ns.ExampleSchema(firstName="Fred", lastName="Huckstable",
33-
dogs=["Fido", "Spot", "Jasper", "Lady", "Tramp"])
65+
ns.ExampleSchema(
66+
firstName="Fred",
67+
lastName="Huckstable",
68+
dogs=["Fido", "Spot", "Jasper", "Lady", "Tramp"],
69+
)
3470

3571

3672
def test_minitems(arrayClass):

0 commit comments

Comments
 (0)