Skip to content

Commit

Permalink
Merge pull request #155 from cwacek/feature/fix-array-refs
Browse files Browse the repository at this point in the history
bugfix: Make sure array validations happen for references items too
  • Loading branch information
cwacek authored Jan 11, 2019
2 parents 9b2251a + 1975663 commit e5788a6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
3 changes: 2 additions & 1 deletion python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ def _build_object(self, nm, clsdata, parents,**kw):
'type': 'array',
'validator': python_jsonschema_objects.wrapper_types.ArrayWrapper.create(
uri,
item_constraint=typ)}
item_constraint=typ,
**detail)}
else:
uri = "{0}/{1}_{2}".format(nm,
prop, "<anonymous_field>")
Expand Down
56 changes: 46 additions & 10 deletions test/test_array_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import pytest

import python_jsonschema_objects as pjo
Expand All @@ -10,27 +9,64 @@ def arrayClass():
"title": "ArrayVal",
"type": "object",
"properties": {
"min": {"type": "array", "items": {"type": "string"}, "default": [], "minItems": 1},
"max": {"type": "array", "items": {"type": "string"}, "default": [], "maxItems": 1},
"both": {"type": "array", "items": {"type": "string"}, "default": [], "maxItems": 2, "minItems": 1},
"unique": {"type": "array", "items": {"type": "string"}, "default": [], "uniqueItems": True}
"min": {
"type": "array",
"items": {"type": "string"},
"default": [],
"minItems": 1,
},
"max": {
"type": "array",
"items": {"type": "string"},
"default": [],
"maxItems": 1,
},
"both": {
"type": "array",
"items": {"type": "string"},
"default": [],
"maxItems": 2,
"minItems": 1,
},
"unique": {
"type": "array",
"items": {"type": "string"},
"default": [],
"uniqueItems": True,
},
"reffed": {
"type": "array",
"items": {"$ref": "#/definitions/myref"},
"minItems": 1,
},
},
"definitions": {"myref": {"type": "string"}},
}

ns = pjo.ObjectBuilder(schema).build_classes()
return ns['Arrayval'](min=["1"], both=["1"])
return ns["Arrayval"](min=["1"], both=["1"])


def test_validators_work_with_reference(arrayClass):
arrayClass.reffed = ["foo"]

with pytest.raises(pjo.ValidationError):
arrayClass.reffed = []


def test_array_length_validates(markdown_examples):

builder = pjo.ObjectBuilder(
markdown_examples['Example Schema'],
resolved=markdown_examples)
markdown_examples["Example Schema"], resolved=markdown_examples
)
ns = builder.build_classes()

with pytest.raises(pjo.ValidationError):
ns.ExampleSchema(firstName="Fred", lastName="Huckstable",
dogs=["Fido", "Spot", "Jasper", "Lady", "Tramp"])
ns.ExampleSchema(
firstName="Fred",
lastName="Huckstable",
dogs=["Fido", "Spot", "Jasper", "Lady", "Tramp"],
)


def test_minitems(arrayClass):
Expand Down

0 comments on commit e5788a6

Please sign in to comment.