Skip to main content

Convert voluptuous schemas to dictionaries

Project description

Voluptuous Serialize

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",
    "required": false,
    "optional": true,  // This is deprecated. Please use "required" key instead.
  }
]

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.

from typing import Any
from voluptuous_serialize import UNSUPPORTED, UnsupportedType, convert

def custom_convert(value: Any) -> dict[str, str] | UnsupportedType:
    if value is my_custom_validator:
        return {'type': 'custom_validator'}
        
    return UNSUPPORTED

convert(value, custom_serializer=custom_convert)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

voluptuous_serialize-2.7.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

voluptuous_serialize-2.7.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file voluptuous_serialize-2.7.0.tar.gz.

File metadata

  • Download URL: voluptuous_serialize-2.7.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.6

File hashes

Hashes for voluptuous_serialize-2.7.0.tar.gz
Algorithm Hash digest
SHA256 d0da959f2fd93c8f1eb779c5d116231940493b51020c2c1026bab76eb56cd09e
MD5 9251df5a9d49177a76a7856986036d3d
BLAKE2b-256 537003a9b61324e1bb8b16682455b8b953bccd1001a28e43478c86f539e26285

See more details on using hashes here.

File details

Details for the file voluptuous_serialize-2.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for voluptuous_serialize-2.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee3ebecace6136f38d0bf8c20ee97155db2486c6b2d0795563fafd04a519e76f
MD5 2a72bdd9ffdf712112e6a5cac788649b
BLAKE2b-256 f741d536d9cf39821c35cc13aff403728e60e32b2fd711c240b6b9980af1c03f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page