Skip to main content

ValidDictorian: Declarative Python Dictionary Validator

Project description

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

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

validdict-0.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

validdict-0.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file validdict-0.1.0.tar.gz.

File metadata

  • Download URL: validdict-0.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/5.15.0-116-generic

File hashes

Hashes for validdict-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fbb780c1dcfea2252b19ef0b3328943dd74f6db6c7109fab21942dcb2d805476
MD5 6558299d446739d708b6fe2d76156600
BLAKE2b-256 1cfd71a82eb66160049d54a3a61809c0dc2c2019c8729846e5ad9d2d8a184517

See more details on using hashes here.

File details

Details for the file validdict-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: validdict-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/5.15.0-116-generic

File hashes

Hashes for validdict-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 26ebbe18b99b101cb1c6070020e8cf2ff48999bbf0c159da4b0b1a9af04fb91c
MD5 0131ab6bfaafecac54b4c437e6589e7c
BLAKE2b-256 0b0c9afe415c4b75a56a04be258b81079adf45de5552d978543d8f4dadb31bb5

See more details on using hashes here.

Supported by

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