Skip to content

A Python validator based on JSR-303, Hibernate Validator, and Symfony Validator

License

Notifications You must be signed in to change notification settings

kinow/python-jsr303

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

python-jsr303

A Python validator based on JSR 303, Hibernate Validator, and Symfony Validator.

Introduction

from jsr303 import validation

validator = validation.create_validator()
violations = validator.validate('Brussels', [Length(min=10), NotBlank()])

if violations:
    for violation in violations:
        print(violation.message)

Custom Validation Constraint

from jsr303 import Constraint, ConstraintValidator, validate

class ContainsAlphanumeric(Constraint):

    message = "The string {string} contains an illegal character: it can only contain letters or numbers."


import re


class ContainsAlphanumericValidator(ConstraintValidator):

    def validate(value: str, constraint: Constraint) -> None:
        """
        :raises UnexpectedTypeException
        """
        if value is None or value == "":
            return;
        if type(value) != str:
            raise UnexpectedTypeException(value, 'str')
        if not re.match('^[a-zA-Z0-9]+$', value):
            self.context.buildViolation(constraint.message).
                parameter(string=value).
                add_violation()


class FooClass(object):

    def __init__(self, value=None):
        self._value = value

    @validate(validators=[NotNone, Blank, ContainsAlphanumeric])
    @property
    def do_something(self):
        return self._value

About

A Python validator based on JSR-303, Hibernate Validator, and Symfony Validator

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages