diff --git a/README.md b/README.md index a911097..98b308f 100644 --- a/README.md +++ b/README.md @@ -16,24 +16,29 @@ vol.Schema({ becomes +_(dictionaries become lists to guarantee order of properties)_ + ```json -{ - "name": { +[ + { + "name": "name", "type": "string", "length-min": 5, "required": true, }, - "age": { + { + "name": "age", "type": "integer", "value-min": 18, "required": true, }, - "hobby": { + { + "name": "hobby", "type": "string", "default": "not specified", "optional": true, } -} +] ``` See the tests for more examples. diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..e0f9802 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[tool:pytest] +testpaths = tests +norecursedirs = .git diff --git a/tests/test_lib.py b/tests/test_lib.py index 0892451..4432790 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -58,24 +58,27 @@ def test_in(): def test_dict(): - assert { - 'name': { + assert [ + { + 'name': 'name', 'type': 'string', 'length-min': 5, 'required': True, }, - 'age': { + { + 'name': 'age', 'type': 'integer', 'value-min': 18, 'required': True, }, - 'hobby': { + { + 'name': 'hobby', 'type': 'string', 'default': 'not specified', 'optional': True, } - } == convert(vol.Schema({ - vol.Required('name'): vol.All(str, vol.Length(min=5)), - vol.Required('age'): vol.All(vol.Coerce(int), vol.Range(min=18)), - vol.Optional('hobby', default='not specified'): str - })) + ] == convert(vol.Schema({ + vol.Required('name'): vol.All(str, vol.Length(min=5)), + vol.Required('age'): vol.All(vol.Coerce(int), vol.Range(min=18)), + vol.Optional('hobby', default='not specified'): str + })) diff --git a/voluptuous_json/__init__.py b/voluptuous_json/__init__.py index ef93df2..0737543 100644 --- a/voluptuous_json/__init__.py +++ b/voluptuous_json/__init__.py @@ -17,7 +17,7 @@ def convert(schema): schema = schema.schema if isinstance(schema, collections.Mapping): - val = {} + val = [] for key, value in schema.items(): if isinstance(key, vol.Marker): @@ -26,6 +26,7 @@ def convert(schema): pkey = key pval = convert(value) + pval['name'] = pkey if isinstance(key, (vol.Required, vol.Optional)): pval[key.__class__.__name__.lower()] = True @@ -33,7 +34,7 @@ def convert(schema): if key.default is not vol.UNDEFINED: pval['default'] = key.default() - val[pkey] = pval + val.append(pval) return val