Exemplo n.º 1
0
class String(HasRegEx, length.HasLength, specified.SimpleChoices):
    """

    Examples:

    """
    __slots__ = ('encoding',) + base.combine_slots(
        HasRegEx,
        length.HasLength,
        specified.SimpleChoices,
    )
    default_coerce_type = str

    def __init__(
            self,
            *,
            encoding: Optional[str] = 'utf-8',
            **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.encoding = encoding

    def _validate(self, value: Any) -> Any:
        if isinstance(value, bytes) and self.encoding is not None:
            try:
                value = value.decode(self.encoding)
            except UnicodeDecodeError as err:
                args = (value, self, str, err)
                raise exc.ValidationTypeError(*args) from err
        return super()._validate(value)
Exemplo n.º 2
0

def raise_unknown_keys(key: str, value: Any) -> NoReturn:
    msg = f'key {key!r} with value {value!r} is forbidden'
    raise KeyError(msg)


def include_unknown_keys(key: str, value: Any) -> Tuple[str, Any]:
    return key, value


def exclude_unknown_keys(key: str, value: Any) -> Tuple[None, None]:
    return None, None


@base.abstractslots(base.combine_slots(base.Validator))
class Schema(base.Validator):
    __slots__ = ()


class MappingSchema(Schema):
    __slots__ = base.get_slots(Schema) + (
        'schema',
        'unknown_key_hook',
        'factory',
    )

    def __init__(
        self,
        *,
        schema: Mapping[str, Any],
Exemplo n.º 3
0
def _validate_template_value(template: Callable[[Any], Any],
                             value: Any) -> Any:
    return template(value)


def _validate_template_type(template: Type, value: Any) -> Any:
    if not isinstance(value, template):
        raise exc.SimpleTypeError(value, template)
    return value


@base.abstractslots(
    base.combine_slots(
        length.HasLength,
        base.Nullable,
        base.Validator,
    ) + (
        'template',
        '_validate_template',
    ))
class Collection(length.HasLength, base.Nullable, base.Validator):
    __slots__ = ()

    def __init__(
        self,
        *,
        template: TemplateType,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
Exemplo n.º 4
0
            raise exc.ExcludedEnumeratedChoiceError(value, self)
        return value


@base.abstractslots(('choices',))
class HasChoices(base.Validator):
    """Abstract base class for validators constrained to choices.

    """
    __slots__ = ()

    def __init__(
        self,
        *,
        choices: Optional[SpecifiedValueValidator] = None,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.choices = choices

    @abc.abstractmethod
    def _validate(self, value: Any) -> Any:
        if self.choices is not None:
            value = self.choices(value)
        return super()._validate(value)


@base.abstractslots(base.combine_slots(HasChoices, base.Simple))
class SimpleChoices(HasChoices, base.Simple):
    __slots__ = ()
Exemplo n.º 5
0
import decimal
import fractions
import math
import numbers

from typing import (
    Any, )

import litecore.validation.base as base
import litecore.validation.specified as specified
import litecore.validation.exceptions as exc


@base.abstractslots(
    base.combine_slots(
        base.HasBounds,
        specified.SimpleChoices,
    ) + ('coerce_implicit', ))
class Numeric(base.HasBounds, specified.SimpleChoices):
    """Abstract base class for validating numeric values."""
    __slots__ = ()

    def __init__(
        self,
        *,
        coerce_implicit: bool = True,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.coerce_implicit = bool(coerce_implicit)