Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

validates_length_of should exclude last member of an end-excluding range

#2981

If provided with an end-excluding range, validates_length_of will not exclude the last member of the range.

In the code below, the current behavior of validates_length_of will consider the first_name attribute valid if it is 61 characters long.

class Person << ActiveRecord::Base
  validates_length_of :first_name, :in => 3...61
end

The attached patch corrects this by properly excluding the last member of an exclusive range.

Reported by jed · July 31st, 2009 @ 07:07 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. jed
  2. José Valim
    José Valim
    • Tag changed from patch, validates_length_of, validations to bugmash, patch, validates_length_of, validations

    August 8th, 2009 @ 03:02 PM

  3. Adam Keys
    Adam Keys

    The patch above appears to apply to master, unless I'm missing something. I've attached a patch that applies to 2-3-stable and verified it.

    August 8th, 2009 @ 06:43 PM

  4. David Trasbo
    David Trasbo

    +1

    Patch is applied cleanly to edge and all tests are passing.

    August 8th, 2009 @ 06:56 PM

  5. José Valim
    José Valim
    • State changed from new to incomplete

    Using range.min and range.max iterates through the whole collection. The patch should do something like:

      min = range.begin
      max = range.end
      max -= 1 if range.exclude_end?
    

    August 8th, 2009 @ 07:37 PM

  6. Adam Keys
    Adam Keys

    Reworked patch per Jose's comments.

    August 8th, 2009 @ 11:10 PM

  7. José Valim
    José Valim
    • State changed from incomplete to resolved
    • Tag changed from bugmash, patch, validates_length_of, validations to patch, validates_length_of, validations

    August 8th, 2009 @ 11:32 PM

  8. Repository