Skip to content

Commit

Permalink
support for multiple errors validation
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Jan 19, 2016
1 parent c234dfe commit 3ed8c0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/quorum/__init__.py
Expand Up @@ -86,7 +86,7 @@
from .errors import errors_json
from .exceptions import BaseError, ServerInitError, ModuleNotFound, OperationalError,\
NotFoundError, ValidationError, BaseInternalError, ValidationInternalError,\
HttpError, JsonError
ValidationMultipleError, HttpError, JsonError
from .execution import ExecutionThread, background, insert_work, interval_work,\
seconds_work, daily_work, weekly_work, monthly_work, seconds_eval, daily_eval,\
weekly_eval, monthly_eval
Expand Down
23 changes: 23 additions & 0 deletions src/quorum/exceptions.py
Expand Up @@ -156,6 +156,29 @@ def __init__(self, name, message):
BaseInternalError.__init__(self, message)
self.name = name

class ValidationMultipleError(ValidationInternalError):
"""
Exception/error considered to be equivalent to the
validation internal error, with the exception that it
may handle multiple errors at the same time.
"""

errors = []
""" The sequence containing the multiple errors associated
with the validation multiple error """

def __init__(self, name = None, message = None):
ValidationInternalError.__init__(self, name, message)
self.errors = []

def add_error(self, name, message):
if not self.name: self.name = name
if not self.message: self.message = message
self.errors.append((name, message))

def add_exception(self, exception):
self.add_error(exception.name, exception.message)

class HttpError(BaseError):
"""
Error raised when an http (client) related issue
Expand Down
14 changes: 13 additions & 1 deletion src/quorum/validation.py
Expand Up @@ -93,22 +93,34 @@ def validate(method = None, methods = [], object = None, ctx = None, build = Tru
# handles the failure setting the value as an empty map
data_j = util.request_json()

# uses all the values referencing data in the request to try
# to populate the object this way it may be constructed using
# any of theses strategies (easier for the developer)
for name, value in data_j.items(): object[name] = value
for name, value in flask.request.files.items(): object[name] = value
for name, value in flask.request.form.items(): object[name] = value
for name, value in flask.request.args.items(): object[name] = value

# iterates over the complete set of methods registered for validation
# and runs them expecting exceptions to be raised from them, each adding
# new errors to the current errors stack
for method in methods:
try: method(object, ctx = ctx)
except exceptions.ValidationMultipleError as error:
errors.extend(error.errors)
except exceptions.ValidationInternalError as error:
errors.append((error.name, error.message))

errors_map = {}
# creates the map that will be used to store the association between the
# field name of the object and the validation errors associated with it
errors_map = dict()
for name, message in errors:
if not name in errors_map: errors_map[name] = []
_errors = errors_map[name]
_errors.append(message)

# returns both the newly created errors map that associates each of the
# model name with the sequence of errors and the validated object (state)
return errors_map, object

def validate_b(method = None, methods = [], object = None, build = True):
Expand Down

0 comments on commit 3ed8c0b

Please sign in to comment.