Skip to main content

ValidDictorian

Declarative Python Dictionary Validator

Copyright 2024 Scott Perry, Available under the MIT license, GitHub repo: https://github.com/sperry04/validdict

Purpose

validdict provides a way to declare schema validation rules for a Python dictionary using Python objects and types, rather than using a bespoke or interpreted schema validation language. This allows validation of virtually any data that can be represented as a Python dict, such as input strings parsed by modules such as pyyaml or json.

Schemas are declarative with built-in scalar type validators, plus compositable map and sequence validators that allow the construction of complex schemas.

Basic Usage

To use the validdict library, follow these steps:

  1. Install the library by running the following command:

    pip install validdict
    
  2. Import the validdict module in your Python script:

    import validdict
    
  3. Create a Schema instance with the declaration of a valid document. Each key in the document represents a field in the input dictionary, and the corresponding validation rules for allowed values.

    For example:

    from validdict import *
    
    schema = Schema({
        "int": Num(),
        "float": Num(),
        "bool": Bool(),
        "string": Str(),
        "regex": Regex(r"\w+@\w+\.com") # trivial email regex
    })
    
  4. Use the schema to validate documents:

    results = schema.validate({
        "int": 1,
        "float": 1.0,
        "string": "string",
        "bool": True,
        "regex": "example@email.com"
    })
    
  5. Print, or otherwise process, the validation results:

    print(results)
    

    Output:

    '<dict>' must be a map like: { RequiredKey(): Num(), RequiredKey(): Num(), RequiredKey(): <snip> = 'PASS'
        RequiredKey('int'):'int' must be type 'str' with value 'int' = 'PASS'
        int:'1' must be type in ('int', 'float') = 'PASS'
        RequiredKey('float'):'float' must be type 'str' with value 'float' = 'PASS'
        float:'1.0' must be type in ('int', 'float') = 'PASS'
        RequiredKey('string'):'string' must be type 'str' with value 'string' = 'PASS'
        string:'string' must be type 'str' = 'PASS'
        RequiredKey('bool'):'bool' must be type 'str' with value 'bool' = 'PASS'
        bool:'True' must be type 'bool' = 'PASS'
        RequiredKey('regex'):'regex' must be type 'str' with value 'regex' = 'PASS'
        regex:'example@email.com' must be type 'str' with value matching '\w+@\w+\.com' = 'PASS'
    

Examples

  1. Validating literals
  2. Handling validation results
  3. Validating scalar types
  4. Validating dictionary keys
  5. Composition of large schemas
  6. Advanced techniques

Metadata

Release files for validdict 0.1.1

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

Source distribution (sdist)

Source distribution for validdict 0.1.1
File Size Uploaded
validdict-0.1.1.tar.gz 15.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for validdict 0.1.1
File Interpreter ABI Platform
validdict-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 35.5 kB

Release files / validdict-0.1.1.tar.gz

Download URL validdict-0.1.1.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
251bdac157ed98437ecb0dce5615e6cdebd190b05656e441ae3563ae30219c23
BLAKE2b-256 checksum
How to use checksums
4327c42ca9baecbf2bf7627cad8100f53749be0b17e5507380d7ae02caa51deb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.12 Linux/5.15.0-116-generic

Release files / validdict-0.1.1-py3-none-any.whl

Download URL validdict-0.1.1-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
815b06918f9caa2985135bee894797ad6eb48aa5c3b1acaa531c51c107eedb75
BLAKE2b-256 checksum
How to use checksums
91f8ff77f084e0cea730ceaa1ab8728aafe7d55d5aa008dd1fad80e133a5988c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.10.12 Linux/5.15.0-116-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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