input_sanitizer 0.2.2 → 0.3.33
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 +5 -5
- data/.gitignore +2 -0
- data/.travis.yml +2 -0
- data/CHANGELOG +92 -0
- data/LICENSE +201 -22
- data/README.md +7 -0
- data/input_sanitizer.gemspec +15 -5
- data/lib/input_sanitizer/errors.rb +142 -0
- data/lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb +15 -0
- data/lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb +15 -0
- data/lib/input_sanitizer/extended_converters/positive_integer_converter.rb +12 -0
- data/lib/input_sanitizer/extended_converters/specific_values_converter.rb +19 -0
- data/lib/input_sanitizer/extended_converters.rb +5 -55
- data/lib/input_sanitizer/restricted_hash.rb +49 -8
- data/lib/input_sanitizer/v1/clean_field.rb +38 -0
- data/lib/input_sanitizer/{default_converters.rb → v1/default_converters.rb} +8 -11
- data/lib/input_sanitizer/v1/sanitizer.rb +163 -0
- data/lib/input_sanitizer/v1.rb +22 -0
- data/lib/input_sanitizer/v2/clean_field.rb +36 -0
- data/lib/input_sanitizer/v2/clean_payload_collection_field.rb +41 -0
- data/lib/input_sanitizer/v2/clean_query_collection_field.rb +40 -0
- data/lib/input_sanitizer/v2/error_collection.rb +49 -0
- data/lib/input_sanitizer/v2/nested_sanitizer_factory.rb +19 -0
- data/lib/input_sanitizer/v2/payload_sanitizer.rb +130 -0
- data/lib/input_sanitizer/v2/payload_transform.rb +42 -0
- data/lib/input_sanitizer/v2/query_sanitizer.rb +33 -0
- data/lib/input_sanitizer/v2/types.rb +213 -0
- data/lib/input_sanitizer/v2.rb +13 -0
- data/lib/input_sanitizer/version.rb +1 -1
- data/lib/input_sanitizer.rb +5 -2
- data/spec/extended_converters/comma_joined_integers_converter_spec.rb +18 -0
- data/spec/extended_converters/comma_joined_strings_converter_spec.rb +18 -0
- data/spec/extended_converters/positive_integer_converter_spec.rb +18 -0
- data/spec/extended_converters/specific_values_converter_spec.rb +27 -0
- data/spec/restricted_hash_spec.rb +37 -7
- data/spec/sanitizer_spec.rb +32 -22
- data/spec/spec_helper.rb +3 -1
- data/spec/{default_converters_spec.rb → v1/default_converters_spec.rb} +27 -9
- data/spec/v2/converters_spec.rb +174 -0
- data/spec/v2/payload_sanitizer_spec.rb +460 -0
- data/spec/v2/payload_transform_spec.rb +98 -0
- data/spec/v2/query_sanitizer_spec.rb +300 -0
- data/v2.md +52 -0
- metadata +86 -30
- data/Gemfile.lock +0 -44
- data/lib/input_sanitizer/sanitizer.rb +0 -179
- data/spec/extended_converters_spec.rb +0 -78
@@ -0,0 +1,13 @@
|
|
1
|
+
module InputSanitizer::V2
|
2
|
+
end
|
3
|
+
|
4
|
+
dir = File.dirname(__FILE__)
|
5
|
+
require File.join(dir, 'v2/types')
|
6
|
+
require File.join(dir, 'v2/clean_payload_collection_field')
|
7
|
+
require File.join(dir, 'v2/clean_query_collection_field')
|
8
|
+
require File.join(dir, 'v2/clean_field')
|
9
|
+
require File.join(dir, 'v2/nested_sanitizer_factory')
|
10
|
+
require File.join(dir, 'v2/error_collection')
|
11
|
+
require File.join(dir, 'v2/payload_sanitizer')
|
12
|
+
require File.join(dir, 'v2/query_sanitizer')
|
13
|
+
require File.join(dir, 'v2/payload_transform')
|
data/lib/input_sanitizer.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'input_sanitizer/extended_converters/comma_joined_integers_converter'
|
3
|
+
|
4
|
+
describe InputSanitizer::CommaJoinedIntegersConverter do
|
5
|
+
let(:converter) { described_class.new }
|
6
|
+
|
7
|
+
it "parses to array of ids" do
|
8
|
+
converter.call("1,2,3,5").should eq([1, 2, 3, 5])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "converts to array if given an integer" do
|
12
|
+
converter.call(7).should eq([7])
|
13
|
+
end
|
14
|
+
|
15
|
+
it "raises on invalid character" do
|
16
|
+
lambda { converter.call(":") }.should raise_error(InputSanitizer::ConversionError)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'input_sanitizer/extended_converters/comma_joined_strings_converter'
|
3
|
+
|
4
|
+
describe InputSanitizer::CommaJoinedStringsConverter do
|
5
|
+
let(:converter) { described_class.new }
|
6
|
+
|
7
|
+
it "parses to array of ids" do
|
8
|
+
converter.call("input,Sanitizer,ROCKS").should eq(["input", "Sanitizer", "ROCKS"])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "allows underscores" do
|
12
|
+
converter.call("input_sanitizer,rocks").should eq(["input_sanitizer", "rocks"])
|
13
|
+
end
|
14
|
+
|
15
|
+
it "raises on invalid character" do
|
16
|
+
lambda { converter.call(":") }.should raise_error(InputSanitizer::ConversionError)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'input_sanitizer/extended_converters/positive_integer_converter'
|
3
|
+
|
4
|
+
describe InputSanitizer::PositiveIntegerConverter do
|
5
|
+
let(:converter) { described_class.new }
|
6
|
+
|
7
|
+
it "casts string to integer" do
|
8
|
+
converter.call("3").should == 3
|
9
|
+
end
|
10
|
+
|
11
|
+
it "raises error if integer less than zero" do
|
12
|
+
lambda { converter.call("-3") }.should raise_error(InputSanitizer::ConversionError)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "raises error if integer equals zero" do
|
16
|
+
lambda { converter.call("0") }.should raise_error(InputSanitizer::ConversionError)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'input_sanitizer/extended_converters/specific_values_converter'
|
3
|
+
|
4
|
+
describe InputSanitizer::SpecificValuesConverter do
|
5
|
+
let(:converter) { described_class.new(values) }
|
6
|
+
let(:values) { [:a, :b] }
|
7
|
+
|
8
|
+
it "converts valid value to symbol" do
|
9
|
+
converter.call("b").should eq(:b)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises on invalid value" do
|
13
|
+
lambda { converter.call("c") }.should raise_error(InputSanitizer::ConversionError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises on nil value" do
|
17
|
+
lambda { converter.call(nil) }.should raise_error(InputSanitizer::ConversionError)
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when specific values are strings" do
|
21
|
+
let(:values) { ["a", "b"] }
|
22
|
+
|
23
|
+
it "keeps given value as string" do
|
24
|
+
converter.call("a").should eq("a")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,19 +1,49 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe InputSanitizer::RestrictedHash do
|
4
|
-
let(:hash) { InputSanitizer::RestrictedHash.new([:a
|
5
|
-
subject { hash }
|
4
|
+
let(:hash) { InputSanitizer::RestrictedHash.new([:a]) }
|
6
5
|
|
7
|
-
it
|
8
|
-
lambda{hash[:
|
6
|
+
it 'does not allow bad keys' do
|
7
|
+
lambda{ hash[:b] }.should raise_error(InputSanitizer::KeyNotAllowedError)
|
9
8
|
end
|
10
9
|
|
11
|
-
it
|
10
|
+
it 'does allow correct keys' do
|
12
11
|
hash[:a].should be_nil
|
13
12
|
end
|
14
13
|
|
15
|
-
it
|
14
|
+
it 'returns value for correct key' do
|
16
15
|
hash[:a] = 'stuff'
|
17
16
|
hash[:a].should == 'stuff'
|
18
17
|
end
|
18
|
+
|
19
|
+
it 'allows to set value for a new key' do
|
20
|
+
hash[:b] = 'other stuff'
|
21
|
+
hash[:b].should == 'other stuff'
|
22
|
+
hash.key_allowed?(:b).should be_truthy
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'adds new allowed keys after `transform_keys` is done' do
|
26
|
+
hash[:a] = 'stuff'
|
27
|
+
hash.transform_keys! { |key| key.to_s }
|
28
|
+
hash['a'].should == 'stuff'
|
29
|
+
hash.key_allowed?('a').should be_truthy
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'removes previous allowed keys after `transform_keys` is done' do
|
33
|
+
hash[:a] = 'stuff'
|
34
|
+
hash.transform_keys! { |key| key.to_s }
|
35
|
+
lambda{ hash[:a] }.should raise_error(InputSanitizer::KeyNotAllowedError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'updates allowed keys on merge!' do
|
39
|
+
merge_hash = { merged: '1' }
|
40
|
+
hash.merge!(merge_hash)
|
41
|
+
hash.key_allowed?(:merged).should be_truthy
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'updates allowed keys on merge' do
|
45
|
+
merge_hash = { merged: '1' }
|
46
|
+
new_hash = hash.merge(some_key: merge_hash)
|
47
|
+
new_hash.key_allowed?(:some_key).should be_truthy
|
48
|
+
end
|
19
49
|
end
|
data/spec/sanitizer_spec.rb
CHANGED
@@ -73,6 +73,12 @@ describe InputSanitizer::Sanitizer do
|
|
73
73
|
cleaned.should_not have_key(:d)
|
74
74
|
end
|
75
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
|
+
|
76
82
|
it "freezes cleaned hash" do
|
77
83
|
@params = {}
|
78
84
|
|
@@ -217,6 +223,23 @@ describe InputSanitizer::Sanitizer do
|
|
217
223
|
]
|
218
224
|
cleaned[:stuff].should eq(expected)
|
219
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
|
242
|
+
end
|
220
243
|
end
|
221
244
|
|
222
245
|
describe ".converters" do
|
@@ -224,43 +247,25 @@ describe InputSanitizer::Sanitizer do
|
|
224
247
|
|
225
248
|
it "includes :integer type" do
|
226
249
|
sanitizer.converters.should have_key(:integer)
|
227
|
-
sanitizer.converters[:integer].should be_a(InputSanitizer::IntegerConverter)
|
250
|
+
sanitizer.converters[:integer].should be_a(InputSanitizer::V1::IntegerConverter)
|
228
251
|
end
|
229
252
|
|
230
253
|
it "includes :string type" do
|
231
254
|
sanitizer.converters.should have_key(:string)
|
232
|
-
sanitizer.converters[:string].should be_a(InputSanitizer::StringConverter)
|
255
|
+
sanitizer.converters[:string].should be_a(InputSanitizer::V1::StringConverter)
|
233
256
|
end
|
234
257
|
|
235
258
|
it "includes :date type" do
|
236
259
|
sanitizer.converters.should have_key(:date)
|
237
|
-
sanitizer.converters[:date].should be_a(InputSanitizer::DateConverter)
|
260
|
+
sanitizer.converters[:date].should be_a(InputSanitizer::V1::DateConverter)
|
238
261
|
end
|
239
262
|
|
240
263
|
it "includes :boolean type" do
|
241
264
|
sanitizer.converters.should have_key(:boolean)
|
242
|
-
sanitizer.converters[:boolean].should be_a(InputSanitizer::BooleanConverter)
|
265
|
+
sanitizer.converters[:boolean].should be_a(InputSanitizer::V1::BooleanConverter)
|
243
266
|
end
|
244
267
|
end
|
245
268
|
|
246
|
-
describe '.extract_options' do
|
247
|
-
|
248
|
-
it "extracts hash from array if is last" do
|
249
|
-
options = { :a => 1}
|
250
|
-
array = [1,2, options]
|
251
|
-
BasicSanitizer.extract_options(array).should == options
|
252
|
-
array.should == [1,2, options]
|
253
|
-
end
|
254
|
-
|
255
|
-
it "does not extract the last element if not a hash and returns default empty hash" do
|
256
|
-
array = [1,2]
|
257
|
-
BasicSanitizer.extract_options(array).should_not == 2
|
258
|
-
BasicSanitizer.extract_options(array).should == {}
|
259
|
-
array.should == [1,2]
|
260
|
-
end
|
261
|
-
|
262
|
-
end
|
263
|
-
|
264
269
|
describe '.extract_options!' do
|
265
270
|
|
266
271
|
it "extracts hash from array if is last" do
|
@@ -327,4 +332,9 @@ describe InputSanitizer::Sanitizer do
|
|
327
332
|
error[:field].should == :c1
|
328
333
|
end
|
329
334
|
end
|
335
|
+
|
336
|
+
it "is valid when given extra params" do
|
337
|
+
@params = { :extra => 'test' }
|
338
|
+
sanitizer.should be_valid
|
339
|
+
end
|
330
340
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe InputSanitizer::IntegerConverter do
|
4
|
-
let(:converter) { InputSanitizer::IntegerConverter.new }
|
3
|
+
describe InputSanitizer::V1::IntegerConverter do
|
4
|
+
let(:converter) { InputSanitizer::V1::IntegerConverter.new }
|
5
5
|
|
6
6
|
it "casts string to integer" do
|
7
7
|
converter.call("42").should == 42
|
@@ -16,8 +16,8 @@ describe InputSanitizer::IntegerConverter do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe InputSanitizer::DateConverter do
|
20
|
-
let(:converter) { InputSanitizer::DateConverter.new }
|
19
|
+
describe InputSanitizer::V1::DateConverter do
|
20
|
+
let(:converter) { InputSanitizer::V1::DateConverter.new }
|
21
21
|
|
22
22
|
it "casts dates in iso format" do
|
23
23
|
converter.call("2012-05-15").should == Date.new(2012, 5, 15)
|
@@ -28,8 +28,8 @@ describe InputSanitizer::DateConverter do
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
describe InputSanitizer::BooleanConverter do
|
32
|
-
let(:converter) { InputSanitizer::BooleanConverter.new }
|
31
|
+
describe InputSanitizer::V1::BooleanConverter do
|
32
|
+
let(:converter) { InputSanitizer::V1::BooleanConverter.new }
|
33
33
|
|
34
34
|
it "casts 'true' to true" do
|
35
35
|
converter.call('true').should eq(true)
|
@@ -68,9 +68,8 @@ describe InputSanitizer::BooleanConverter do
|
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
let(:converter) { InputSanitizer::TimeConverter.new }
|
71
|
+
describe InputSanitizer::V1::TimeConverter do
|
72
|
+
let(:converter) { InputSanitizer::V1::TimeConverter.new }
|
74
73
|
|
75
74
|
it "raises if timezone part given" do
|
76
75
|
lambda { converter.call("2012-05-15 13:42:54 +01:00") }.should raise_error(InputSanitizer::ConversionError)
|
@@ -121,3 +120,22 @@ describe InputSanitizer::TimeConverter do
|
|
121
120
|
lambda { converter.call({}) }.should raise_error(InputSanitizer::ConversionError)
|
122
121
|
end
|
123
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
|