Beispiel #1
0
def _check_types(name, options: Options, key, value):
    if not is_compatible(value, options.get_type()):
        raise ValidationError(
            f"Field `{name}#{key}`: {options.get_type()} is not compatible with value `{value}`:{type(value)}"
        )

    return value
Beispiel #2
0
def _validate_size(name, o: Options, k, v):
    if o.size is None:
        return v

    size = o.size

    if callable(size):
        size = size()

    if size is not None and size != len(v):
        raise ValidationError(f"Field `{name}#{k}`: len(`{v}`) is not equal `{size}`")

    return v
Beispiel #3
0
def _validate_max_value(name, o: Options, k, v):
    if o.max_value is None:
        return v

    max_value = o.max_value

    if callable(max_value):
        max_value = max_value()

    if max_value is not None and v > max_value:
        raise ValidationError(f"Field `{name}#{k}`: value `{v}` is greater than required")

    return v
Beispiel #4
0
def _validate_min_value(name, o: Options, k, v):
    if o.min_value is None:
        return v

    min_value = o.min_value

    if callable(min_value):
        min_value = min_value()

    if min_value is not None and v < min_value:
        raise ValidationError(f"Field `{name}#{k}`: value `{v}` is less than required")

    return v
Beispiel #5
0
def _validate_max_length(name, o: Options, k, v):
    if o.max_length is None:
        return v

    max_length = o.max_length

    if callable(max_length):
        max_length = max_length()

    if max_length is not None and len(v) > max_length:
        raise ValidationError(f"Field `{name}#{k}`: len(`{v}`) is greater than required")

    return v
Beispiel #6
0
def _validate_min_length(name, o: Options, k, v):
    if o.min_length is None:
        return v

    min_length = o.min_length

    if callable(min_length):
        min_length = min_length()

    if min_length is not None and len(v) < min_length:
        raise ValidationError(f"Field `{name}#{k}`: len(`{v}`) is less than required")

    return v
Beispiel #7
0
def _validate_allowed(name, o: Options, k, v):
    if o.allowed is None:
        return v

    allowed = o.allowed

    if callable(o.allowed):
        allowed = o.allowed()

    if allowed and v not in allowed:
        raise ValidationError(f"Field `{name}#{k}`: value `{v}` is not allowed. Allowed vs: `{allowed}`")

    return v
Beispiel #8
0
def _validate_size(name, options: Options, key, value):
    if options.size is None:
        return value

    size = options.size

    if callable(size):
        size = size()

    if size is not None and size != len(value):
        raise ValidationError(
            f"Field `{name}#{key}`: len(`{value}`) is not equal `{size}`")

    return value
Beispiel #9
0
def _validate_max_value(name, options: Options, key, value):
    if options.max_value is None:
        return value

    max_value = options.max_value

    if callable(max_value):
        max_value = max_value()

    if max_value is not None and value > max_value:
        raise ValidationError(
            f"Field `{name}#{key}`: value `{value}` is greater than required")

    return value
Beispiel #10
0
def _validate_min_value(name, options: Options, key, value):
    if options.min_value is None:
        return value

    min_value = options.min_value

    if callable(min_value):
        min_value = min_value()

    if min_value is not None and value < min_value:
        raise ValidationError(
            f"Field `{name}#{key}`: value `{value}` is less than required")

    return value
Beispiel #11
0
def _validate_max_length(name, options: Options, key, value):
    if options.max_length is None:
        return value

    max_length = options.max_length

    if callable(max_length):
        max_length = max_length()

    if max_length is not None and len(value) > max_length:
        raise ValidationError(
            f"Field `{name}#{key}`: len(`{value}`) is greater than required")

    return value
Beispiel #12
0
def _validate_allowed(name, options: Options, key, value):
    if options.allowed is None:
        return value

    allowed = options.allowed

    if callable(options.allowed):
        allowed = options.allowed()

    if allowed and value not in allowed:
        raise ValidationError(
            f"Field `{name}#{key}`: value `{value}` is not allowed. Allowed vs: `{allowed}`"
        )

    return value
Beispiel #13
0
def _strip_unknown(cls, unknown, strip_unknown=False):
    if not strip_unknown and len(unknown):
        raise ValidationError(f"{cls}.__new__() got an unexpected keyword arguments {unknown.keys()}")
Beispiel #14
0
def _check_types(name, o: Options, k, v):
    if not is_compatible(v, o.get_type()):
        raise ValidationError(f"Field `{name}#{k}`: {o.get_type()} is not compatible with value `{v}`:{type(v)}")

    return v