class Post < ActiveRecord::Base
validates :title, length: { minium: 10, maximum: 20 }
end
post = Post.new(title: "Invalid")
puts post.valid?
This code prints “true” instead of “false”. The problem is that we have a typo: “minimum” is correct but there’s “minium”.
In short, when there’s a typo in either side of “minimum” or “maximum”, the validator doesn’t raise an error. And it’s easier to typo on “minimum” and “maximum” in my experience.
Solution
I believe we can add min and max aliases to LengthValidator options since it’s much less possible to typo on them. They are also the name of the methods in Range class so it’s easier to memorize.
I feel like this should raise an ArgumentError if the hash includes unknown keys. A simple assert_valid_keys should solve this and would avoid ambiguity of what happens when both maximum and max are provided. Also, this would be consistent with what association arguments do.