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

Factor the way we reset sanitized object on failed validation. #11

Open
nicolasdespres opened this issue May 21, 2013 · 3 comments
Open
Labels

Comments

@nicolasdespres
Copy link
Owner

This chunk of code is duplicated in many places:

rescue ValidationError => e
  # Reset sanitized object.
  self.sanitized_object = nil
  raise e
end
@chiting
Copy link

chiting commented May 30, 2014

IMO, self.sanitized_object = nil should not lie in the rescue block. Then you can get rid of the rescue block.

@nicolasdespres
Copy link
Owner Author

The "sanitized_object" attribute has to be reset since the validation has failed otherwise it will be in an inconsistent state.

Actually, after a quick fresh look at the code I think the defect found its root an OO design flaw. The validator should be a functor with its own class hierarchy instead of a method inside the "schema" hierarchy. Having the validator and the schema melt together in one single hierarchy class breaks the principle of separating the code from the data. In our case the Schema is the data and the validator is the code.

Assuming we have these two concepts (validator and schema) separated in their class hierarchy, we can still keep the "sanitized_object" attribute for convenience. But then I think the chunk of code mentioned above will appears only once in the code and the duplication will gone.

@chiting
Copy link

chiting commented Jun 3, 2014

👍

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

No branches or pull requests

2 participants