Ejemplo n.º 1
0
 def validate(self, value):
     errors = []
     new_value = value
     if isinstance(value, self.enum_class):
         return errors, new_value
     if not isinstance(value, str):
         errors.append(ErrorInfo(ErrorCode.NotEnumError))
         return errors, None
     for i, enum_value in enumerate(self.enum_class):
         if value == enum_value.name:
             new_value = enum_value
             return errors, new_value
     errors.append(ErrorInfo(ErrorCode.NotDatetimeStringError))
     return errors, None
Ejemplo n.º 2
0
 def validate(self, value):
     errors = []
     new_value = value
     if isinstance(value, datetime):
         return errors, new_value
     if not isinstance(value, str):
         errors.append(ErrorInfo(ErrorCode.NotDatetimeError))
         return errors, None
     try:
         new_value = datetime.strptime(new_value, DATETIME_FORMAT)
     except Exception as ex:
         errors.append(ErrorInfo(ErrorCode.NotDatetimeStringError))
         return errors, None
     return errors, new_value
Ejemplo n.º 3
0
def sql_alchemy_error_handler(error: DBAPIError):
    if error.orig is None:
        response = jsonify(message=ErrorInfo(ErrorCode.UnknownError).to_dict(),
                           orig=str(error))
        response.status_code = 500
        return response
    if error.orig.args[0] == 1062:  # not unique mysql error
        response = jsonify(message=ErrorInfo(
            ErrorCode.NotUniqueError).to_dict(),
                           orig=error.orig.args[1])
    else:
        response = jsonify(message=ErrorInfo(ErrorCode.UnknownError).to_dict(),
                           orig=error.orig.args[1])
    response.status_code = 500
    return response
Ejemplo n.º 4
0
 def validate(self, value):
     errors = []
     new_value = value
     if isinstance(value, bool):
         return errors, new_value
     if isinstance(value, int):
         new_value = bool(value)
         return errors, new_value
     if isinstance(value, str):
         lower_value = value.lower().strip()
         if lower_value == 'true':
             new_value = True
         elif lower_value == 'false':
             new_value = False
         else:
             errors.append(ErrorInfo(ErrorCode.NotBoolStringError))
         return errors, None if errors else new_value
     errors.append(ErrorInfo(ErrorCode.NotBoolError))
     return errors, None
Ejemplo n.º 5
0
 def validate(self, value):
     errors = []
     new_value = value
     if isinstance(value, int):
         return errors, new_value
     if not isinstance(value, str):
         errors.append(ErrorInfo(ErrorCode.NotIntegerError))
         return errors, None
     try:
         new_value = int(new_value)
     except Exception as ex:
         errors.append(ErrorCode.NotIntegerStringError)
         return errors, None
     return errors, new_value
Ejemplo n.º 6
0
def unknown_error_handler(error: Exception):
    response = jsonify(message=ErrorInfo(ErrorCode.UnknownError).to_dict(),
                       orig=str(error))
    response.status_code = 500
    return response
Ejemplo n.º 7
0
 def validate(self, value):
     errors = []
     new_value = value
     if not isinstance(value, str):
         errors.append(ErrorInfo(ErrorCode.NotStringError))
     return errors, None if errors else new_value
Ejemplo n.º 8
0
 def validate(self, value):
     errors = []
     new_value = value
     if value not in self.allowed_values:
         errors.append(ErrorInfo(ErrorCode.NotAllowedValueError))
     return errors, None if errors else new_value
Ejemplo n.º 9
0
 def validate(self, value):
     errors, new_value = self.string_validator.validate(value)
     if not errors and not any(regex for regex in self.url_regexs if re.match(regex, value)):
         errors.append(ErrorInfo(ErrorCode.NotPostUrlStringError))
     return errors, None if errors else new_value
Ejemplo n.º 10
0
 def validate(self, value):
     errors, new_value = self.string_validator.validate(value)
     if not errors and len(value) < self.min_length:
         errors.append(ErrorInfo(ErrorCode.MaxLengthStringError, min_length=self.min_length))
     return errors, None if errors else new_value
Ejemplo n.º 11
0
 def validate(self, value):
     errors, new_value = self.integer_validator.validate(value)
     if not errors and new_value < self.min_value:
         errors.append(ErrorInfo(ErrorCode.MinValueError, min_value=self.min_value))
     return errors, None if errors else new_value