From 4ed9c27ecdf5cd7dfd733e41ec353049f01c1d14 Mon Sep 17 00:00:00 2001 From: Simeon Visser Date: Fri, 16 Feb 2018 12:10:15 +0000 Subject: [PATCH] Added support for Marker descriptions --- tests/test_lib.py | 11 +++++++++++ voluptuous_serialize/__init__.py | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/tests/test_lib.py b/tests/test_lib.py index bcf825d..a61c501 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -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, + })) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 5bcec1b..386c2fe 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -21,13 +21,17 @@ def convert(schema): val = [] for key, value in schema.items(): + description = None if isinstance(key, vol.Marker): pkey = key.schema + description = key.description 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