Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Do not validate Float unless precision and scale are explicitly defined
Browse files Browse the repository at this point in the history
[#1038 state:resolved]
  • Loading branch information
dkubb committed Sep 15, 2009
1 parent 711c7df commit 8f552a5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 8 deletions.
Expand Up @@ -80,8 +80,8 @@ def validate_numeric(value, errors)
precision = options[:precision]
scale = options[:scale]

regexp = if precision
if scale.nil? || precision > scale && scale == 0
regexp = if precision && scale
if precision > scale && scale == 0
/\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
elsif precision > scale
/\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
Expand Down
3 changes: 2 additions & 1 deletion dm-validations/spec/fixtures/basketball_player.rb
Expand Up @@ -26,7 +26,8 @@ class BasketballPlayer
# Validations
#

validates_is_number :height, :weight
# precision and scale need to be defined for length to be validated
validates_is_number :height, :weight, :precision => 10
end
BasketballPlayer.auto_migrate!

Expand Down
Expand Up @@ -14,7 +14,6 @@
it_should_behave_like "valid model"
end


describe "with height as integer" do
before :all do
@model.height = 198
Expand All @@ -23,7 +22,6 @@
it_should_behave_like "valid model"
end


describe "with height as string containing only integers" do
before :all do
@model.height = "198"
Expand All @@ -32,7 +30,6 @@
it_should_behave_like "valid model"
end


describe "with height as string containing a float" do
before :all do
@model.height = "198.1"
Expand Down Expand Up @@ -62,7 +59,6 @@
it_should_behave_like "invalid model"
end


describe "with height as string containing random punctuation characters" do
before :all do
@height = '$$ * $?'
Expand All @@ -76,7 +72,6 @@
it_should_behave_like "invalid model"
end


describe "with nil height" do
before :all do
@model.height = nil
Expand Down

0 comments on commit 8f552a5

Please sign in to comment.