Convert Voluptuous schemas to json serializable objects.
import voluptuous as vol
from voluptuous_serialize import convert
schema = 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,
}
)
result = convert(schema)
becomes
(dictionaries become lists to guarantee order of properties)
[
{
"name": "name",
"type": "string",
"lengthMin": 5,
"required": true,
},
{
"name": "age",
"type": "integer",
"valueMin": 18,
"required": true,
},
{
"name": "hobby",
"type": "string",
"default": "not specified",
"optional": true,
}
]
See the tests for more examples.
You can pass a custom serializer to be able to process custom validators. If the serializer returns UNSUPPORTED
, it will return to normal processing.
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)