input_sanitizer 0.2.0 → 0.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 +4 -4
- data/.gitignore +0 -1
- data/.travis.yml +2 -7
- data/Gemfile.lock +44 -0
- data/README.md +9 -3
- data/input_sanitizer.gemspec +1 -0
- data/lib/input_sanitizer/extended_converters.rb +13 -0
- data/lib/input_sanitizer/sanitizer.rb +18 -9
- data/lib/input_sanitizer/version.rb +1 -1
- data/spec/default_converters_spec.rb +12 -8
- data/spec/extended_converters_spec.rb +16 -0
- data/spec/sanitizer_spec.rb +14 -3
- data/spec/spec_helper.rb +11 -0
- metadata +27 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 22a6e4537da366c88f1578e47d886e94319e9104
|
|
4
|
+
data.tar.gz: d039d03743961d3712a8e651f83af62087837231
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5696a63090cd6e7428669dae8f0536c9ef6d04cf2d0fe040c1f36319172be9d11e7b6e385d57dad12efb1357e1c4992e81d2a27f39bd84fd646ea25265589518
|
|
7
|
+
data.tar.gz: c9a42b2d1ad1b9702d59305a8df143f2a4c3054a6999d24a89c00471b29685cc5cc86d4195c16dd2cb8fa7f54224b6b3c2d5e202ae0d5b1d0e2ed94bd7dea445
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
input_sanitizer (0.2.2)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
coderay (1.1.0)
|
|
10
|
+
diff-lcs (1.2.5)
|
|
11
|
+
docile (1.1.5)
|
|
12
|
+
method_source (0.8.2)
|
|
13
|
+
multi_json (1.10.1)
|
|
14
|
+
pry (0.10.0)
|
|
15
|
+
coderay (~> 1.1.0)
|
|
16
|
+
method_source (~> 0.8.1)
|
|
17
|
+
slop (~> 3.4)
|
|
18
|
+
rspec (3.0.0)
|
|
19
|
+
rspec-core (~> 3.0.0)
|
|
20
|
+
rspec-expectations (~> 3.0.0)
|
|
21
|
+
rspec-mocks (~> 3.0.0)
|
|
22
|
+
rspec-core (3.0.3)
|
|
23
|
+
rspec-support (~> 3.0.0)
|
|
24
|
+
rspec-expectations (3.0.3)
|
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
26
|
+
rspec-support (~> 3.0.0)
|
|
27
|
+
rspec-mocks (3.0.3)
|
|
28
|
+
rspec-support (~> 3.0.0)
|
|
29
|
+
rspec-support (3.0.3)
|
|
30
|
+
simplecov (0.9.0)
|
|
31
|
+
docile (~> 1.1.0)
|
|
32
|
+
multi_json
|
|
33
|
+
simplecov-html (~> 0.8.0)
|
|
34
|
+
simplecov-html (0.8.0)
|
|
35
|
+
slop (3.6.0)
|
|
36
|
+
|
|
37
|
+
PLATFORMS
|
|
38
|
+
ruby
|
|
39
|
+
|
|
40
|
+
DEPENDENCIES
|
|
41
|
+
input_sanitizer!
|
|
42
|
+
pry
|
|
43
|
+
rspec
|
|
44
|
+
simplecov
|
data/README.md
CHANGED
|
@@ -19,12 +19,10 @@ Or install it yourself as:
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
21
|
```ruby
|
|
22
|
-
|
|
23
22
|
class PersonSanitizer < InputSanitizer::Sanitizer
|
|
24
23
|
string :name
|
|
25
24
|
string :address
|
|
26
25
|
integer :height
|
|
27
|
-
float :weight
|
|
28
26
|
date :birthday
|
|
29
27
|
end
|
|
30
28
|
|
|
@@ -57,7 +55,15 @@ PrivilegedSanitizer.clean({:birthday => '1986-10-06'})
|
|
|
57
55
|
data = PrivilegedSanitizer.clean({:account_id => 3})
|
|
58
56
|
data[:account] # instead of :account_id
|
|
59
57
|
# => InputSanitizer::KeyNotAllowedError: Key not allowed: account
|
|
60
|
-
|
|
58
|
+
|
|
59
|
+
# supports custom value converters
|
|
60
|
+
class SomethingSanitizer < InputSanitizer::Sanitizer
|
|
61
|
+
custom :backward, :converter => lambda { |v| v.reverse }
|
|
62
|
+
integer :version
|
|
63
|
+
custom :name, :provide => :version, :converter => lambda { |name, version|
|
|
64
|
+
version < 3 ? name.downcase : name
|
|
65
|
+
}
|
|
66
|
+
end
|
|
61
67
|
```
|
|
62
68
|
|
|
63
69
|
|
data/input_sanitizer.gemspec
CHANGED
|
@@ -20,6 +20,19 @@ module InputSanitizer
|
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
class CommaJoinedStringsConverter
|
|
24
|
+
def call(value)
|
|
25
|
+
non_valid = value.gsub(/[a-zA-Z,]/, "")
|
|
26
|
+
if non_valid.empty?
|
|
27
|
+
parts = value.split(",").map(&:to_s)
|
|
28
|
+
else
|
|
29
|
+
invalid_chars = non_valid.split(//)
|
|
30
|
+
invalid_chars_desc = invalid_chars.join(", ")
|
|
31
|
+
raise InputSanitizer::ConversionError.new("Invalid chars: #{invalid_chars_desc}")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
23
36
|
class SpecificValuesConverter
|
|
24
37
|
def initialize(values)
|
|
25
38
|
@valid_values = values
|
|
@@ -25,7 +25,8 @@ class InputSanitizer::Sanitizer
|
|
|
25
25
|
collection = hash[:options][:collection]
|
|
26
26
|
namespace = hash[:options][:namespace]
|
|
27
27
|
default = hash[:options][:default]
|
|
28
|
-
|
|
28
|
+
provide = hash[:options][:provide]
|
|
29
|
+
clean_field(field, type, required, collection, namespace, default, provide)
|
|
29
30
|
end
|
|
30
31
|
@performed = true
|
|
31
32
|
@cleaned.freeze
|
|
@@ -105,10 +106,10 @@ class InputSanitizer::Sanitizer
|
|
|
105
106
|
array.last.is_a?(Hash) ? array.last : {}
|
|
106
107
|
end
|
|
107
108
|
|
|
108
|
-
def clean_field(field, type, required, collection, namespace, default)
|
|
109
|
+
def clean_field(field, type, required, collection, namespace, default, provide)
|
|
109
110
|
if @data.has_key?(field)
|
|
110
111
|
begin
|
|
111
|
-
@cleaned[field] = convert(field, type, collection, namespace)
|
|
112
|
+
@cleaned[field] = convert(field, type, collection, namespace, provide)
|
|
112
113
|
rescue InputSanitizer::ConversionError => ex
|
|
113
114
|
add_error(field, :invalid_value, @data[field], ex.message)
|
|
114
115
|
end
|
|
@@ -132,21 +133,29 @@ class InputSanitizer::Sanitizer
|
|
|
132
133
|
add_error(field, :missing, nil, nil)
|
|
133
134
|
end
|
|
134
135
|
|
|
135
|
-
def convert(field, type, collection, namespace)
|
|
136
|
+
def convert(field, type, collection, namespace, provide)
|
|
136
137
|
if collection
|
|
137
138
|
@data[field].map { |v|
|
|
138
|
-
convert_single(type, v, namespace)
|
|
139
|
+
convert_single(type, v, namespace, provide)
|
|
139
140
|
}
|
|
140
141
|
else
|
|
141
|
-
convert_single(type, @data[field], namespace)
|
|
142
|
+
convert_single(type, @data[field], namespace, provide)
|
|
142
143
|
end
|
|
143
144
|
end
|
|
144
145
|
|
|
145
|
-
def convert_single(type, value, namespace)
|
|
146
|
+
def convert_single(type, value, namespace, provide)
|
|
146
147
|
if namespace
|
|
147
|
-
{ namespace => converter(type)
|
|
148
|
+
{ namespace => convert_value(converter(type), value[namespace], provide) }
|
|
148
149
|
else
|
|
149
|
-
converter(type)
|
|
150
|
+
convert_value(converter(type), value, provide)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def convert_value(converter, value, provide)
|
|
155
|
+
if provide
|
|
156
|
+
converter.call(value, @data[provide])
|
|
157
|
+
else
|
|
158
|
+
converter.call(value)
|
|
150
159
|
end
|
|
151
160
|
end
|
|
152
161
|
|
|
@@ -32,35 +32,35 @@ describe InputSanitizer::BooleanConverter do
|
|
|
32
32
|
let(:converter) { InputSanitizer::BooleanConverter.new }
|
|
33
33
|
|
|
34
34
|
it "casts 'true' to true" do
|
|
35
|
-
converter.call('true').should
|
|
35
|
+
converter.call('true').should eq(true)
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it "casts true to true" do
|
|
39
|
-
converter.call(true).should
|
|
39
|
+
converter.call(true).should eq(true)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it "casts '1' to true" do
|
|
43
|
-
converter.call('1').should
|
|
43
|
+
converter.call('1').should eq(true)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it "casts 'yes' to true" do
|
|
47
|
-
converter.call('yes').should
|
|
47
|
+
converter.call('yes').should eq(true)
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
it "casts 'false' to false" do
|
|
51
|
-
converter.call('false').should
|
|
51
|
+
converter.call('false').should eq(false)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
54
|
it "casts false to false" do
|
|
55
|
-
converter.call(false).should
|
|
55
|
+
converter.call(false).should eq(false)
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
it "casts '0' to false" do
|
|
59
|
-
converter.call('0').should
|
|
59
|
+
converter.call('0').should eq(false)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it "casts 'no' to false" do
|
|
63
|
-
converter.call('no').should
|
|
63
|
+
converter.call('no').should eq(false)
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
it "raises error if cannot cast" do
|
|
@@ -116,4 +116,8 @@ describe InputSanitizer::TimeConverter do
|
|
|
116
116
|
t = Time.now
|
|
117
117
|
converter.call(t).should == t.utc
|
|
118
118
|
end
|
|
119
|
+
|
|
120
|
+
it "raises error if value is of invalid type" do
|
|
121
|
+
lambda { converter.call({}) }.should raise_error(InputSanitizer::ConversionError)
|
|
122
|
+
end
|
|
119
123
|
end
|
|
@@ -23,6 +23,10 @@ end
|
|
|
23
23
|
describe InputSanitizer::PositiveIntegerConverter do
|
|
24
24
|
let(:converter) { InputSanitizer::PositiveIntegerConverter.new }
|
|
25
25
|
|
|
26
|
+
it "casts string to integer" do
|
|
27
|
+
converter.call("3").should == 3
|
|
28
|
+
end
|
|
29
|
+
|
|
26
30
|
it "raises error if integer less than zero" do
|
|
27
31
|
lambda { converter.call("-3") }.should raise_error(InputSanitizer::ConversionError)
|
|
28
32
|
end
|
|
@@ -44,6 +48,18 @@ describe InputSanitizer::CommaJoinedIntegersConverter do
|
|
|
44
48
|
end
|
|
45
49
|
end
|
|
46
50
|
|
|
51
|
+
describe InputSanitizer::CommaJoinedStringsConverter do
|
|
52
|
+
let(:converter) { described_class.new }
|
|
53
|
+
|
|
54
|
+
it "parses to array of ids" do
|
|
55
|
+
converter.call("input,Sanitizer,ROCKS").should == ["input", "Sanitizer", "ROCKS"]
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it "raises on invalid character" do
|
|
59
|
+
lambda { converter.call(":") }.should raise_error(InputSanitizer::ConversionError)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
47
63
|
describe InputSanitizer::SpecificValuesConverter do
|
|
48
64
|
let(:converter) { InputSanitizer::SpecificValuesConverter.new([:a, :b]) }
|
|
49
65
|
|
data/spec/sanitizer_spec.rb
CHANGED
|
@@ -14,6 +14,9 @@ class BasicSanitizer < InputSanitizer::Sanitizer
|
|
|
14
14
|
time :updated_at
|
|
15
15
|
custom :cust1, :cust2, :converter => lambda { |v| v.reverse }
|
|
16
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
|
+
}
|
|
17
20
|
end
|
|
18
21
|
|
|
19
22
|
class BrokenCustomSanitizer < InputSanitizer::Sanitizer
|
|
@@ -132,7 +135,7 @@ describe InputSanitizer::Sanitizer do
|
|
|
132
135
|
|
|
133
136
|
cleaned.should have_key(:num)
|
|
134
137
|
cleaned[:num].should == 23
|
|
135
|
-
cleaned[:is_nice].should
|
|
138
|
+
cleaned[:is_nice].should eq(false)
|
|
136
139
|
end
|
|
137
140
|
|
|
138
141
|
it "overrides inherited fields" do
|
|
@@ -183,9 +186,17 @@ describe InputSanitizer::Sanitizer do
|
|
|
183
186
|
end
|
|
184
187
|
|
|
185
188
|
it "raises an error when converter is not defined" do
|
|
186
|
-
|
|
189
|
+
lambda do
|
|
187
190
|
BrokenCustomSanitizer.custom(:x)
|
|
188
|
-
end.
|
|
191
|
+
end.should raise_error
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it "provides the converter with requested value" do
|
|
195
|
+
@params = { :custom3 => 'three', :num => 1 }
|
|
196
|
+
cleaned.should have_key(:custom3)
|
|
197
|
+
cleaned.should have_key(:num)
|
|
198
|
+
cleaned[:custom3].should eq('eerht')
|
|
199
|
+
cleaned[:num].should eq(1)
|
|
189
200
|
end
|
|
190
201
|
end
|
|
191
202
|
|
data/spec/spec_helper.rb
CHANGED
|
@@ -6,4 +6,15 @@ unless ENV['CI']
|
|
|
6
6
|
SimpleCov.start
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
+
RSpec.configure do |config|
|
|
10
|
+
config.expect_with :rspec do |c|
|
|
11
|
+
c.syntax = :should
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
config.mock_with :rspec do |c|
|
|
15
|
+
c.syntax = :should
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
9
19
|
require 'input_sanitizer'
|
|
20
|
+
require 'pry'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: input_sanitizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tomek Paczkowski
|
|
@@ -10,34 +10,48 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2014-07-31 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: rspec
|
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
|
18
18
|
requirements:
|
|
19
|
-
- -
|
|
19
|
+
- - ">="
|
|
20
20
|
- !ruby/object:Gem::Version
|
|
21
21
|
version: '0'
|
|
22
22
|
type: :development
|
|
23
23
|
prerelease: false
|
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
25
25
|
requirements:
|
|
26
|
-
- -
|
|
26
|
+
- - ">="
|
|
27
27
|
- !ruby/object:Gem::Version
|
|
28
28
|
version: '0'
|
|
29
29
|
- !ruby/object:Gem::Dependency
|
|
30
30
|
name: simplecov
|
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
|
32
32
|
requirements:
|
|
33
|
-
- -
|
|
33
|
+
- - ">="
|
|
34
34
|
- !ruby/object:Gem::Version
|
|
35
35
|
version: '0'
|
|
36
36
|
type: :development
|
|
37
37
|
prerelease: false
|
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
|
39
39
|
requirements:
|
|
40
|
-
- -
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
- !ruby/object:Gem::Dependency
|
|
44
|
+
name: pry
|
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
type: :development
|
|
51
|
+
prerelease: false
|
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
41
55
|
- !ruby/object:Gem::Version
|
|
42
56
|
version: '0'
|
|
43
57
|
description: Gem to sanitize hash of incoming data
|
|
@@ -49,10 +63,11 @@ executables: []
|
|
|
49
63
|
extensions: []
|
|
50
64
|
extra_rdoc_files: []
|
|
51
65
|
files:
|
|
52
|
-
- .gitignore
|
|
53
|
-
- .rspec
|
|
54
|
-
- .travis.yml
|
|
66
|
+
- ".gitignore"
|
|
67
|
+
- ".rspec"
|
|
68
|
+
- ".travis.yml"
|
|
55
69
|
- Gemfile
|
|
70
|
+
- Gemfile.lock
|
|
56
71
|
- LICENSE
|
|
57
72
|
- README.md
|
|
58
73
|
- Rakefile
|
|
@@ -77,17 +92,17 @@ require_paths:
|
|
|
77
92
|
- lib
|
|
78
93
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
94
|
requirements:
|
|
80
|
-
- -
|
|
95
|
+
- - ">="
|
|
81
96
|
- !ruby/object:Gem::Version
|
|
82
97
|
version: '0'
|
|
83
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
99
|
requirements:
|
|
85
|
-
- -
|
|
100
|
+
- - ">="
|
|
86
101
|
- !ruby/object:Gem::Version
|
|
87
102
|
version: '0'
|
|
88
103
|
requirements: []
|
|
89
104
|
rubyforge_project:
|
|
90
|
-
rubygems_version: 2.
|
|
105
|
+
rubygems_version: 2.2.2
|
|
91
106
|
signing_key:
|
|
92
107
|
specification_version: 4
|
|
93
108
|
summary: Gem to sanitize hash of incoming data
|
|
@@ -97,4 +112,3 @@ test_files:
|
|
|
97
112
|
- spec/restricted_hash_spec.rb
|
|
98
113
|
- spec/sanitizer_spec.rb
|
|
99
114
|
- spec/spec_helper.rb
|
|
100
|
-
has_rdoc:
|