Ejemplo n.º 1
0
    def validate(self, field_name, value, **options):
        errors = []

        required = options.pop('required', False)

        try:
            required_validator(value, required)
        except ValidationError as exc:
            errors.append(exc.detail)

        many = options.pop('many', False)

        try:
            # should be given list type but given something else
            many_validator(value, many)
        except ValidationError as exc:
            errors.append(exc.detail)

        if many:
            for single_value in value:
                try:
                    self.validate(field_name, single_value, **options)
                except ValidationError as exc:
                    errors.append(exc.detail)

            if errors:
                raise ValidationError(errors, field_name)

        validator = options.pop('validator')
        typ = validator['type']

        try:
            self.type_validators[typ](value)
        except ValidationError as exc:
            errors.append(exc.detail)

        for value, key in options.items():
            if value not in self.option_validators:
                raise ValidationOptionNotFound
            try:
                self.option_validators[value](key)
            except ValidationError as exc:
                errors.append(exc.detail)

        if errors:
            raise ValidationError(errors, field_name)
Ejemplo n.º 2
0
    def __init__(self, **kwargs):
        errors = []
        # FIXME: ValidationError handling still seems clumsy, need to reviewed
        for field_name, value in kwargs.items():
            try:
                self.validator.validate(field_name, value, **self.fields[field_name])
                setattr(self, field_name, value)
            except ValidationError as exc:
                errors.append(exc.detail)

        if errors:
            logger = logging.getLogger('django.request')
            logging.error(errors)
            raise ValidationError(errors)

        for field_name, options in self.fields.items():
            if not hasattr(self, field_name):
                setattr(self,
                        field_name,
                        _default_value_setter(field_name, self.fields[field_name]['validator']['type'])
                        )
Ejemplo n.º 3
0
def many_validator(value, many=False):
    if not isinstance(value, list) and many:
        raise ValidationError(
            VALIDATION_MESSAGES[ValidationCode.INVALID_TYPE] %
            (type(value), list), ValidationCode.INVALID_TYPE)
Ejemplo n.º 4
0
def required_validator(value, required=False):
    if not value and required:
        raise ValidationError(VALIDATION_MESSAGES[ValidationCode.REQUIRED],
                              ValidationCode.REQUIRED)
Ejemplo n.º 5
0
def int_type_validator(value):
    if not isinstance(value, int):
        raise ValidationError(
            VALIDATION_MESSAGES[ValidationCode.INVALID_TYPE] %
            (type(value), int), ValidationCode.INVALID_TYPE)
Ejemplo n.º 6
0
def max_length_validator(value, length):
    if len(value) > length:
        raise ValidationError(
            VALIDATION_MESSAGES[ValidationCode.MAX_LENGTH] % length,
            ValidationCode.MAX_LENGTH)
Ejemplo n.º 7
0
def bool_type_validator(value):
    if value not in [1, 0, 'true', 'false', '1', '0']:
        raise ValidationError(
            VALIDATION_MESSAGES[ValidationCode.INVALID_TYPE] %
            (type(value), bool), ValidationCode.INVALID_TYPE)
Ejemplo n.º 8
0
def es_type_validator(value):
    if not isinstance(value, str) or not value.isnumeric():
        raise ValidationError(
            VALIDATION_MESSAGES[ValidationCode.INVALID_TYPE] %
            (type(value), str), ValidationCode.INVALID_TYPE)