Python/Cython predicate/guard/validation system.
Project description
This package helps defining and creating small and reusable components that can serve as guard or validation methods.
Example
Below is an example of a validation on a content item.
from dataclasses import dataclass
from prejudice.errors import ConstraintError
from prejudice.validators import Or
@dataclass
class Document:
id: str
body: str = ''
content_type: str = 'text/plain'
def non_empty_document(item):
"""Implementation of a validator/predicate
"""
if not item.body:
raise ConstraintError('Body is empty.')
class ContentType:
def __init__(self, content_type):
self.ct = content_type
def __call__(self, item):
if item.content_type != self.ct:
raise ConstraintError(
f'Expected {self.ct}, got {item.content_type}.')
validator = Or((
ContentType('text/plain'),
Or((ContentType('text/html'), non_empty_document))
))
document = Document(id='test', content_type='application/json')
validator(document) # raises ConstraintsErrors
CHANGES
0.1 (2022-03-16)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
prejudice-0.1.tar.gz
(77.2 kB
view hashes)
Built Distributions
Close
Hashes for prejudice-0.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 741a42f4e27d18cd71f89f66153dd07661d9fe0efca7231ac02ddb85ec7b66ed |
|
MD5 | 7a4c48a2c8b07703d3c5a245cae5763d |
|
BLAKE2b-256 | 365b4648de240e595b5efe7f6bcdd909c72e18c97db6ad5795868a847289afbf |
Close
Hashes for prejudice-0.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bfa266f51a7f8b823d1e14a1616d1d7a9ce7005729340e9fb41d06691e5eff9 |
|
MD5 | b109435daaa24315a8749cee452e861e |
|
BLAKE2b-256 | c57543be1b732abea55e5bfeb825b123097ab9d3784f583fb2187f1fa8b5817c |