Skip to content

Commit aa4c915

Browse files
authored
Merge pull request #60 from blr246/boolean-in-child-object
Support boolean in schema child object.
2 parents 088932b + 55c063c commit aa4c915

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

python_jsonschema_objects/classbuilder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,9 @@ def setprop(self, val):
686686
if not isinstance(typ, dict):
687687
type_checks.append(typ)
688688
continue
689-
typ = validators.SCHEMA_TYPE_MAPPING[typ['type']]
689+
typ = next(t
690+
for n, t in validators.SCHEMA_TYPE_MAPPING
691+
if typ['type'] == t)
690692
if typ is None:
691693
typ = type(None)
692694
if isinstance(typ, (list, tuple)):

python_jsonschema_objects/pattern_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def instantiate(self, name, val):
9191
if self._additional_type is True:
9292

9393
valtype = [k for k, t
94-
in six.iteritems(validators.SCHEMA_TYPE_MAPPING)
94+
in validators.SCHEMA_TYPE_MAPPING
9595
if t is not None and isinstance(val, t)]
9696
valtype = valtype[0]
9797
return cb.MakeLiteral(name, valtype, val)

python_jsonschema_objects/validators.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import logging
55
logger = logging.getLogger(__name__)
66

7-
SCHEMA_TYPE_MAPPING = {
8-
'array': list,
9-
'boolean': bool,
10-
'integer': six.integer_types,
11-
'number': six.integer_types + (float,),
12-
'null': type(None),
13-
'string': six.string_types,
14-
'object': dict
15-
}
7+
SCHEMA_TYPE_MAPPING = (
8+
('array', list),
9+
('boolean', bool),
10+
('integer', six.integer_types),
11+
('number', six.integer_types + (float,)),
12+
('null', type(None)),
13+
('string', six.string_types),
14+
('object', dict),
15+
)
16+
"""Sequence of schema type mappings to be checked in precedence order."""
1617

1718

1819
class ValidationError(Exception):

test/test_pytest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,21 @@ def test_strict_mode():
431431
ns = builder.build_classes(strict=True)
432432
NameData = ns.NameData
433433
NameData(lastName="hello")
434+
435+
436+
def test_boolean_in_child_object():
437+
schema = {
438+
"$schema": "http://json-schema.org/schema#",
439+
"id": "test",
440+
"type": "object",
441+
"properties": {
442+
"data": {
443+
"type": "object",
444+
"additionalProperties": True
445+
}
446+
}
447+
}
448+
builder = pjs.ObjectBuilder(schema)
449+
ns = builder.build_classes()
450+
451+
ns.Test(data={"my_bool": True})

0 commit comments

Comments
 (0)