def validator(value): if not isinstance(value, dict): raise ValidationError(errors['type'].format(type_name='object')) validated = {} invalid = {} for key, validator in spec.items(): try: item = value[key] except KeyError: # No value included for this field. try: item = validator.default_value except AttributeError: # A missing required field. invalid[key] = errors['required'] else: # An `optional()` field. if item is not ignore: # An `optional()` field with a default. validated[key] = item else: # A value has been included for this field. try: validated[key] = validator(item) except ValidationError as exc: invalid[key] = exc.description if invalid: raise ValidationError(invalid) return validated
def validator(value): if is_null(value, allow_null): return None if isinstance(value, string_types) and len(value) > 100: raise ValidationError(errors['too_large']) try: value = decimal.Decimal(value) except (ValueError, decimal.InvalidOperation): raise ValidationError(errors['value'].format(type_name='number')) except TypeError: raise ValidationError(errors['type'].format(type_name='number')) if quantizer is not None: value = value.quantize(quantizer, rounding=decimal.ROUND_HALF_UP) if max_value is not None and value > max_value: raise ValidationError( errors['max_value'].format(max_value=max_value)) if min_value is not None and value < min_value: raise ValidationError( errors['min_value'].format(min_value=min_value)) return value
def is_null(value, allow_null): if value not in ('', None): return False if not allow_null: if value is None: raise ValidationError(errors['null']) raise ValidationError(errors['blank']) return True
def validator(value): if is_null(value, allow_null): return None if isinstance(value, datetime.time): return value if not isinstance(value, string_types): raise ValidationError(errors['type'].format(type_name='time')) if len(value) > 100: raise ValidationError(errors['too_large']) try: return parse_iso8601_time(value) except ValueError: raise ValidationError(errors['value'].format(type_name='time'))
def validator(value): if is_blank(value, allow_blank): return False if not isinstance(value, valid_types): raise ValidationError(errors['type'].format(type_name='boolean')) if isinstance(value, string_types): value = value.lower() if value in true_values: return True if value in false_values: return False raise ValidationError(errors['value'].format(type_name='boolean'))
def validator(value): if not isinstance(value, list): raise ValidationError(errors['type'].format(type_name='list')) if not allow_empty and not value: raise ValidationError(errors['empty']) validated = [] for idx, item in enumerate(value): try: validated.append(child_validator(item)) except ValidationError as exc: index_msg = errors['index'].format(index=idx) raise ValidationError(index_msg + ' ' + exc.description) return validated
def validator(value): if value is None: raise ValidationError(errors['null']) if not isinstance(value, string_types): raise ValidationError(errors['type'].format(type_name='string')) if trim_whitespace: value = value.strip() if is_blank(value, allow_blank): return '' if (max_length is not None) and (len(value) > max_length): raise ValidationError( errors['max_length'].format(max_length=max_length)) if (min_length is not None) and (len(value) < min_length): raise ValidationError( errors['min_length'].format(min_length=min_length)) return value
def wrapper(*args, **kwargs): """ 3. When a function decorated by `@validate()` is called, this wrapper function is what actaully gets executed. """ # Turn any positional arguments into keyword arguments instead. for idx, value in enumerate(args): key = arg_names[idx] kwargs[key] = value # Validate any inputs as required, collecting any errors. errors = {} for key, value in kwargs.items(): if key in validated: validator = validated[key] try: kwargs[key] = validator(value) except ValidationError as exc: errors[key] = exc.description # If any errors occured, then fail. if errors: raise ValidationError(errors) # Call the underlying function. return func(**kwargs)
def validator(value): if not isinstance(value, dict): raise ValidationError(errors['type'].format(type_name='object')) if not allow_empty and not value: raise ValidationError(errors['empty']) validated = {} invalid = {} for key, item in value.items(): try: validated[key] = child_validator(item) except ValidationError as exc: invalid[key] = exc.description if invalid: raise ValidationError(invalid) return validated
def validator(value): if is_null(value, allow_null): return None if isinstance(value, string_types) and len(value) > 100: raise ValidationError(errors['too_large']) try: value = float(value) except TypeError: raise ValidationError(errors['type'].format(type_name='number')) except ValueError: raise ValidationError(errors['value'].format(type_name='number')) if (max_value is not None) and (value > max_value): raise ValidationError( errors['max_value'].format(max_value=max_value)) if (min_value is not None) and (value < min_value): raise ValidationError( errors['min_value'].format(min_value=min_value)) return value
def validator(value): value = base_validator(value) if ':' not in value: raise ValidationError(errors['value'].format(type_name='url')) return value