示例#1
0
def _validate_iterable(iterable_type, value):
    """Convert the iterable to iterable_type, or raise a Configuration
    exception.
    """
    if isinstance(value, six.string_types):
        msg = "Invalid iterable of type(%s): %s"
        raise ValidationError(msg % (tuple(value), value))

    try:
        return iterable_type(value)
    except TypeError:
        raise ValidationError("Invalid iterable: %s" % (value))
示例#2
0
def validate_log_level(value):
    """Validate a log level from a string value. Returns a constant from
    the :mod:`logging` module.
    """
    try:
        return getattr(logging, value)
    except AttributeError:
        raise ValidationError("Unknown log level: %s" % value)
示例#3
0
def validate_time(value):
    if isinstance(value, datetime.time):
        return value

    for format_ in time_formats:
        try:
            return datetime.time(*time.strptime(value, format_)[3:6])
        except ValueError:
            pass
    raise ValidationError("Invalid time format: %s" % value)
示例#4
0
def validate_datetime(value):
    if isinstance(value, datetime.datetime):
        return value

    for format in date_formats:
        try:
            return datetime.datetime.strptime(value, format)
        except ValueError:
            pass
    raise ValidationError("Invalid date format: %s" % value)
示例#5
0
def validate_numeric(type_func, value):
    try:
        return type_func(value)
    except ValueError:
        raise ValidationError("Invalid %s: %s" % (type_func.__name__, value))
示例#6
0
def validate_regex(value):
    try:
        return re.compile(value)
    except (re.error, TypeError) as e:
        raise ValidationError("Invalid regex: %s, %s" % (e, value))