custom validations only if all attributes are valid

I think I'd be much more annoyed by this kind of behavior. "Okay,
these two things are wrong. Let's fix them. Wait, now it's _still_
wrong, with something totally unrelated?"

You could fix this case almost trivially by 'if age.to_i < 16', right?
I mean, I know you're talking about a more general case, but...

I agree with Steve, when we call valid?, we expect all validations to run. If you want to skip some validation in case errors already exist in the object for any particular reason, you can always to that as you said: if errors.empty?. This will give you the behavior you expect, although I’d recommend avoiding this and running all validations.

Agreed, and by “running all validations” you can have a certain validator that acts as a state machine on a set of validations that gives more meaningful errors as the state moves forward. I think most of us have done that in the past to get the behavior you are looking for. Changing core behavior for that case does not make sense.

  • Ken

Steve, It’s easy to reply to your post, because you have a code example :slight_smile:

In you case, you will have to duplicate all checks in your custom validations, that is not DRY approach.

You will duplicate .to_i in all methods that will depends on age as integer.

I agree with you. Technically I can check that errors are empty, but I have to do it in all my custom validations.
it will not look nice :frowning:
What I offer is a two stage validation. When on the second stage you will be sure that all data in correct state and you can use it with confidence.
Anyway you will rely on one valid? method.

What I added into my project is:

The reason I did it is a lot of exceptions we got when our clients didn’t provide all necessary data (or in correct format).
The obvious solution of course was to manually check all data in custom validations, but why I need to duplicate code if AR/AM already implemented it?