Skip to content

Commit

Permalink
Change mapping type to list
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Feb 2, 2018
1 parent f70ed80 commit fa4fce7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool:pytest]
testpaths = tests
norecursedirs = .git
21 changes: 12 additions & 9 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
5 changes: 3 additions & 2 deletions voluptuous_json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -26,14 +26,15 @@ def convert(schema):
pkey = key

pval = convert(value)
pval['name'] = pkey

if isinstance(key, (vol.Required, vol.Optional)):
pval[key.__class__.__name__.lower()] = True

if key.default is not vol.UNDEFINED:
pval['default'] = key.default()

val[pkey] = pval
val.append(pval)

return val

Expand Down

0 comments on commit fa4fce7

Please sign in to comment.