Easily define and validate configuration file structures 📂🍒
Project description
validit
Easily define configuration file structures, and validate files using the templates. 🍒📂
Installation
validit is tested on CPython 3.6, 3.7, 3.8, and 3.9. Simply install using pip:
$ (sudo) pip install validit
Support for additional file formats
By default, validit only supports JSON
configuration files, or
already loaded data (not directly from a configuration file). However, using
additional dependencies, validit supports the following file formats:
JSON
YAML
TOML
To install validit with the additional required dependencies to support your preferred file format, use:
pip install validit[yaml] # install dependencies for yaml files
pip install validit[toml] # toml files
pip install validit[json,toml] # json and toml files
pip install validit[all] # all available file formats
Usage
Defining a template
To create a template, you will need the basic Template
module, and usually the
other three basic modules TemplateList
, TemplateDict
, and Optional
.
In the following example, we will create a basic template that represents a single user:
from validit import Template, TemplateList, TemplateDict, Optional
TemplateUser = TemplateDict( # a dictionary with 2 required values
username=Template(str), # username must be a string
passcode=Template(int, str), # can be a string or an integer.
nickname=Optional(Template(str)), # optional - if provided, must be a string.
)
Validating data
To validate your data with a template, you should use the Validate
object.
from validit import Template, TemplateDict, Optional, Validate
template = TemplateDict(
username=Template(str),
passcode=Template(int, str),
nickname=Optional(Template(str)),
)
data = {
'username': 'RealA10N',
'passcode': 123,
}
valid = Validate(template, data)
if valid.errors: # if one or more errors found
print(valid.errors) # print errors to console
exit(1) # exit the script with exit code 1
else: # if data matches the template
run_script(valid.data) # run the script with the loaded data
Validating data from files
If your data is stored in a file, it is possible to use the ValidateFromJSON
,
ValidateFromYAML
or ValidateFromTOML
objects instead:
from validit import Template, TemplateDict, Optional, ValidateFromYAML
filepath = '/path/to/data.yaml'
template = TemplateDict(
username=Template(str),
passcode=Template(int, str),
nickname=Optional(Template(str)),
)
with open(filepath, 'r') as file:
# load and validate data from the file
valid = ValidateFromYAML(file, template)
if valid.errors: # if one or more errors found
print(valid.errors) # print errors to console
exit(1) # exit the script with exit code 1
else: # if data matches the template
run_script(valid.data) # run the script with the loaded data
Using validit as a dependency
validit is still under active development, and some core features may change substantially in the near future.
If you are planning to use validit as a dependency for your project,
we highly recommend specifying the exact version of the module you are using
in the requirements.txt
file or setup.py
scripts.
For example, to pinpoint version v1.3.2 use the following line in your
requirements.txt
file:
validit==1.3.2
validit[yaml]==1.3.2 # If using extra file formats
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
Built Distribution
File details
Details for the file validit-1.3.2.tar.gz
.
File metadata
- Download URL: validit-1.3.2.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 520be6b9893b675761bcd8a17c3773e5d1c87a90244102a8f820f54a70ee2dca |
|
MD5 | 7f3eb05c8cbbb02eb9321f7aa4231b2f |
|
BLAKE2b-256 | 9ebe5031a86c6bf77731e37dc3b7274774e24efa7fd371cc07c9feda942f0226 |
File details
Details for the file validit-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: validit-1.3.2-py3-none-any.whl
- Upload date:
- Size: 13.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06e0f2cc211f9096077a61009af75346346689b7ce80f159642037e34231b8ab |
|
MD5 | c65a51956d9d96aaed1a3cbc11d7d708 |
|
BLAKE2b-256 | 302b039bd87693910be6e71c74665217e1fa9ddba2982d4bbdbd7b744c7123ea |