Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

About dschema

pypi Master Documentation Status codecov

dschema is a small library for validating the content of python dictionary objects against a schema.

The schema can be defined in code or entirely as text (parsed from json generally)

dschema was mainly made for validating config files written in JSON.

Example

See documentation link for more examples

import re
import phonenumbers
import dschema


# https://github.com/daviddrysdale/python-phonenumbers
# pip install phonenumbers


def phone_type(number):
    # Exceptions are validation errors
    # Very similar design to the "argparse" module
    return phonenumbers.parse(number)


def ssn_type(ssn):
    if re.match('^\d{3}-?\d{2}-?\d{4}$', ssn):
        return ssn
    else:
        raise ValueError('"{}" is not a valid SSN.')


schema = {
    'person': {
        'first_name': dschema.prop(required=True),
        'last_name': dschema.prop(required=True),
        'phone': dschema.prop(required=True, type=phone_type),
        'ssn': dschema.prop(required=True, type='ssn_type'),

        dschema.Required: True
        # "person" namespace is required, you must specify
        # even if "person" itself contains required properties
    },

    # Allow a raw dictionary value to pass through

    'other_info': dschema.prop(default=dict(), dict=True),

    # default to False if not present

    'subscribed': dschema.prop(default=False, type=bool)
}

validator = dschema.Validator(schema)

# you can use this to add types that are recognized by name.
# which is useful if you want your schema to be entirely textual

validator.add_type('ssn_type', ssn_type)

# you will need to define default types on your own
# if you want to reference them by name

# validator.add_type('int', int)


data = {
    'person': {
        'first_name': "John",
        'last_name': "Smith",
        'phone': '+1 234 5678 9000',
        'ssn': '123-45-6789'
    },

    'other_info': {
        'website': 'www.johnsmith.com',
    }
}

# If namespace is left False, a plain dictionary is returned

result = validator.validate(data, namespace=True)

print(result)

# Prints: (un-indented)

# Namespace(
#     person=Namespace(
#         first_name='John',
#         last_name='Smith',
#         phone=PhoneNumber(...),
#         ssn='123-45-6789'),
#     other_info={'website': 'www.johnsmith.com'},
#     subscribed=False
# )


# Each Namespace is just a dynamic object

print(result.person.first_name)  # -> John
print(result.person.last_name)  # -> Smith

print(result.person.phone)
# - > Country Code: 1 National Number: 23456789000

print(result.person.ssn)  # -> 123-45-6789

print(result.other_info)  # -> {'website': 'www.johnsmith.com'}

print(result.subscribed)  # -> False (default)

Metadata

Release files for dschema 0.3.1.3a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dschema 0.3.1.3a1
File Size Uploaded Provenance
dschema-0.3.1.3a1.tar.gz 8.1 kB Without provenance Details

Release files / dschema-0.3.1.3a1.tar.gz

Download URL dschema-0.3.1.3a1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
68088c2f342d192c30c737b80c5e2fde9a54f73f79dc63513ac4da5bef346ebc
BLAKE2b-256 checksum
How to use checksums
64941d0d8c671a73d42e355365538130da27260539a22bb1b7c6aa988ecff190
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

Security for dschema 0.3.1.3a1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page