File tree 3 files changed +58
-2
lines changed
lib/grape/validations/types
spec/grape/validations/types
3 files changed +58
-2
lines changed Original file line number Diff line number Diff line change 9
9
#### Fixes
10
10
11
11
* Your contribution here.
12
+ * [ #2067 ] ( https://github.com/ruby-grape/grape/pull/2067 ) : Coerce empty string to nil for all primitive types except String
12
13
* [ #2064 ] ( https://github.com/ruby-grape/grape/pull/2064 ) : Fix Ruby 2.7 deprecation warning in ` Grape::Middleware::Base#initialize ` - [ @skarger ] ( https://github.com/skarger ) .
13
14
14
15
### 1.3.3 (2020/05/23)
Original file line number Diff line number Diff line change @@ -37,7 +37,6 @@ def initialize(type, strict = false)
37
37
def call ( val )
38
38
return InvalidValue . new if reject? ( val )
39
39
return nil if val . nil? || treat_as_nil? ( val )
40
- return '' if val == ''
41
40
42
41
super
43
42
end
@@ -60,7 +59,7 @@ def reject?(val)
60
59
# absence of a value and coerces it into nil. See a discussion there
61
60
# https://github.com/ruby-grape/grape/pull/2045
62
61
def treat_as_nil? ( val )
63
- val == '' && type == Grape :: API :: Boolean
62
+ val == '' && type != String
64
63
end
65
64
end
66
65
end
Original file line number Diff line number Diff line change 14
14
it 'coerces to BigDecimal' do
15
15
expect ( subject . call ( 5 ) ) . to eq ( BigDecimal ( 5 ) )
16
16
end
17
+
18
+ it 'coerces an empty string to nil' do
19
+ expect ( subject . call ( '' ) ) . to be_nil
20
+ end
17
21
end
18
22
19
23
context 'Boolean' do
40
44
end
41
45
end
42
46
47
+ context 'DateTime' do
48
+ let ( :type ) { DateTime }
49
+
50
+ it 'coerces an empty string to nil' do
51
+ expect ( subject . call ( '' ) ) . to be_nil
52
+ end
53
+ end
54
+
55
+ context 'Float' do
56
+ let ( :type ) { Float }
57
+
58
+ it 'coerces an empty string to nil' do
59
+ expect ( subject . call ( '' ) ) . to be_nil
60
+ end
61
+ end
62
+
63
+ context 'Integer' do
64
+ let ( :type ) { Integer }
65
+
66
+ it 'coerces an empty string to nil' do
67
+ expect ( subject . call ( '' ) ) . to be_nil
68
+ end
69
+ end
70
+
71
+ context 'Numeric' do
72
+ let ( :type ) { Numeric }
73
+
74
+ it 'coerces an empty string to nil' do
75
+ expect ( subject . call ( '' ) ) . to be_nil
76
+ end
77
+ end
78
+
79
+ context 'Time' do
80
+ let ( :type ) { Time }
81
+
82
+ it 'coerces an empty string to nil' do
83
+ expect ( subject . call ( '' ) ) . to be_nil
84
+ end
85
+ end
86
+
43
87
context 'String' do
44
88
let ( :type ) { String }
45
89
46
90
it 'coerces to String' do
47
91
expect ( subject . call ( 10 ) ) . to eq ( '10' )
48
92
end
93
+
94
+ it 'does not coerce an empty string to nil' do
95
+ expect ( subject . call ( '' ) ) . to eq ( '' )
96
+ end
97
+ end
98
+
99
+ context 'Symbol' do
100
+ let ( :type ) { Symbol }
101
+
102
+ it 'coerces an empty string to nil' do
103
+ expect ( subject . call ( '' ) ) . to be_nil
104
+ end
49
105
end
50
106
51
107
context 'the strict mode' do
You can’t perform that action at this time.
0 commit comments