Example #1
0
    def test_named_subtype(self):
        SmallString = c.subtype(c.String, lambda d: len(d) <= 10, name='SmallString')

        with self.assertRaises(exceptions.PyCombValidationError) as e:
            SmallString('12345678901')
        e = e.exception
        self.assertEqual('Error on SmallString: expected SmallString but was str', e.args[0])
        self.assertEqual('12345', SmallString('12345'))
Example #2
0
    def test_returning(self):
        @returning(cmb.subtype(cmb.String, lambda d: len(d) < 10))
        def f(n):
            return ' ' * n

        self.assertEqual('   ', f(3))

        with(self.assertRaises(exceptions.PyCombValidationError)):
            f(10)
Example #3
0
 def test_subtype_production(self):
     SmallString = c.subtype(c.String, lambda d: len(d) <= 10)
     self.assertEqual('12345678901', SmallString('12345678901', ctx=context.create(production_mode=True)))
Example #4
0
 def test_subtype_custom_error(self):
     observer = Mock()
     ctx = context.create(validation_error_observer=observer)
     SmallString = c.subtype(c.String, lambda d: len(d) <= 10)
     SmallString('12345678901', ctx=ctx)
     observer.on_error.assert_called_once_with(_ANY_CONTEXT, 'Subtype(String)', str)
Example #5
0
import re

from pycomb import combinators
from pycomb.exceptions import PyCombValidationError

from src import exceptions


def is_amount(amount: str):
    try:
        return bool(float(amount) > 0)
    except ValueError:
        return


CurrencyCombinator = combinators.subtype(combinators.String,
                                         lambda currency: len(currency) == 3)

ReferenceDateCombinator = combinators.subtype(
    combinators.String, lambda date: re.match(
        r'([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))', date))

AmountCombinator = combinators.subtype(combinators.String, is_amount)


def validate_payload(**rules):
    def decorator(fun):
        @functools.wraps(fun)
        def wrapper(request):
            valid_args = set(request.query.keys()) & set(rules.keys())
            if valid_args != set(rules.keys()):
                raise exceptions.MissingArgumentsException(
Example #6
0
from pycomb.decorators import returning


class MyObserver(context.ValidationErrorObserver):
    def on_error(self, ctx, expected_type, found_type):
        print("Expected {}, got {}".format(expected_type, found_type))


def is_positive(x: int) -> bool:
    return x >= 0


def is_integer(x: int) -> bool:
    return x % 1 == 0


def is_natural(x: int) -> bool:
    return is_positive(x) and is_integer(x)


natural_numbers = combinators.subtype(combinators.Int,
                                      is_natural,
                                      example=42,
                                      name="Natural number")


@combinators.function(natural_numbers, natural_numbers)
@returning(natural_numbers)
def add(a: int, b: int) -> int:
    return a + b
Example #7
0
                    raise exceptions.WrongParametersException
            if silent:
                try:
                    validation_class(p)
                except PyCombValidationError:
                    raise exceptions.WrongParametersException
            else:
                validation_class(p)
            return fun(*a, params=p, **kw)

        return wrapper

    return decorator


UUIDValidator = validators.subtype(validators.String, is_uuid)

SplitProtocolValidator = validators.subtype(validators.String,
                                            lambda x: x in ['fxc1'])

SplitSessionValidator = validators.struct({
    "secret":
    validators.struct({
        "value": validators.String,
        "protocol": validators.maybe(SplitProtocolValidator),
    })
})

SplitSessionCreateValidator = validators.struct(
    {
        "client_alias":