Skip to content
This repository has been archived by the owner on Aug 27, 2023. It is now read-only.

Is it possible to get all validation errors? #29

Open
justinoue opened this issue Oct 12, 2015 · 3 comments
Open

Is it possible to get all validation errors? #29

justinoue opened this issue Oct 12, 2015 · 3 comments

Comments

@justinoue
Copy link

I'm working on a some basic crud stuff and noticed that flywheel appears to raise an exception on the first validation failure and that's it.

Is it possible to get a list of all the fields with failed validations?

@justinoue
Copy link
Author

Just a quick example of what I'm thinking of.

    def pre_save_(self, engine):
        """ Called before saving items """
        self.__engine__ = engine
        v_err = []
        for field in six.itervalues(self.meta_.fields):
            try:
              field.validate(self)
            except ValueError, e:
              v_err.append(e.message)
        if len(v_err) > 0:
          raise ValueError("{}".format(v_err))

Not sure how best to integrate this as an optional feature, possibly an option you can set when defining the model?

Custom error messages would be great too but I digress.

@justinoue
Copy link
Author

I've made a Base class/mixin to subclass along with Model to add this feature if anyone is interested in it:

class Base(object):
  def validate(self):
    self._fields_with_errors = []
    for field in self.meta_.fields.itervalues():
      try:
        field.validate(self)
      except ValueError, e:
        res = re.search('Validation check on field (.*) failed for value (.*)', e.message)
        self._fields_with_errors.append(res.group(1))
    return len(self._fields_with_errors) == 0

@stevearc
Copy link
Owner

This seems like a pretty good idea. I'll put it on my list of things to do for the next version.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants