Skip to main content

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:

$ (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

Metadata

Release files for validit 1.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for validit 1.3.2
File Size Uploaded
validit-1.3.2.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for validit 1.3.2
File Interpreter ABI Platform
validit-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.2 kB

Release files / validit-1.3.2.tar.gz

Download URL validit-1.3.2.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
520be6b9893b675761bcd8a17c3773e5d1c87a90244102a8f820f54a70ee2dca
BLAKE2b-256 checksum
How to use checksums
9ebe5031a86c6bf77731e37dc3b7274774e24efa7fd371cc07c9feda942f0226
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / validit-1.3.2-py3-none-any.whl

Download URL validit-1.3.2-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06e0f2cc211f9096077a61009af75346346689b7ce80f159642037e34231b8ab
BLAKE2b-256 checksum
How to use checksums
302b039bd87693910be6e71c74665217e1fa9ddba2982d4bbdbd7b744c7123ea
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.3.2 This release

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page