John
John

Reputation: 443

Rspec Validation Tests for Date and Boolean

I want to re-write two custom validation tests, one for Dates and one for Boolean values without having to use the custom methods that I've created. Is there a shorthand way to validate Date or Boolean values (as being of those type) using shoulda-matchers or valid-attribute gems?

I would like to steer away from having to use another gem if possible. I have not included my code because I'm not looking for a custom re-write, just shorthand generic validation syntax.

Upvotes: 1

Views: 726

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29419

The accepted way to validate a boolean field is:

validates :field, :inclusion => {:in => [true, false]}

I don't know if there is as much consensus on validating dates, but I'm aware of the following gem options:

although both of these only claim support through Rails 3.

A custom Rails 4 solution is described in http://andowebsit.es/blog/noteslog.com/post/how-to-validate-dates-in-rails-4/

Upvotes: 1

Related Questions