Skip to content

Commit

Permalink
Added support for Marker descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
svisser committed Feb 16, 2018
1 parent 45251bc commit b2ccaf6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ def test_dict():
vol.Required('age'): vol.All(vol.Coerce(int), vol.Range(min=18)),
vol.Optional('hobby', default='not specified'): str
}))


def test_marker_description():
assert [{
'name': 'name',
'type': 'string',
'description': 'Description of name',
'required': True,
}] == convert(vol.Schema({
vol.Required('name', description='Description of name'): str,
}))
7 changes: 7 additions & 0 deletions voluptuous_serialize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ def convert(schema):
val = []

for key, value in schema.items():
description = None
if isinstance(key, vol.Marker):
pkey = key.schema
try:
description = key.description
except AttributeError:
pass
else:
pkey = key

pval = convert(value)
pval['name'] = pkey
if description is not None:
pval['description'] = description

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

0 comments on commit b2ccaf6

Please sign in to comment.