Giter Club home page Giter Club logo

validit's Introduction

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

validit's People

Contributors

reala10n avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

megatron93

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.