Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/json2d/validium
a functional library for creating validators in Python
https://github.com/json2d/validium
functional-programming validation
Last synced: 26 days ago
JSON representation
a functional library for creating validators in Python
- Host: GitHub
- URL: https://github.com/json2d/validium
- Owner: json2d
- License: mit
- Created: 2020-02-28T23:05:59.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-21T00:02:05.000Z (almost 5 years ago)
- Last Synced: 2024-09-22T11:06:19.885Z (4 months ago)
- Topics: functional-programming, validation
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# validium
a Python utility library for performing validations flexibly## Installation
```bash
pip install validium
```## Quick Start
Here's an example of how to create and use some very simple validators for some numbers:
```py
import validium as VPI = 3.14
ONE = 1is_number = V.Validator(lambda x: isinstance(x, Number), 'must be a number')
is_number.validate(PI) # pass
is_number.validate(ONE) # passis_positive = V.Validator(lambda x: x > 0, 'must be positive')
is_positive.validate(PI) # pass
is_positive.validate(ONE) # passis_not_one = V.Validator(lambda x: not x == 1, 'must not equal 1')
is_not_one.validate(PI) # pass
is_not_one.validate(ONE) # AssertionError: must not equal 1```
### Building Up
Here's an example of how to parameterize and reuse a common validator pattern:
```py
is_not = lambda y: V.Validator(lambda x: not x == y, 'must not equal {}'.format(x)) # uis_not(-1).validate(ONE) # pass
is_not(0).validate(ONE) # pass
is_not(1).validate(ONE) # AssertionError: must not equal 1```
This approach will help keep your code nice and DRY in the event you need handful of validators that behave mostly the same but slightly different.