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:
-
Install the library by running the following command:
pip install validdict -
Import the
validdictmodule in your Python script:import validdict
-
Create a
Schemainstance 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 })
-
Use the schema to validate documents:
results = schema.validate({ "int": 1, "float": 1.0, "string": "string", "bool": True, "regex": "example@email.com" })
-
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file validdict-0.1.1.tar.gz.
File metadata
- Download URL: validdict-0.1.1.tar.gz
- Upload date:
- Size: 15.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
251bdac157ed98437ecb0dce5615e6cdebd190b05656e441ae3563ae30219c23
|
|
| MD5 |
f26437a2cf54afffaa0a76b718ad6e25
|
|
| BLAKE2b-256 |
4327c42ca9baecbf2bf7627cad8100f53749be0b17e5507380d7ae02caa51deb
|
File details
Details for the file validdict-0.1.1-py3-none-any.whl.
File metadata
- Download URL: validdict-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
815b06918f9caa2985135bee894797ad6eb48aa5c3b1acaa531c51c107eedb75
|
|
| MD5 |
6b6337b0b66c9c2f22fa807f0cecdc7a
|
|
| BLAKE2b-256 |
91f8ff77f084e0cea730ceaa1ab8728aafe7d55d5aa008dd1fad80e133a5988c
|