Easily define configuration file structures, and validate files using the templates. ?
validit is tested on CPython 3.6, 3.7, 3.8, and 3.9. Simply install using pip:
$ (sudo) pip install validitBy 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:
JSONYAMLTOMLTo 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 formatsTo 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.
)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 dataIf 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 datavalidit 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