Skip to main content

Pure python 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 roughrider.predicate.errors import ConstraintError
from roughrider.predicate.validators import Validator, 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(Validator):

    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.3.1 (2021-10-21)

  • Changing packaging of extensions.

0.3 (2021-10-21)

  • Code reorganization and Cython optimizations.

0.2 (2021-10-20)

  • Correction of the resolve_validator errors list extension.

0.1 (2021-10-09)

  • 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

roughrider.predicate-0.3.1.tar.gz (75.6 kB view details)

Uploaded Source

File details

Details for the file roughrider.predicate-0.3.1.tar.gz.

File metadata

  • Download URL: roughrider.predicate-0.3.1.tar.gz
  • Upload date:
  • Size: 75.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for roughrider.predicate-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a355feefa3c5486847ac10084a0541d18809810cfc6a9134bb9f81d870fc8168
MD5 e911853944198d08ceed3de062c62d93
BLAKE2b-256 a5d401c105e08a2ccb511925599463442de46732795de3404b92fa334a258fa2

See more details on using hashes here.

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