[Proposal] Add `min` and `max` alias to LengthValidator

Problem

Consider the following code:

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.

Do you think it’s worth adding?

1 Like

I think that was a good idea and I want to do it :laughing:

I like this idea too. @RobertChang do you want to go ahead and make a PR?

Hi @ghiculescu

Sure, I will try to make a PR for this idea

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.

2 Likes

Guys, I made a PR for this.

hopes it would be merged :laughing:

1 Like