Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validit v2.0 #2

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
66346f1
🔥 Removed all `validit-v1` code
RealA10N Jan 4, 2022
f0eceab
✨ Added abstract `Schema` class
RealA10N Jan 4, 2022
263a29e
✨ Added `String` schema
RealA10N Jan 4, 2022
e47c570
🔥 Removed `install_requires`
RealA10N Jan 4, 2022
fc9fff5
✨ Added `String` and `Schema` to `__init__` import
RealA10N Jan 4, 2022
db68c6a
✅ Added `String` schema tests (no regex)
RealA10N Jan 4, 2022
365732b
✅ Added `String` regex tests
RealA10N Jan 4, 2022
f3f970d
✨ Added `__repr__` to Schema
RealA10N Jan 4, 2022
bd73adc
✨ Added `Union` Schema
RealA10N Jan 4, 2022
677a094
✨ Added `Optional` Schema
RealA10N Jan 4, 2022
60edc73
✨ Added `Boolean` Schema
RealA10N Jan 4, 2022
ea433b7
✅ Added `Union` Schema tests
RealA10N Jan 4, 2022
48e86b1
✨ Added `Dictionary` Schema
RealA10N Jan 4, 2022
672f098
✨ Added `Number` and `Integer` Schemas (no tests)
RealA10N Jan 4, 2022
517ab06
⚡️ Rearranged `__init__.py`
RealA10N Jan 4, 2022
bb59d72
👷 Added CI tests with py3.10
RealA10N Jan 4, 2022
fc9da0b
💚 Fixed CI config
RealA10N Jan 4, 2022
56eee8a
⚡️ Updated implementation of `Number` Schema
RealA10N Jan 4, 2022
a155446
✅ Added `Number` and `Integer` tests
RealA10N Jan 4, 2022
e86492d
⚡️ `String` schema now accepts regex patterns as strings
RealA10N Jan 4, 2022
6df1935
✨ Added external `validate` function
RealA10N Jan 4, 2022
0e8e912
📝 Updated `README.md` to v2.0
RealA10N Jan 4, 2022
e635b0c
⚡️ Updated test to new `String` RegEx format
RealA10N Jan 4, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
version: [3.6, 3.7, 3.8, 3.9]
version: ['3.6', '3.7', '3.8', '3.9', '3.10']

steps:
- name: Clone 👀
Expand Down
127 changes: 16 additions & 111 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,134 +2,39 @@

[![Test](https://img.shields.io/github/workflow/status/reala10n/validit/%E2%9C%94%20Test?label=test)](https://github.com/RealA10N/validit/actions/workflows/test.yaml)
[![PyPI](https://img.shields.io/pypi/v/validit)](https://pypi.org/project/validit/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/validit)](https://pypi.org/project/validit/)
[![GitHub Repo stars](https://img.shields.io/github/stars/reala10n/validit?style=social)](https://github.com/RealA10N/validit)

_Easily define configuration file structures, and validate files using the templates. 🍒📂_

- [Installation](#installation)
- [Support for additional file formats](#support-for-additional-file-formats)
- [Usage](#usage)
- [Defining a template](#defining-a-template)
- [Validating data](#validating-data)
- [Validating data from files](#validating-data-from-files)
- [Using validit as a dependency](#using-validit-as-a-dependency)
- [Defining a schema](#defining-a-schema)

## Installation

**validit** is tested on CPython 3.6, 3.7, 3.8, and 3.9.
**validit** is tested on CPython 3.6 - 3.10.
Simply install using pip:

```bash
$ (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:

```yaml
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:

```python
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.

```python
from validit import Template, TemplateDict, Optional, Validate
### Defining a schema

template = TemplateDict(
username=Template(str),
passcode=Template(int, str),
nickname=Optional(Template(str)),
)
**validit** uses Python objects for constructing the Schema.
We provide a handful collection of Schema classes that should cover all your
basic usage cases, especially if you are going to validate a JSON, TOML or a YAML configuration file.
We will briefly talk about how you can create your own Schema objects later.

data = {
'username': 'RealA10N',
'passcode': 123,
}
The classes that we provide for defining schemas are:

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
Name | Description
---------------------|-------------------------------------------------------------
`validit.String` | Validates a string and matches it with a _RegEx_ pattern.
`validit.Number` | Validates any real number in a certain range.
`validit.Integer` | Validates an integer in a certain range.
`validit.Union` | Matches a value with at least one schema.
`validit.Dictionary` | Validates a dictionary and all values inside it recursively.
`validit.Optional` | Converts a field inside a `Dictionary` into an optional one.

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:

```python
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:

```yaml
validit==1.3.2
validit[yaml]==1.3.2 # If using extra file formats
```
9 changes: 0 additions & 9 deletions examples/README.md

This file was deleted.

47 changes: 0 additions & 47 deletions examples/example1/example1.json

This file was deleted.

21 changes: 0 additions & 21 deletions examples/example1/example1.py

This file was deleted.

29 changes: 0 additions & 29 deletions examples/example1/example1.toml

This file was deleted.

34 changes: 0 additions & 34 deletions examples/example1/example1.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions examples/isodates/isodates.py

This file was deleted.

11 changes: 0 additions & 11 deletions examples/isodates/isodates.toml

This file was deleted.

8 changes: 0 additions & 8 deletions examples/isodates/isodates.yaml

This file was deleted.

28 changes: 6 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,25 @@ def load_readme():
return readme.read()


REQUIRES = (
'termcolor==1.1.0',

# the dataclasses module is prebuilt into python>=3.7
# For Python 3.6, it is supported using a backport
'dataclasses; python_version < "3.7"',
)


EXTRAS = {
'dev': (
'pytest>=6.2, <6.3',
'flake8>=3.9, <3.10'
),
'yaml': (
'pyyaml>=5.4, <5.5',
),
'toml': (
'toml>=0.10.2, <0.11',
),
}

EXTRAS['all'] = tuple(
package
for group in EXTRAS
if group != 'dev'
for package in EXTRAS[group]

REQUIRES = (
# the dataclasses module is prebuilt into python>=3.7
# For Python 3.6, it is supported using a backport
'dataclasses; python_version < "3.7"',
)

EXTRAS['dev'] += EXTRAS['all']

setup(
name='validit',
description='Easily define and validate configuration file structures 📂🍒',
version='1.3.2',
version='2.0.0-dev',
author='RealA10N',
author_email='[email protected]',
long_description=load_readme(),
Expand Down
Loading