-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 8.0.8
-
Component/s: Validations
With Mongoid 8.0.8, when we use validates_numericality_of with allow_blank: true and assign a string, the validation pass as you can see in this example :
require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'concurrent-ruby', '1.3.4' gem 'mongoid', '~> 8.0.8' # gem 'mongoid', '7.5.4' end Mongoid.configure do |config| config.clients.default = { hosts: ['mongo:27017'], database: 'mongoid_test', } end Mongoid.purge! class Article include Mongoid::Document field :send_delay, type: Integer validates_numericality_of :send_delay, allow_blank: true end article = Article.new(send_delay: 'abc') pp article.send_delay # => nil pp article.valid? # => true pp article.errors # => []
With Mongoid 7.5, it gives this result :
pp article.send_delay # => 0 pp article.valid? # => false pp article.errors # => <ActiveModel::Errors [#<ActiveModel::Error attribute=send_delay, type=not_a_number, options={:allow_blank=>true, :value=>"abc"}>]>
I'm not sure, but maybe read_attribute_for_validation have to be changed so return the raw attribute.
It happens because on this line, value is nil instead of 0 so the validation pass.