Skip to main content

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.1 (2023-11-09)

  • Cython update and included py3.11

0.1 (2022-03-16)

  • Initial release.

Project details


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.1.tar.gz (125.8 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page