input_sanitizer 0.1.10 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yaml +26 -0
  3. data/.github/workflows/gempush.yml +28 -0
  4. data/.gitignore +2 -1
  5. data/CHANGELOG +99 -0
  6. data/LICENSE +201 -22
  7. data/README.md +24 -4
  8. data/input_sanitizer.gemspec +10 -4
  9. data/lib/input_sanitizer.rb +5 -2
  10. data/lib/input_sanitizer/errors.rb +142 -0
  11. data/lib/input_sanitizer/extended_converters.rb +5 -42
  12. data/lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb +15 -0
  13. data/lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb +15 -0
  14. data/lib/input_sanitizer/extended_converters/positive_integer_converter.rb +12 -0
  15. data/lib/input_sanitizer/extended_converters/specific_values_converter.rb +19 -0
  16. data/lib/input_sanitizer/restricted_hash.rb +49 -8
  17. data/lib/input_sanitizer/v1.rb +22 -0
  18. data/lib/input_sanitizer/v1/clean_field.rb +38 -0
  19. data/lib/input_sanitizer/{default_converters.rb → v1/default_converters.rb} +20 -13
  20. data/lib/input_sanitizer/v1/sanitizer.rb +166 -0
  21. data/lib/input_sanitizer/v2.rb +13 -0
  22. data/lib/input_sanitizer/v2/clean_field.rb +36 -0
  23. data/lib/input_sanitizer/v2/clean_payload_collection_field.rb +41 -0
  24. data/lib/input_sanitizer/v2/clean_query_collection_field.rb +40 -0
  25. data/lib/input_sanitizer/v2/error_collection.rb +49 -0
  26. data/lib/input_sanitizer/v2/nested_sanitizer_factory.rb +19 -0
  27. data/lib/input_sanitizer/v2/payload_sanitizer.rb +130 -0
  28. data/lib/input_sanitizer/v2/payload_transform.rb +42 -0
  29. data/lib/input_sanitizer/v2/query_sanitizer.rb +33 -0
  30. data/lib/input_sanitizer/v2/types.rb +227 -0
  31. data/lib/input_sanitizer/version.rb +1 -1
  32. data/spec/extended_converters/comma_joined_integers_converter_spec.rb +18 -0
  33. data/spec/extended_converters/comma_joined_strings_converter_spec.rb +18 -0
  34. data/spec/extended_converters/positive_integer_converter_spec.rb +18 -0
  35. data/spec/extended_converters/specific_values_converter_spec.rb +27 -0
  36. data/spec/restricted_hash_spec.rb +37 -7
  37. data/spec/sanitizer_spec.rb +129 -26
  38. data/spec/spec_helper.rb +17 -2
  39. data/spec/v1/default_converters_spec.rb +141 -0
  40. data/spec/v2/converters_spec.rb +174 -0
  41. data/spec/v2/payload_sanitizer_spec.rb +534 -0
  42. data/spec/v2/payload_transform_spec.rb +98 -0
  43. data/spec/v2/query_sanitizer_spec.rb +300 -0
  44. data/v2.md +52 -0
  45. metadata +105 -40
  46. data/.travis.yml +0 -12
  47. data/lib/input_sanitizer/sanitizer.rb +0 -140
  48. data/spec/default_converters_spec.rb +0 -101
  49. data/spec/extended_converters_spec.rb +0 -62
@@ -1,11 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class NestedSanitizer < InputSanitizer::Sanitizer
4
+ integer :foo
5
+ end
6
+
3
7
  class BasicSanitizer < InputSanitizer::Sanitizer
4
8
  string :x, :y, :z
9
+ integer :namespaced, :namespace => :value
10
+ integer :array, :collection => true
11
+ integer :namespaced_array, :collection => true, :namespace => :value
5
12
  integer :num
6
13
  date :birthday
7
14
  time :updated_at
8
15
  custom :cust1, :cust2, :converter => lambda { |v| v.reverse }
16
+ nested :stuff, :sanitizer => NestedSanitizer, :collection => true, :namespace => :nested
17
+ custom :custom3, :provide => :num, :converter => lambda { |v, num|
18
+ num == 1 ? v.reverse : v
19
+ }
9
20
  end
10
21
 
11
22
  class BrokenCustomSanitizer < InputSanitizer::Sanitizer
@@ -28,12 +39,17 @@ class RequiredCustom < BasicSanitizer
28
39
  custom :c1, :required => true, :converter => lambda { |v| v }
29
40
  end
30
41
 
42
+ class DefaultParameters < BasicSanitizer
43
+ integer :funky_number, :default => 5
44
+ custom :fixed_stuff, :converter => lambda {|v| v }, :default => "default string"
45
+ end
46
+
31
47
  describe InputSanitizer::Sanitizer do
32
48
  let(:sanitizer) { BasicSanitizer.new(@params) }
33
49
 
34
50
  describe ".clean" do
35
51
  it "returns cleaned data" do
36
- clean_data = mock()
52
+ clean_data = double
37
53
  BasicSanitizer.any_instance.should_receive(:cleaned).and_return(clean_data)
38
54
  BasicSanitizer.clean({}).should be(clean_data)
39
55
  end
@@ -57,6 +73,12 @@ describe InputSanitizer::Sanitizer do
57
73
  cleaned.should_not have_key(:d)
58
74
  end
59
75
 
76
+ it "does not include ommited fields" do
77
+ @params = { "x" => 1, "z" => 3 }
78
+
79
+ cleaned.should_not have_key(:y)
80
+ end
81
+
60
82
  it "freezes cleaned hash" do
61
83
  @params = {}
62
84
 
@@ -83,6 +105,30 @@ describe InputSanitizer::Sanitizer do
83
105
  cleaned.should_not have_key(:d)
84
106
  end
85
107
 
108
+ it "preserves namespace" do
109
+ value = { :value => 5 }
110
+ @params = { :namespaced => value }
111
+
112
+ cleaned[:namespaced].should eq(value)
113
+ end
114
+
115
+ it "maps values for collection fields" do
116
+ numbers = [3, 5, 6]
117
+ @params = { :array => numbers }
118
+
119
+ cleaned[:array].should eq(numbers)
120
+ end
121
+
122
+ it "maps values for collection fields with namespace" do
123
+ numbers = [
124
+ { :value => 2 },
125
+ { :value => 5 }
126
+ ]
127
+ @params = { :namespaced_array => numbers }
128
+
129
+ cleaned[:namespaced_array].should eq(numbers)
130
+ end
131
+
86
132
  it "silently discards cast errors" do
87
133
  @params = {:num => "f"}
88
134
 
@@ -95,7 +141,7 @@ describe InputSanitizer::Sanitizer do
95
141
 
96
142
  cleaned.should have_key(:num)
97
143
  cleaned[:num].should == 23
98
- cleaned[:is_nice].should be_false
144
+ cleaned[:is_nice].should eq(false)
99
145
  end
100
146
 
101
147
  it "overrides inherited fields" do
@@ -106,6 +152,32 @@ describe InputSanitizer::Sanitizer do
106
152
  cleaned[:is_nice].should == 42
107
153
  end
108
154
 
155
+ context "when sanitizer is initialized with default values" do
156
+ context "when paremeters are not overwriten" do
157
+ let(:sanitizer) { DefaultParameters.new({}) }
158
+
159
+ it "returns default value for non custom key" do
160
+ sanitizer.cleaned[:funky_number].should == 5
161
+ end
162
+
163
+ it "returns default value for custom key" do
164
+ sanitizer.cleaned[:fixed_stuff].should == "default string"
165
+ end
166
+ end
167
+
168
+ context "when parameters are overwriten" do
169
+ let(:sanitizer) { DefaultParameters.new({ :funky_number => 2, :fixed_stuff => "fixed" }) }
170
+
171
+ it "returns default value for non custom key" do
172
+ sanitizer.cleaned[:funky_number].should == 2
173
+ end
174
+
175
+ it "returns default value for custom key" do
176
+ sanitizer.cleaned[:fixed_stuff].should == "fixed"
177
+ end
178
+ end
179
+ end
180
+
109
181
  end
110
182
 
111
183
  describe ".custom" do
@@ -120,9 +192,53 @@ describe InputSanitizer::Sanitizer do
120
192
  end
121
193
 
122
194
  it "raises an error when converter is not defined" do
123
- expect do
195
+ lambda do
124
196
  BrokenCustomSanitizer.custom(:x)
125
- end.to raise_error
197
+ end.should raise_error
198
+ end
199
+
200
+ it "provides the converter with requested value" do
201
+ @params = { :custom3 => 'three', :num => 1 }
202
+ cleaned.should have_key(:custom3)
203
+ cleaned.should have_key(:num)
204
+ cleaned[:custom3].should eq('eerht')
205
+ cleaned[:num].should eq(1)
206
+ end
207
+ end
208
+
209
+ describe ".nested" do
210
+ let(:sanitizer) { BasicSanitizer.new(@params) }
211
+ let(:cleaned) { sanitizer.cleaned }
212
+
213
+ it "sanitizes nested values" do
214
+ nested = [
215
+ { :nested => { :foo => "5" } },
216
+ { :nested => { :foo => 8 } },
217
+ ]
218
+ @params = { :stuff => nested }
219
+
220
+ expected = [
221
+ { :nested => { :foo => 5 } },
222
+ { :nested => { :foo => 8 } },
223
+ ]
224
+ cleaned[:stuff].should eq(expected)
225
+ end
226
+
227
+ it "propagates errors" do
228
+ nested = [ { :nested => { :foo => "INVALID" } } ]
229
+ @params = { :stuff => nested }
230
+
231
+ sanitizer.should_not be_valid
232
+ end
233
+
234
+ it "returns an error when given a nil for a nested value" do
235
+ @params = { :stuff => nil }
236
+ sanitizer.should_not be_valid
237
+ end
238
+
239
+ it "returns an error when given a string for a nested value" do
240
+ @params = { :stuff => 'nope' }
241
+ sanitizer.should_not be_valid
126
242
  end
127
243
  end
128
244
 
@@ -131,43 +247,25 @@ describe InputSanitizer::Sanitizer do
131
247
 
132
248
  it "includes :integer type" do
133
249
  sanitizer.converters.should have_key(:integer)
134
- sanitizer.converters[:integer].should be_a(InputSanitizer::IntegerConverter)
250
+ sanitizer.converters[:integer].should be_a(InputSanitizer::V1::IntegerConverter)
135
251
  end
136
252
 
137
253
  it "includes :string type" do
138
254
  sanitizer.converters.should have_key(:string)
139
- sanitizer.converters[:string].should be_a(InputSanitizer::StringConverter)
255
+ sanitizer.converters[:string].should be_a(InputSanitizer::V1::StringConverter)
140
256
  end
141
257
 
142
258
  it "includes :date type" do
143
259
  sanitizer.converters.should have_key(:date)
144
- sanitizer.converters[:date].should be_a(InputSanitizer::DateConverter)
260
+ sanitizer.converters[:date].should be_a(InputSanitizer::V1::DateConverter)
145
261
  end
146
262
 
147
263
  it "includes :boolean type" do
148
264
  sanitizer.converters.should have_key(:boolean)
149
- sanitizer.converters[:boolean].should be_a(InputSanitizer::BooleanConverter)
265
+ sanitizer.converters[:boolean].should be_a(InputSanitizer::V1::BooleanConverter)
150
266
  end
151
267
  end
152
268
 
153
- describe '.extract_options' do
154
-
155
- it "extracts hash from array if is last" do
156
- options = { :a => 1}
157
- array = [1,2, options]
158
- BasicSanitizer.extract_options(array).should == options
159
- array.should == [1,2, options]
160
- end
161
-
162
- it "does not extract the last element if not a hash and returns default empty hash" do
163
- array = [1,2]
164
- BasicSanitizer.extract_options(array).should_not == 2
165
- BasicSanitizer.extract_options(array).should == {}
166
- array.should == [1,2]
167
- end
168
-
169
- end
170
-
171
269
  describe '.extract_options!' do
172
270
 
173
271
  it "extracts hash from array if is last" do
@@ -234,4 +332,9 @@ describe InputSanitizer::Sanitizer do
234
332
  error[:field].should == :c1
235
333
  end
236
334
  end
335
+
336
+ it "is valid when given extra params" do
337
+ @params = { :extra => 'test' }
338
+ sanitizer.should be_valid
339
+ end
237
340
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,22 @@
1
1
  require 'bundler'
2
2
  Bundler.setup(:test)
3
3
 
4
- require 'simplecov'
5
- SimpleCov.start unless ENV['CI'] == 'true'
4
+ skip_coverage = ENV['CI']
5
+
6
+ unless skip_coverage
7
+ require 'simplecov'
8
+ SimpleCov.start
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :should
14
+ end
15
+
16
+ config.mock_with :rspec do |c|
17
+ c.syntax = :should
18
+ end
19
+ end
6
20
 
7
21
  require 'input_sanitizer'
22
+ require 'pry'
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ describe InputSanitizer::V1::IntegerConverter do
4
+ let(:converter) { InputSanitizer::V1::IntegerConverter.new }
5
+
6
+ it "casts string to integer" do
7
+ converter.call("42").should == 42
8
+ end
9
+
10
+ it "casts integer to integer" do
11
+ converter.call(42).should == 42
12
+ end
13
+
14
+ it "raises error if cannot cast" do
15
+ lambda { converter.call("f") }.should raise_error(InputSanitizer::ConversionError)
16
+ end
17
+ end
18
+
19
+ describe InputSanitizer::V1::DateConverter do
20
+ let(:converter) { InputSanitizer::V1::DateConverter.new }
21
+
22
+ it "casts dates in iso format" do
23
+ converter.call("2012-05-15").should == Date.new(2012, 5, 15)
24
+ end
25
+
26
+ it "raises error if cannot cast" do
27
+ lambda { converter.call("2012-02-30") }.should raise_error(InputSanitizer::ConversionError)
28
+ end
29
+ end
30
+
31
+ describe InputSanitizer::V1::BooleanConverter do
32
+ let(:converter) { InputSanitizer::V1::BooleanConverter.new }
33
+
34
+ it "casts 'true' to true" do
35
+ converter.call('true').should eq(true)
36
+ end
37
+
38
+ it "casts true to true" do
39
+ converter.call(true).should eq(true)
40
+ end
41
+
42
+ it "casts '1' to true" do
43
+ converter.call('1').should eq(true)
44
+ end
45
+
46
+ it "casts 'yes' to true" do
47
+ converter.call('yes').should eq(true)
48
+ end
49
+
50
+ it "casts 'false' to false" do
51
+ converter.call('false').should eq(false)
52
+ end
53
+
54
+ it "casts false to false" do
55
+ converter.call(false).should eq(false)
56
+ end
57
+
58
+ it "casts '0' to false" do
59
+ converter.call('0').should eq(false)
60
+ end
61
+
62
+ it "casts 'no' to false" do
63
+ converter.call('no').should eq(false)
64
+ end
65
+
66
+ it "raises error if cannot cast" do
67
+ lambda { converter.call("notboolean") }.should raise_error(InputSanitizer::ConversionError)
68
+ end
69
+ end
70
+
71
+ describe InputSanitizer::V1::TimeConverter do
72
+ let(:converter) { InputSanitizer::V1::TimeConverter.new }
73
+
74
+ it "raises if timezone part given" do
75
+ lambda { converter.call("2012-05-15 13:42:54 +01:00") }.should raise_error(InputSanitizer::ConversionError)
76
+ end
77
+
78
+ it "casts date time in iso format" do
79
+ t = Time.utc(2012, 5, 15, 13, 42, 54)
80
+ converter.call("2012-05-15 13:42:54").should == t
81
+ converter.call("2012-05-15T13:42:54").should == t
82
+ converter.call("20120515134254").should == t
83
+ end
84
+
85
+ it "works with miliseconds" do
86
+ t = Time.utc(2012, 5, 15, 13, 42, 54)
87
+ converter.call("2012-05-15 13:42:54.000").should == t
88
+ converter.call("2012-05-15T13:42:54.000").should == t
89
+ converter.call("20120515134254000").should == t
90
+ end
91
+
92
+ it "works with Z at the end" do
93
+ t = Time.utc(2012, 5, 15, 13, 42, 54)
94
+ converter.call("2012-05-15 13:42:54.000Z").should == t
95
+ converter.call("2012-05-15T13:42:54.000Z").should == t
96
+ converter.call("2012-05-15T13:42:54Z").should == t
97
+ converter.call("20120515134254000Z").should == t
98
+ end
99
+
100
+ it "does not require time part" do
101
+ converter.call("2012-05-15 13:42").should == Time.utc(2012, 5, 15, 13, 42)
102
+ converter.call("2012-05-15 13").should == Time.utc(2012, 5, 15, 13)
103
+ converter.call("2012-05-15").should == Time.utc(2012, 5, 15)
104
+ end
105
+
106
+ it "raises error if can format is wrong" do
107
+ lambda { converter.call("2/10/2031 13:44:22") }.should raise_error(InputSanitizer::ConversionError)
108
+ end
109
+
110
+ it "raises error if date is wrong" do
111
+ lambda { converter.call("2012-02-32") }.should raise_error(InputSanitizer::ConversionError)
112
+ end
113
+
114
+ it "allows the instance of Time" do
115
+ t = Time.now
116
+ converter.call(t).should == t.utc
117
+ end
118
+
119
+ it "raises error if value is of invalid type" do
120
+ lambda { converter.call({}) }.should raise_error(InputSanitizer::ConversionError)
121
+ end
122
+ end
123
+
124
+ describe InputSanitizer::V1::AllowNil do
125
+ it "passes blanks" do
126
+ lambda { |_| 1 }.extend(InputSanitizer::V1::AllowNil).call("").should be_nil
127
+ end
128
+
129
+ it "passes things the extended sanitizer passes" do
130
+ lambda { |_| :something }.extend(InputSanitizer::V1::AllowNil).call(:stuff).
131
+ should eq(:something)
132
+ end
133
+
134
+ it "raises error if the extended sanitizer raises error" do
135
+ action = lambda do
136
+ lambda { |_| raise "Some error" }.extend(InputSanitizer::V1::AllowNil).call(:stuff)
137
+ end
138
+
139
+ action.should raise_error
140
+ end
141
+ end
@@ -0,0 +1,174 @@
1
+ require 'spec_helper'
2
+
3
+ # OPTIONS
4
+ # :allow_nil (default true)
5
+ # :allow_blank (default true)
6
+ # :require (default false, requires given key in params, and additionally sets :allow_nil and :allow_blank to false)
7
+
8
+ # | allow_nil | allow_blank
9
+ # __________|___________|_____________
10
+ # string | YES | YES
11
+ # url | YES | YES
12
+ # datetime | YES | YES
13
+ # integer | YES | NO
14
+ # boolean | NO | NO
15
+ # ------------------------------------
16
+
17
+ # [type, value, option] => valid? # if valid? == false then check for ValueMissing error
18
+
19
+ test_cases = {
20
+ [InputSanitizer::V2::Types::StringCheck, { :allow_nil => false }, nil] => false,
21
+ [InputSanitizer::V2::Types::StringCheck, { :allow_nil => false }, ""] => true,
22
+ [InputSanitizer::V2::Types::StringCheck, { :allow_nil => false }, "zz"] => true,
23
+ [InputSanitizer::V2::Types::StringCheck, { :allow_blank => false }, nil] => false,
24
+ [InputSanitizer::V2::Types::StringCheck, { :allow_blank => false }, ""] => false,
25
+ [InputSanitizer::V2::Types::StringCheck, { :allow_blank => false }, "zz"] => true,
26
+ [InputSanitizer::V2::Types::StringCheck, { :required => true }, nil] => false,
27
+ [InputSanitizer::V2::Types::StringCheck, { :required => true }, ""] => false,
28
+ [InputSanitizer::V2::Types::StringCheck, { :required => true }, "zz"] => true,
29
+ [InputSanitizer::V2::Types::StringCheck, { }, nil] => true,
30
+ [InputSanitizer::V2::Types::StringCheck, { }, ""] => true,
31
+ [InputSanitizer::V2::Types::StringCheck, { }, "zz"] => true,
32
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_nil => false }, nil] => false,
33
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_nil => false }, ""] => :invalid_type,
34
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_nil => false }, 1] => true,
35
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_blank => false }, nil] => false,
36
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_blank => false }, ""] => :invalid_type,
37
+ [InputSanitizer::V2::Types::IntegerCheck, { :allow_blank => false }, 1] => true,
38
+ [InputSanitizer::V2::Types::IntegerCheck, { :required => true }, nil] => false,
39
+ [InputSanitizer::V2::Types::IntegerCheck, { :required => true }, ""] => :invalid_type,
40
+ [InputSanitizer::V2::Types::IntegerCheck, { :required => true }, 1] => true,
41
+ [InputSanitizer::V2::Types::IntegerCheck, { }, nil] => true,
42
+ [InputSanitizer::V2::Types::IntegerCheck, { }, ""] => :invalid_type,
43
+ [InputSanitizer::V2::Types::IntegerCheck, { }, 1] => true,
44
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_nil => false }, nil] => false,
45
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_nil => false }, "null"] => false,
46
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_nil => false }, ""] => :invalid_type,
47
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_nil => false }, 1] => true,
48
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_blank => false }, nil] => false,
49
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_blank => false }, "null"] => false,
50
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_blank => false }, ""] => :invalid_type,
51
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :allow_blank => false }, 1] => true,
52
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :required => true }, nil] => false,
53
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :required => true }, "null"] => false,
54
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :required => true }, ""] => :invalid_type,
55
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { :required => true }, 1] => true,
56
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { }, nil] => true,
57
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { }, "null"] => true,
58
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { }, ""] => :invalid_type,
59
+ [InputSanitizer::V2::Types::CoercingIntegerCheck, { }, 1] => true,
60
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_nil => false }, nil] => false,
61
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_nil => false }, ""] => :invalid_type,
62
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_nil => false }, 1] => true,
63
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_nil => false }, 3.1415] => true,
64
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_blank => false }, nil] => false,
65
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_blank => false }, ""] => :invalid_type,
66
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_blank => false }, 1] => true,
67
+ [InputSanitizer::V2::Types::FloatCheck, { :allow_blank => false }, 3.1415] => true,
68
+ [InputSanitizer::V2::Types::FloatCheck, { :required => true }, nil] => false,
69
+ [InputSanitizer::V2::Types::FloatCheck, { :required => true }, ""] => :invalid_type,
70
+ [InputSanitizer::V2::Types::FloatCheck, { :required => true }, 1] => true,
71
+ [InputSanitizer::V2::Types::FloatCheck, { :required => true }, 3.1415] => true,
72
+ [InputSanitizer::V2::Types::FloatCheck, { }, nil] => true,
73
+ [InputSanitizer::V2::Types::FloatCheck, { }, ""] => :invalid_type,
74
+ [InputSanitizer::V2::Types::FloatCheck, { }, 1] => true,
75
+ [InputSanitizer::V2::Types::FloatCheck, { }, 3.1415] => true,
76
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, nil] => false,
77
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, "null"] => false,
78
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, ""] => :invalid_type,
79
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, 1] => true,
80
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, 3.1415] => true,
81
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_nil => false }, "3.1415"] => true,
82
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, nil] => false,
83
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, "null"] => false,
84
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, ""] => :invalid_type,
85
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, 1] => true,
86
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, 3.1415] => true,
87
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :allow_blank => false }, "3.1415"] => true,
88
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, nil] => false,
89
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, "null"] => false,
90
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, ""] => :invalid_type,
91
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, 1] => true,
92
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, 3.1415] => true,
93
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { :required => true }, "3.1415"] => true,
94
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, nil] => true,
95
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, "null"] => true,
96
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, ""] => :invalid_type,
97
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, 1] => true,
98
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, 3.1415] => true,
99
+ [InputSanitizer::V2::Types::CoercingFloatCheck, { }, "3.1415"] => true,
100
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_nil => false }, nil] => false,
101
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_nil => false }, ""] => :invalid_type,
102
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_nil => false }, true] => true,
103
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_blank => false }, nil] => false,
104
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_blank => false }, ""] => :invalid_type,
105
+ [InputSanitizer::V2::Types::BooleanCheck, { :allow_blank => false }, true] => true,
106
+ [InputSanitizer::V2::Types::BooleanCheck, { :required => true }, nil] => false,
107
+ [InputSanitizer::V2::Types::BooleanCheck, { :required => true }, ""] => :invalid_type,
108
+ [InputSanitizer::V2::Types::BooleanCheck, { :required => true }, true] => true,
109
+ [InputSanitizer::V2::Types::BooleanCheck, { }, nil] => false,
110
+ [InputSanitizer::V2::Types::BooleanCheck, { }, ""] => :invalid_type,
111
+ [InputSanitizer::V2::Types::BooleanCheck, { }, true] => true,
112
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_nil => false }, nil] => false,
113
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_nil => false }, ""] => true,
114
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_nil => false }, "2014-08-27T16:32:56Z"] => true,
115
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_blank => false }, nil] => false,
116
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_blank => false }, ""] => false,
117
+ [InputSanitizer::V2::Types::DatetimeCheck, { :allow_blank => false }, "2014-08-27T16:32:56Z"] => true,
118
+ [InputSanitizer::V2::Types::DatetimeCheck, { :required => true }, nil] => false,
119
+ [InputSanitizer::V2::Types::DatetimeCheck, { :required => true }, ""] => false,
120
+ [InputSanitizer::V2::Types::DatetimeCheck, { :required => true }, "2014-08-27T16:32:56Z"] => true,
121
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, nil] => true,
122
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, ""] => true,
123
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, "2014-08-27T16:32:56Z"] => true,
124
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, 1234] => :invalid_type,
125
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, []] => :invalid_type,
126
+ [InputSanitizer::V2::Types::DatetimeCheck, { }, {}] => :invalid_type,
127
+ [InputSanitizer::V2::Types::URLCheck, { :allow_nil => false }, nil] => false,
128
+ [InputSanitizer::V2::Types::URLCheck, { :allow_nil => false }, ""] => true,
129
+ [InputSanitizer::V2::Types::URLCheck, { :allow_nil => false }, "http://getbase.com"] => true,
130
+ [InputSanitizer::V2::Types::URLCheck, { :allow_blank => false }, nil] => false,
131
+ [InputSanitizer::V2::Types::URLCheck, { :allow_blank => false }, ""] => false,
132
+ [InputSanitizer::V2::Types::URLCheck, { :allow_blank => false }, "http://getbase.com"] => true,
133
+ [InputSanitizer::V2::Types::URLCheck, { :required => true }, nil] => false,
134
+ [InputSanitizer::V2::Types::URLCheck, { :required => true }, ""] => false,
135
+ [InputSanitizer::V2::Types::URLCheck, { :required => true }, "http://getbase.com"] => true,
136
+ [InputSanitizer::V2::Types::URLCheck, { }, nil] => true,
137
+ [InputSanitizer::V2::Types::URLCheck, { }, ""] => true,
138
+ [InputSanitizer::V2::Types::URLCheck, { }, "http://getbase.com"] => true,
139
+ }
140
+
141
+ describe 'type checks' do
142
+ test_cases.each do |(test_case, result)|
143
+ describe test_case[0] do
144
+ context "with options: #{test_case[1]}" do
145
+ show_value = case test_case[2]
146
+ when nil then 'nil'
147
+ when '' then "''"
148
+ else "'#{test_case[2]}'"
149
+ end
150
+
151
+ it "correctly handles value #{show_value}" do
152
+ case result
153
+ when true
154
+ lambda { test_case[0].new.call(test_case[2], test_case[1]) }.should_not raise_error
155
+ when false
156
+ lambda { test_case[0].new.call(test_case[2], test_case[1]) }.should raise_error(InputSanitizer::BlankValueError)
157
+ when :invalid_type
158
+ lambda { test_case[0].new.call(test_case[2], test_case[1]) }.should raise_error(InputSanitizer::TypeMismatchError)
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ # Generating test cases:
167
+
168
+ # types = ['string', 'integer', 'boolean', 'datetime', 'url']
169
+ # options = [:allow_nil_false, :allow_blank_false, :required_true, :no_params]
170
+ # values = [nil, '', lambda { |type| { 'string' => 'zz', 'integer' => 1, 'boolean' => true, 'datetime' => '2014-08-27T16:32:56Z', 'url' => 'http://getbase.com' }[type] }]
171
+ # test_cases = types.product(options).product(values).map(&:flatten).map! do |test_case|
172
+ # test_case[2] = test_case[2].call(test_case[0]) if test_case[2].is_a?(Proc); test_case
173
+ # end
174
+ # test_cases.each { |test_case| puts "#{test_case.inspect} => []," }; nil