Skip to main content

Easily define and validate configuration file structures 📂🍒

Project description

validit

Test PyPI PyPI - Python Version GitHub Repo stars

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:

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.0.0 use the following line in your requirements.txt file:

validit==1.0.0
validit[yaml]==1.0.0     # 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

validit-1.0.0.tar.gz (11.5 kB view hashes)

Uploaded Source

Built Distribution

validit-1.0.0-py3-none-any.whl (12.6 kB view hashes)

Uploaded Python 3

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