diff options
author | Ruben Undheim <ruben.undheim@gmail.com> | 2019-10-19 11:41:18 +0200 |
---|---|---|
committer | Ruben Undheim <ruben.undheim@gmail.com> | 2019-10-19 11:41:18 +0200 |
commit | fabf88d0279a55f3b6b03bfa67e13134569f38fc (patch) | |
tree | 9d7633a174349fcb52aec79413605d42387e8435 | |
parent | 6dfbccb15630472233639bf2ad093493e90f3629 (diff) |
New upstream version 2.3.0
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | tests/test_lib.py | 21 | ||||
-rw-r--r-- | voluptuous_serialize/__init__.py | 17 |
4 files changed, 53 insertions, 4 deletions
@@ -42,3 +42,20 @@ _(dictionaries become lists to guarantee order of properties)_ ``` See the tests for more examples. + +## Custom serializer + +You can pass a custom serializer to be able to process custom validators. If the serializer returns `UNSUPPORTED`, it will return to normal processing. + +```python + +from voluptuous_serialize import UNSUPPORTED, convert + +def custom_convert(value): + if value is my_custom_validator: + return {'type': 'custom_validator'} + + return UNSUPPORTED + +convert(value, custom_serializer=custom_convert) +``` @@ -1,7 +1,7 @@ from setuptools import setup setup(name='voluptuous-serialize', - version='2.1.0', + version='2.2.0', description='Convert voluptuous schemas to dictionaries', url='http://github.com/balloob/voluptuous-serialize', author='Paulus Schoutsen', diff --git a/tests/test_lib.py b/tests/test_lib.py index 9e0e2d0..30a5a37 100644 --- a/tests/test_lib.py +++ b/tests/test_lib.py @@ -142,3 +142,24 @@ def test_strip(): 'type': 'string', 'strip': True, } == convert(vol.Schema(vol.All(vol.Strip, str))) + + +def test_email(): + assert { + 'type': 'string', + 'format': 'email', + } == convert(vol.Schema(vol.All(vol.Email, str))) + + +def test_url(): + assert { + 'type': 'string', + 'format': 'url', + } == convert(vol.Schema(vol.All(vol.Url, str))) + + +def test_fqdnurl(): + assert { + 'type': 'string', + 'format': 'fqdnurl', + } == convert(vol.Schema(vol.All(vol.FqdnUrl, str))) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 8bc2676..b12d597 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -11,13 +11,19 @@ TYPES_MAP = { bool: 'boolean', } +UNSUPPORTED = object() -def convert(schema): +def convert(schema, *, custom_serializer=None): """Convert a voluptuous schema to a dictionary.""" # pylint: disable=too-many-return-statements,too-many-branches if isinstance(schema, vol.Schema): schema = schema.schema + if custom_serializer: + val = custom_serializer(schema) + if val is not UNSUPPORTED: + return val + if isinstance(schema, Mapping): val = [] @@ -29,7 +35,7 @@ def convert(schema): else: pkey = key - pval = convert(value) + pval = convert(value, custom_serializer=custom_serializer) pval['name'] = pkey if description is not None: pval['description'] = description @@ -47,7 +53,7 @@ def convert(schema): if isinstance(schema, vol.All): val = {} for validator in schema.validators: - val.update(convert(validator)) + val.update(convert(validator, custom_serializer=custom_serializer)) return val if isinstance(schema, (vol.Clamp, vol.Range)): @@ -88,6 +94,11 @@ def convert(schema): schema.__name__.lower(): True, } + if schema in (vol.Email, vol.Url, vol.FqdnUrl): + return { + 'format': schema.__name__.lower(), + } + if isinstance(schema, vol.Coerce): schema = schema.type |