stevearc / flywheel

Object mapper for Amazon's DynamoDB
MIT License
128 stars 25 forks source link

Is it possible to get all validation errors? #29

Open justinoue opened 9 years ago

justinoue commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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