bcardarella / valid_attribute

Minimalist validation BDD for ActiveModel specs
178 stars 18 forks source link

feature: custom error messages #6

Closed vinibaggio closed 13 years ago

vinibaggio commented 13 years ago

Hello! First thanks for this gem, I really liked it! finally a decent approach to BDDing validations.

The problem is, sometimes we want custom error messages for a specific validations. I am implementing locally in my own fork of valid_attribute but I would like some feedback on implementation. I think the following is very good:

it { should_not have_valid(:url).when('localhost').with_message("can't be localhost") }

The problem is that the DSL would sound very weird in positive cases, such as

it { should have_valid(:url).when('google.com').with_message("can't be localhost") }

Do you have an Idea how this case could be handled? Thanks!

bcardarella commented 13 years ago

Thanks for the kind words :)

I had thought about including message validation. The reason I didn't include it was because the message itself is not a logic pathway. The message could be gibberish and the application can still work.

Also, ValidAttribute does not tie itself to any validation. It only cares if the values are valid or invalid. So I can provide as many valid or invalid test values I want. If you start including messages how would one associate the proper message with the value? For example not including any value could trigger a 'presence' validation and print a "no blank values" custom message. But I could also have a 'inclusion' validation on the attribute that has a custom message of "must be a valid US State". One could put the order of the custom messages in the order of the values but then you'd have to include the message for each value you're passing even if they are not custom messages. Then of course you can go one step further and ask to support i18n.

In the end I decided what is most important is this semantic way of testing if a value is invalid or not. I tend to look at the validation messages in the same way I look at the markup and text of my apps.

I'm going to close this issue but if you feel strongly about this please feel free to re-open and we can have a debate :)

vinibaggio commented 13 years ago

Yeah, I think you're completely right, I am convinced.

Thanks for your time :)