Repository: fossasia/open-event-server

Issue:
Return meaningful validation errors in POST and PUT API
Instead of the generic `'Validation of \'%s\' field failed' % key` , more meaningful errors can be returned. 



Discussion:

Resolving patch:
diff --git a/open_event/api/custom_fields.py b/open_event/api/custom_fields.py
--- a/open_event/api/custom_fields.py
+++ b/open_event/api/custom_fields.py
@@ -13,6 +13,7 @@ class CustomField(Raw):
     Custom Field base class with validate feature
     """
     __schema_type__ = 'string'
+    validation_error = 'Validation of %s field failed'
 
     def __init__(self, *args, **kwargs):
         super(CustomField, self).__init__(**kwargs)
@@ -40,6 +41,7 @@ def validate_empty(self):
         Return when value is empty or null
         """
         if self.required:
+            self.validation_error = 'Required field %s. Cannot be null or empty'
             return False
         else:
             return True
@@ -62,6 +64,7 @@ def validate(self, value):
         if not value:
             return self.validate_empty()
         if not EMAIL_REGEX.match(value):
+            self.validation_error = 'Invalid Email in %s'
             return False
         return True
 
@@ -77,6 +80,7 @@ def validate(self, value):
         if not value:
             return self.validate_empty()
         if not URI_REGEX.match(value):
+            self.validation_error = 'Invalid Url in %s'
             return False
         return True
 
@@ -102,6 +106,7 @@ def validate(self, value):
             # using the same colour package used by sqlalchemy and wtforms
             colour.Color(value)
         except Exception:
+            self.validation_error = 'Invalid Color in %s'
             return False
         return True
 
@@ -134,6 +139,7 @@ def validate(self, value):
             else:
                 self.to_str(value)
         except Exception:
+            self.validation_error = 'Incorrect format of datetime used in %s field. Should be YYYY-MM-DD HH:MM:SS'
             return False
         return True
 
@@ -148,6 +154,7 @@ def validate(self, value):
         if value.__class__.__name__ in ['unicode', 'str']:
             return True
         else:
+            self.validation_error = '%s should be a String'
             return False
 
 
@@ -165,8 +172,10 @@ def validate(self, value):
         if value is None:
             return self.validate_empty()
         if type(value) != int:
+            self.validation_error = '%s should be an Integer'
             return False
         if self.positive and value < 0:
+            self.validation_error = '%s should be a positive Integer'
             return False
         return True
 
@@ -186,4 +195,5 @@ def validate(self, value):
             float(value)
             return True
         except Exception:
+            self.validation_error = '%s should be a Number'
             return False
diff --git a/open_event/api/helpers.py b/open_event/api/helpers.py
--- a/open_event/api/helpers.py
+++ b/open_event/api/helpers.py
@@ -154,7 +154,7 @@ def validate_payload(payload, api_model):
         if isinstance(field, CustomField) and hasattr(field, 'validate'):
             for i in data:
                 if not field.validate(i):
-                    _error_abort(400, 'Validation of \'%s\' field failed' % key)
+                    _error_abort(400, field.validation_error % ('\'%s\'' % key))
 
 
 def save_db_model(new_model, model_name, event_id=None):


Test patch: