Merge pull request #2831 from NobodysNightmare/feature/prohibit_float_for_int

Fix validations for some custom value types
pull/2838/merge
Alex Coles 10 years ago
commit a434f2d998
  1. 12
      app/models/custom_value/date_strategy.rb
  2. 12
      app/models/custom_value/int_strategy.rb
  3. 14
      spec/models/custom_value/bool_strategy_spec.rb
  4. 7
      spec/models/custom_value/date_strategy_spec.rb
  5. 15
      spec/models/custom_value/float_strategy_spec.rb
  6. 14
      spec/models/custom_value/int_strategy_spec.rb

@ -35,9 +35,13 @@ class CustomValue::DateStrategy < CustomValue::FormatStrategy
end
def validate_type_of_value
Date.iso8601(value)
nil
rescue
:not_a_date
return nil if value.is_a? Date
begin
Date.iso8601(value)
nil
rescue
:not_a_date
end
end
end

@ -35,9 +35,13 @@ class CustomValue::IntStrategy < CustomValue::FormatStrategy
end
def validate_type_of_value
Kernel.Integer(value)
nil
rescue
:not_an_integer
return :not_an_integer if value.is_a? Float
begin
Kernel.Integer(value)
nil
rescue
:not_an_integer
end
end
end

@ -113,5 +113,19 @@ describe CustomValue::BoolStrategy do
is_expected.to be_nil
end
end
context 'value is true' do
let(:value) { true }
it 'accepts' do
is_expected.to be_nil
end
end
context 'value is false' do
let(:value) { false }
it 'accepts' do
is_expected.to be_nil
end
end
end
end

@ -83,5 +83,12 @@ describe CustomValue::DateStrategy do
is_expected.to eql(:not_a_date)
end
end
context 'value is valid date' do
let(:value) { Date.iso8601('2015-01-03') }
it 'accepts' do
is_expected.to be_nil
end
end
end
end

@ -76,5 +76,20 @@ describe CustomValue::FloatStrategy do
is_expected.to eql(:not_a_number)
end
end
context 'value is float' do
let(:value) { 3.14 }
it 'accepts' do
is_expected.to be_nil
end
end
context 'value is int' do
let(:value) { 3 }
it 'accepts' do
# accepting here, as we can "losslessly" convert
is_expected.to be_nil
end
end
end
end

@ -76,5 +76,19 @@ describe CustomValue::IntStrategy do
is_expected.to eql(:not_an_integer)
end
end
context 'value is an actual int' do
let(:value) { 10 }
it 'accepts' do
is_expected.to be_nil
end
end
context 'value is a float' do
let(:value) { 2.3 }
it 'rejects' do
is_expected.to eql(:not_an_integer)
end
end
end
end

Loading…
Cancel
Save