hoodoo 2.2.1 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74ed5c7f8e4d541e0fa485aa51d93c35e10e010749e372b1d007ade4550f3ce9
4
- data.tar.gz: 236eca0e1da38dce2e63b529f38d7e3d52d75f4685fbaf81341368721f7d4ac7
3
+ metadata.gz: d3cfd418d564c82821044bc2a95cb8d1e194a134027bc9fac61a56f823157b61
4
+ data.tar.gz: abd1ca2e208d34fbd00146989234daac40611f24e32869fd56debb0b1d0e62a6
5
5
  SHA512:
6
- metadata.gz: ad755f6aa427b937706e02634b752ec629d6a8ed2c472fb65531eb921d1060d31bbce57a94f20a971a106b681250eb202393135afe9d651f287be47124e5bcca
7
- data.tar.gz: a8a3452c1c155825b40696dc7b9f943b28976f2d59255977e94d180bda56b767a670beecd8bf309e327a8bb17c014891614665dd5517e8bbdc0d0794803d0275
6
+ metadata.gz: c887163252c009e44267179282e725bcfc4a49ce20e48270ab688f446dd5a03bf29bdaeab692ae4411b0f38dca2a202edd3b7025336963e29f68cd612f12ce3f
7
+ data.tar.gz: fa52f3e1fdb454022cd24fbdf56cdf25b3355ee0fe3cced729ae2cb73437b14c3d137f1e2958fb7d2d636222b0cf301fa4d356a7226125b31a75a0b797ffafd9
@@ -7,6 +7,14 @@ module Hoodoo
7
7
  #
8
8
  class Decimal < Hoodoo::Presenters::Field
9
9
 
10
+ # In theory this is derived from Rubinius source, but "master" didn't seem
11
+ # to include it at the time of writing. See:
12
+ #
13
+ # https://github.com/rubinius/rubinius/
14
+ # https://stackoverflow.com/questions/1034418/determine-if-a-string-is-a-valid-float-value
15
+ #
16
+ VALIDATOR = Regexp.new('^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$')
17
+
10
18
  # The precision of the Decimal.
11
19
  #
12
20
  attr_accessor :precision
@@ -28,11 +36,21 @@ module Hoodoo
28
36
 
29
37
  # Check if data is a valid Decimal and return a Hoodoo::Errors instance.
30
38
  #
39
+ # Decimals are expressed in JSON as Strings with any amount of leading or
40
+ # trailing space, can be positive or negative and may use simple (e.g.
41
+ # <tt>"-12.45"</tt>) or scientific (e.g. <tt>"-0.1245e2"</tt>) notation
42
+ # with a lower case or capital <tt>E</tt> in the latter case.
43
+ #
44
+ # A leading "0" before a decimal place may be omitted; "0.12" and ".12"
45
+ # are considered equivalent and valid. An optional leading "+" is allowed
46
+ # for positive numbers. Between digits, an underscore is permitted as a
47
+ # visual separator; "12_431_999" and "12431999" are equivalent and valid.
48
+ #
31
49
  def validate( data, path = '' )
32
50
  errors = super( data, path )
33
51
  return errors if errors.has_errors? || ( ! @required && data.nil? )
34
52
 
35
- unless data.is_a?( ::BigDecimal )
53
+ unless data.is_a?( ::String ) && data.match( VALIDATOR ) != nil
36
54
  errors.add_error(
37
55
  'generic.invalid_decimal',
38
56
  :message => "Field `#{ full_path( path ) }` is an invalid decimal",
@@ -12,11 +12,11 @@ module Hoodoo
12
12
  # The Hoodoo gem version. If this changes, be sure to re-run
13
13
  # <tt>bundle install</tt> or <tt>bundle update</tt>.
14
14
  #
15
- VERSION = '2.2.1'
15
+ VERSION = '2.2.2'
16
16
 
17
17
  # The Hoodoo gem date. If this changes, be sure to re-run
18
18
  # <tt>bundle install</tt> or <tt>bundle update</tt>.
19
19
  #
20
- DATE = '2017-11-10'
20
+ DATE = '2017-11-16'
21
21
 
22
22
  end
@@ -274,7 +274,7 @@ describe Hoodoo::Presenters::Array do
274
274
  'boolean' => { :valid => [ true ], :invalid => [ 4.51, 'false' ] },
275
275
  'date' => { :valid => [ Date.today.iso8601 ], :invalid => [ Date.today, '23rd January 2041' ] },
276
276
  'date_time' => { :valid => [ DateTime.now.iso8601 ], :invalid => [ DateTime.now, '2017-01-27 12:00' ] },
277
- 'decimal' => { :valid => [ BigDecimal.new(4.51, 2) ], :invalid => [ 4.51, '4.51' ] },
277
+ 'decimal' => { :valid => [ '4.51' ], :invalid => [ 4.51, BigDecimal.new( '4.51' ) ] },
278
278
  'enum' => { :valid => [ 'one' ], :invalid => [ 'One', 1 ] },
279
279
  'float' => { :valid => [ 4.51 ], :invalid => [ BigDecimal.new(4.51, 2), '4.51' ] },
280
280
  'integer' => { :valid => [ 4 ], :invalid => [ '4' ] },
@@ -457,8 +457,8 @@ describe Hoodoo::Presenters::Array do
457
457
  it 'validates entries' do
458
458
  data = {
459
459
  'numbers' => [
460
- BigDecimal.new( '42.21' ),
461
- 'not a decimal'
460
+ '42.21',
461
+ '41.0 not a decimal'
462
462
  ]
463
463
  }
464
464
 
@@ -16,14 +16,69 @@ describe Hoodoo::Presenters::Decimal do
16
16
 
17
17
  describe '#validate' do
18
18
  it 'should return [] when valid decimal' do
19
- expect(@inst.validate(BigDecimal.new(12.231,3)).errors).to eq([])
19
+ [
20
+ '0',
21
+ '0.0',
22
+ ' .123245e2 ',
23
+ ' .1_2_3_2_4_5E-2 ',
24
+ ' 0.123245e+2 ',
25
+ ' 0_3.123245E2 ',
26
+ ' 03.123245E2_3 ',
27
+ ' 12.3245 ',
28
+ ' 12.3245',
29
+ '12.3245 ',
30
+ '12.3245',
31
+ '12.00',
32
+ '12',
33
+ ' -.123245e2 ',
34
+ ' -.123245E2 ',
35
+ ' -0.123245e2 ',
36
+ ' -0.123245E2 ',
37
+ ' -12.3245 ',
38
+ ' -12.3245',
39
+ '-12.3245 ',
40
+ '-12.3245',
41
+ '-12.00',
42
+ '-12',
43
+ ' +.123245e2 ',
44
+ ' +.123245E2 ',
45
+ ' +0.123245e2 ',
46
+ ' +0.123245E2 ',
47
+ ' +12.3245 ',
48
+ ' +12.3245',
49
+ '+12.3245 ',
50
+ '+12.3245',
51
+ '+12.00',
52
+ '+12'
53
+ ].each do | item |
54
+ expect( @inst.validate( item ).errors ).to eq( [] )
55
+ end
56
+
20
57
  end
21
58
 
22
59
  it 'should return correct error when data is not a decimal' do
23
- errors = @inst.validate('asckn')
24
-
25
- err = [ {'code'=>"generic.invalid_decimal", 'message'=>"Field `one` is an invalid decimal", 'reference'=>"one"}]
26
- expect(errors.errors).to eq(err)
60
+ [
61
+ 'hello',
62
+ '!23.00',
63
+ '24!',
64
+ '23.41 suffix',
65
+ '+0.123245j2',
66
+ '0.123245e+-2',
67
+ '+-0.123245e2',
68
+ '03_.123245E2',
69
+ '03_.123245E2',
70
+ '03._123245E2',
71
+ '03.123245_E2',
72
+ '03.123245E23_',
73
+ ].each do | item |
74
+ expect( @inst.validate( item ).errors ).to eq( [
75
+ {
76
+ 'code' => "generic.invalid_decimal",
77
+ 'message' => "Field `one` is an invalid decimal",
78
+ 'reference' => "one"
79
+ }
80
+ ] )
81
+ end
27
82
  end
28
83
 
29
84
  it 'should not return error when not required and absent' do
@@ -598,7 +598,7 @@ describe Hoodoo::Presenters::Hash do
598
598
  'boolean' => { :valid => [ true ], :invalid => [ 4.51, 'false' ] },
599
599
  'date' => { :valid => [ Date.today.iso8601 ], :invalid => [ Date.today, '23rd January 2041' ] },
600
600
  'date_time' => { :valid => [ DateTime.now.iso8601 ], :invalid => [ DateTime.now, '2017-01-27 12:00' ] },
601
- 'decimal' => { :valid => [ BigDecimal.new(4.51, 2) ], :invalid => [ 4.51, '4.51' ] },
601
+ 'decimal' => { :valid => [ '4.51' ], :invalid => [ 4.51, BigDecimal.new( '4.51' ) ] },
602
602
  'enum' => { :valid => [ 'one' ], :invalid => [ 'One', 1 ] },
603
603
  'float' => { :valid => [ 4.51 ], :invalid => [ BigDecimal.new(4.51, 2), '4.51' ] },
604
604
  'integer' => { :valid => [ 4 ], :invalid => [ '4' ] },
@@ -1166,7 +1166,7 @@ describe Hoodoo::Presenters::Hash do
1166
1166
  'boolean' => { :definition => { :length => 9, :type => :boolean }, :valid => [ true ], :invalid => [ 4.51, 'false' ] },
1167
1167
  'date' => { :definition => { :length => 9, :type => :date }, :valid => [ Date.today.iso8601 ], :invalid => [ Date.today, '23rd January 2041' ] },
1168
1168
  'date_time' => { :definition => { :length => 9, :type => :date_time }, :valid => [ DateTime.now.iso8601 ], :invalid => [ DateTime.now, '2017-01-27 12:00' ] },
1169
- 'decimal' => { :definition => { :length => 9, :type => :decimal, :field_precision => 2 }, :valid => [ BigDecimal.new(4.51, 2) ], :invalid => [ 4.51, '4.51' ] },
1169
+ 'decimal' => { :definition => { :length => 9, :type => :decimal, :field_precision => 2 }, :valid => [ '4.51' ], :invalid => [ 4.51, BigDecimal.new( '4.51' ) ] },
1170
1170
  'enum' => { :definition => { :length => 9, :type => :enum, :field_from => [ :one, :two, :three ] }, :valid => [ 'one' ], :invalid => [ 'One', 1 ] },
1171
1171
  'float' => { :definition => { :length => 9, :type => :float }, :valid => [ 4.51 ], :invalid => [ BigDecimal.new(4.51, 2), '4.51' ] },
1172
1172
  'integer' => { :definition => { :length => 9, :type => :integer }, :valid => [ 4 ], :invalid => [ '4' ] },
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loyalty New Zealand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-10 00:00:00.000000000 Z
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dalli