Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2237

validates_numericality_of doesn't work as expected

    • Type: Icon: Task Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Using mongoid (3.0.1):

      I have run into problems with the validator. Specifically, it will accept floats and even non-numbers for integer fields and the validation passes.

      class MyTest
        include Mongoid::Document
        include Mongoid::Timestamps
      
        field :filesize, :type => Integer
      
        validates_numericality_of :filesize, :greater_than_or_equal_to => 0, :only_integer => true
      end
      
      mt = MyTest.new
      mt.filesize = 5.9
      mt.valid? # true
      mt.filesize == 5 #true
      

      That shouldn't be valid, but it says it is and truncates it to 5.

      There's also this issue:

      mt = MyTest.new
      mt.filesize = 'horse'
      mt.valid? # true
      mt.filesize == 0 #true
      

      This seems directly related to issue MONGOID-2137, which was closed. However, this is a relevant issue for us as it breaks the ActiveRecord validation API.

            Assignee:
            Unassigned Unassigned
            Reporter:
            achappellAtUniqueltd achappellAtUniqueltd [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: