Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Length validation handles correctly nil. Fix #7180

When nil or empty string are not allowed, they are not valid.
  • Loading branch information...
commit 08b018039ab6c19e6fe5d1ad02adbca537144d29 1 parent 02c30c6
Michal Zima authored
16 activemodel/lib/active_model/validations/length.rb
@@ -14,6 +14,10 @@ def initialize(options)
14 14 options[:minimum], options[:maximum] = range.min, range.max
15 15 end
16 16
  17 + if options[:allow_blank] == false && options[:minimum].nil? && options[:is].nil?
  18 + options[:minimum] = 1
  19 + end
  20 +
17 21 super
18 22 end
19 23
@@ -40,7 +44,10 @@ def validate_each(record, attribute, value)
40 44
41 45 CHECKS.each do |key, validity_check|
42 46 next unless check_value = options[key]
43   - next if value_length.send(validity_check, check_value)
  47 +
  48 + if !value.nil? || skip_nil_check?(key)
  49 + next if value_length.send(validity_check, check_value)
  50 + end
44 51
45 52 errors_options[:count] = check_value
46 53
@@ -58,6 +65,10 @@ def tokenize(value)
58 65 options[:tokenizer].call(value)
59 66 end || value
60 67 end
  68 +
  69 + def skip_nil_check?(key)
  70 + key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
  71 + end
61 72 end
62 73
63 74 module HelperMethods
@@ -79,7 +90,8 @@ module HelperMethods
79 90 #
80 91 # Configuration options:
81 92 # * <tt>:minimum</tt> - The minimum size of the attribute.
82   - # * <tt>:maximum</tt> - The maximum size of the attribute.
  93 + # * <tt>:maximum</tt> - The maximum size of the attribute. Allows +nil+ by
  94 + # default if not used with :minimum.
83 95 # * <tt>:is</tt> - The exact size of the attribute.
84 96 # * <tt>:within</tt> - A range specifying the minimum and maximum size of
85 97 # the attribute.
39 activemodel/test/cases/validations/length_validation_test.rb
@@ -375,4 +375,43 @@ def test_validates_length_of_for_infinite_maxima
375 375 t.author_name = "A very long author name that should still be valid." * 100
376 376 assert t.valid?
377 377 end
  378 +
  379 + def test_validates_length_of_using_maximum_should_not_allow_nil_when_nil_not_allowed
  380 + Topic.validates_length_of :title, :maximum => 10, :allow_nil => false
  381 + t = Topic.new
  382 + assert t.invalid?
  383 + end
  384 +
  385 + def test_validates_length_of_using_maximum_should_not_allow_nil_and_empty_string_when_blank_not_allowed
  386 + Topic.validates_length_of :title, :maximum => 10, :allow_blank => false
  387 + t = Topic.new
  388 + assert t.invalid?
  389 +
  390 + t.title = ""
  391 + assert t.invalid?
  392 + end
  393 +
  394 + def test_validates_length_of_using_both_minimum_and_maximum_should_not_allow_nil
  395 + Topic.validates_length_of :title, :minimum => 5, :maximum => 10
  396 + t = Topic.new
  397 + assert t.invalid?
  398 + end
  399 +
  400 + def test_validates_length_of_using_minimum_0_should_not_allow_nil
  401 + Topic.validates_length_of :title, :minimum => 0
  402 + t = Topic.new
  403 + assert t.invalid?
  404 +
  405 + t.title = ""
  406 + assert t.valid?
  407 + end
  408 +
  409 + def test_validates_length_of_using_is_0_should_not_allow_nil
  410 + Topic.validates_length_of :title, :is => 0
  411 + t = Topic.new
  412 + assert t.invalid?
  413 +
  414 + t.title = ""
  415 + assert t.valid?
  416 + end
378 417 end

0 comments on commit 08b0180

Please sign in to comment.
Something went wrong with that request. Please try again.