input_sanitizer 0.2.2 → 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.travis.yml +2 -0
  4. data/CHANGELOG +92 -0
  5. data/LICENSE +201 -22
  6. data/README.md +7 -0
  7. data/input_sanitizer.gemspec +15 -5
  8. data/lib/input_sanitizer/errors.rb +142 -0
  9. data/lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb +15 -0
  10. data/lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb +15 -0
  11. data/lib/input_sanitizer/extended_converters/positive_integer_converter.rb +12 -0
  12. data/lib/input_sanitizer/extended_converters/specific_values_converter.rb +19 -0
  13. data/lib/input_sanitizer/extended_converters.rb +5 -55
  14. data/lib/input_sanitizer/restricted_hash.rb +49 -8
  15. data/lib/input_sanitizer/v1/clean_field.rb +38 -0
  16. data/lib/input_sanitizer/{default_converters.rb → v1/default_converters.rb} +8 -11
  17. data/lib/input_sanitizer/v1/sanitizer.rb +163 -0
  18. data/lib/input_sanitizer/v1.rb +22 -0
  19. data/lib/input_sanitizer/v2/clean_field.rb +36 -0
  20. data/lib/input_sanitizer/v2/clean_payload_collection_field.rb +41 -0
  21. data/lib/input_sanitizer/v2/clean_query_collection_field.rb +40 -0
  22. data/lib/input_sanitizer/v2/error_collection.rb +49 -0
  23. data/lib/input_sanitizer/v2/nested_sanitizer_factory.rb +19 -0
  24. data/lib/input_sanitizer/v2/payload_sanitizer.rb +130 -0
  25. data/lib/input_sanitizer/v2/payload_transform.rb +42 -0
  26. data/lib/input_sanitizer/v2/query_sanitizer.rb +33 -0
  27. data/lib/input_sanitizer/v2/types.rb +213 -0
  28. data/lib/input_sanitizer/v2.rb +13 -0
  29. data/lib/input_sanitizer/version.rb +1 -1
  30. data/lib/input_sanitizer.rb +5 -2
  31. data/spec/extended_converters/comma_joined_integers_converter_spec.rb +18 -0
  32. data/spec/extended_converters/comma_joined_strings_converter_spec.rb +18 -0
  33. data/spec/extended_converters/positive_integer_converter_spec.rb +18 -0
  34. data/spec/extended_converters/specific_values_converter_spec.rb +27 -0
  35. data/spec/restricted_hash_spec.rb +37 -7
  36. data/spec/sanitizer_spec.rb +32 -22
  37. data/spec/spec_helper.rb +3 -1
  38. data/spec/{default_converters_spec.rb → v1/default_converters_spec.rb} +27 -9
  39. data/spec/v2/converters_spec.rb +174 -0
  40. data/spec/v2/payload_sanitizer_spec.rb +460 -0
  41. data/spec/v2/payload_transform_spec.rb +98 -0
  42. data/spec/v2/query_sanitizer_spec.rb +300 -0
  43. data/v2.md +52 -0
  44. metadata +86 -30
  45. data/Gemfile.lock +0 -44
  46. data/lib/input_sanitizer/sanitizer.rb +0 -179
  47. data/spec/extended_converters_spec.rb +0 -78
metadata CHANGED
@@ -1,64 +1,88 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: input_sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.33
5
5
  platform: ruby
6
6
  authors:
7
- - Tomek Paczkowski
8
- - Tomasz Werbicki
9
- - Michal Bugno
7
+ - Zendesk
10
8
  autorequire:
11
9
  bindir: bin
12
10
  cert_chain: []
13
- date: 2014-07-31 00:00:00.000000000 Z
11
+ date: 2020-04-24 00:00:00.000000000 Z
14
12
  dependencies:
15
13
  - !ruby/object:Gem::Dependency
16
- name: rspec
14
+ name: method_struct
17
15
  requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
17
  - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
19
+ version: 0.2.2
20
+ type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
23
  requirements:
26
24
  - - ">="
27
25
  - !ruby/object:Gem::Version
28
- version: '0'
26
+ version: 0.2.2
29
27
  - !ruby/object:Gem::Dependency
30
- name: simplecov
28
+ name: activesupport
31
29
  requirement: !ruby/object:Gem::Requirement
32
30
  requirements:
33
31
  - - ">="
34
32
  - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
33
+ version: 3.0.0
34
+ type: :runtime
37
35
  prerelease: false
38
36
  version_requirements: !ruby/object:Gem::Requirement
39
37
  requirements:
40
38
  - - ">="
41
39
  - !ruby/object:Gem::Version
42
- version: '0'
40
+ version: 3.0.0
43
41
  - !ruby/object:Gem::Dependency
44
42
  name: pry
45
43
  requirement: !ruby/object:Gem::Requirement
46
44
  requirements:
47
- - - ">="
45
+ - - "~>"
48
46
  - !ruby/object:Gem::Version
49
- version: '0'
47
+ version: 0.10.1
50
48
  type: :development
51
49
  prerelease: false
52
50
  version_requirements: !ruby/object:Gem::Requirement
53
51
  requirements:
54
- - - ">="
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
55
81
  - !ruby/object:Gem::Version
56
- version: '0'
82
+ version: 3.2.0
57
83
  description: Gem to sanitize hash of incoming data
58
84
  email:
59
- - tom@futuresimple.com
60
- - tomasz@futuresimple.com
61
- - michal@futuresimple.com
85
+ - opensource@zendesk.com
62
86
  executables: []
63
87
  extensions: []
64
88
  extra_rdoc_files: []
@@ -66,25 +90,51 @@ files:
66
90
  - ".gitignore"
67
91
  - ".rspec"
68
92
  - ".travis.yml"
93
+ - CHANGELOG
69
94
  - Gemfile
70
- - Gemfile.lock
71
95
  - LICENSE
72
96
  - README.md
73
97
  - Rakefile
74
98
  - input_sanitizer.gemspec
75
99
  - lib/input_sanitizer.rb
76
- - lib/input_sanitizer/default_converters.rb
100
+ - lib/input_sanitizer/errors.rb
77
101
  - lib/input_sanitizer/extended_converters.rb
102
+ - lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb
103
+ - lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb
104
+ - lib/input_sanitizer/extended_converters/positive_integer_converter.rb
105
+ - lib/input_sanitizer/extended_converters/specific_values_converter.rb
78
106
  - lib/input_sanitizer/restricted_hash.rb
79
- - lib/input_sanitizer/sanitizer.rb
107
+ - lib/input_sanitizer/v1.rb
108
+ - lib/input_sanitizer/v1/clean_field.rb
109
+ - lib/input_sanitizer/v1/default_converters.rb
110
+ - lib/input_sanitizer/v1/sanitizer.rb
111
+ - lib/input_sanitizer/v2.rb
112
+ - lib/input_sanitizer/v2/clean_field.rb
113
+ - lib/input_sanitizer/v2/clean_payload_collection_field.rb
114
+ - lib/input_sanitizer/v2/clean_query_collection_field.rb
115
+ - lib/input_sanitizer/v2/error_collection.rb
116
+ - lib/input_sanitizer/v2/nested_sanitizer_factory.rb
117
+ - lib/input_sanitizer/v2/payload_sanitizer.rb
118
+ - lib/input_sanitizer/v2/payload_transform.rb
119
+ - lib/input_sanitizer/v2/query_sanitizer.rb
120
+ - lib/input_sanitizer/v2/types.rb
80
121
  - lib/input_sanitizer/version.rb
81
- - spec/default_converters_spec.rb
82
- - spec/extended_converters_spec.rb
122
+ - spec/extended_converters/comma_joined_integers_converter_spec.rb
123
+ - spec/extended_converters/comma_joined_strings_converter_spec.rb
124
+ - spec/extended_converters/positive_integer_converter_spec.rb
125
+ - spec/extended_converters/specific_values_converter_spec.rb
83
126
  - spec/restricted_hash_spec.rb
84
127
  - spec/sanitizer_spec.rb
85
128
  - spec/spec_helper.rb
129
+ - spec/v1/default_converters_spec.rb
130
+ - spec/v2/converters_spec.rb
131
+ - spec/v2/payload_sanitizer_spec.rb
132
+ - spec/v2/payload_transform_spec.rb
133
+ - spec/v2/query_sanitizer_spec.rb
134
+ - v2.md
86
135
  homepage: ''
87
- licenses: []
136
+ licenses:
137
+ - Apache-2.0
88
138
  metadata: {}
89
139
  post_install_message:
90
140
  rdoc_options: []
@@ -101,14 +151,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
151
  - !ruby/object:Gem::Version
102
152
  version: '0'
103
153
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.2.2
154
+ rubygems_version: 3.1.2
106
155
  signing_key:
107
156
  specification_version: 4
108
157
  summary: Gem to sanitize hash of incoming data
109
158
  test_files:
110
- - spec/default_converters_spec.rb
111
- - spec/extended_converters_spec.rb
159
+ - spec/extended_converters/comma_joined_integers_converter_spec.rb
160
+ - spec/extended_converters/comma_joined_strings_converter_spec.rb
161
+ - spec/extended_converters/positive_integer_converter_spec.rb
162
+ - spec/extended_converters/specific_values_converter_spec.rb
112
163
  - spec/restricted_hash_spec.rb
113
164
  - spec/sanitizer_spec.rb
114
165
  - spec/spec_helper.rb
166
+ - spec/v1/default_converters_spec.rb
167
+ - spec/v2/converters_spec.rb
168
+ - spec/v2/payload_sanitizer_spec.rb
169
+ - spec/v2/payload_transform_spec.rb
170
+ - spec/v2/query_sanitizer_spec.rb
data/Gemfile.lock DELETED
@@ -1,44 +0,0 @@
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
@@ -1,179 +0,0 @@
1
- require 'input_sanitizer/restricted_hash'
2
- require 'input_sanitizer/default_converters'
3
-
4
- class InputSanitizer::Sanitizer
5
- def initialize(data)
6
- @data = symbolize_keys(data)
7
- @performed = false
8
- @errors = []
9
- @cleaned = InputSanitizer::RestrictedHash.new(self.class.fields.keys)
10
- end
11
-
12
- def self.clean(data)
13
- new(data).cleaned
14
- end
15
-
16
- def [](field)
17
- cleaned[field]
18
- end
19
-
20
- def cleaned
21
- return @cleaned if @performed
22
- self.class.fields.each do |field, hash|
23
- type = hash[:type]
24
- required = hash[:options][:required]
25
- collection = hash[:options][:collection]
26
- namespace = hash[:options][:namespace]
27
- default = hash[:options][:default]
28
- provide = hash[:options][:provide]
29
- clean_field(field, type, required, collection, namespace, default, provide)
30
- end
31
- @performed = true
32
- @cleaned.freeze
33
- end
34
-
35
- def valid?
36
- cleaned
37
- @errors.empty?
38
- end
39
-
40
- def errors
41
- cleaned
42
- @errors
43
- end
44
-
45
- def self.converters
46
- {
47
- :integer => InputSanitizer::IntegerConverter.new,
48
- :string => InputSanitizer::StringConverter.new,
49
- :date => InputSanitizer::DateConverter.new,
50
- :time => InputSanitizer::TimeConverter.new,
51
- :boolean => InputSanitizer::BooleanConverter.new,
52
- :integer_or_blank => InputSanitizer::IntegerConverter.new.extend(InputSanitizer::AllowNil),
53
- :string_or_blank => InputSanitizer::StringConverter.new.extend(InputSanitizer::AllowNil),
54
- :date_or_blank => InputSanitizer::DateConverter.new.extend(InputSanitizer::AllowNil),
55
- :time_or_blank => InputSanitizer::TimeConverter.new.extend(InputSanitizer::AllowNil),
56
- :boolean_or_blank => InputSanitizer::BooleanConverter.new.extend(InputSanitizer::AllowNil),
57
- }
58
- end
59
-
60
- def self.inherited(subclass)
61
- subclass.fields = self.fields.dup
62
- end
63
-
64
- converters.keys.each do |name|
65
- class_eval <<-END
66
- def self.#{name}(*keys)
67
- set_keys_to_type(keys, :#{name})
68
- end
69
- END
70
- end
71
-
72
- def self.custom(*keys)
73
- options = keys.pop
74
- converter = options.delete(:converter)
75
- keys.push(options)
76
- raise "You did not define a converter for a custom type" if converter == nil
77
- self.set_keys_to_type(keys, converter)
78
- end
79
-
80
- def self.nested(*keys)
81
- options = keys.pop
82
- sanitizer = options.delete(:sanitizer)
83
- keys.push(options)
84
- raise "You did not define a sanitizer for nested value" if sanitizer == nil
85
- converter = lambda { |value|
86
- sanitizer.clean(value)
87
- }
88
- self.set_keys_to_type(keys, converter)
89
- end
90
-
91
- protected
92
- def self.fields
93
- @fields ||= {}
94
- end
95
-
96
- def self.fields=(new_fields)
97
- @fields = new_fields
98
- end
99
-
100
- private
101
- def self.extract_options!(array)
102
- array.last.is_a?(Hash) ? array.pop : {}
103
- end
104
-
105
- def self.extract_options(array)
106
- array.last.is_a?(Hash) ? array.last : {}
107
- end
108
-
109
- def clean_field(field, type, required, collection, namespace, default, provide)
110
- if @data.has_key?(field)
111
- begin
112
- @cleaned[field] = convert(field, type, collection, namespace, provide)
113
- rescue InputSanitizer::ConversionError => ex
114
- add_error(field, :invalid_value, @data[field], ex.message)
115
- end
116
- elsif default
117
- @cleaned[field] = converter(type).call(default)
118
- elsif required
119
- add_missing(field)
120
- end
121
- end
122
-
123
- def add_error(field, error_type, value, description = nil)
124
- @errors << {
125
- :field => field,
126
- :type => error_type,
127
- :value => value,
128
- :description => description
129
- }
130
- end
131
-
132
- def add_missing(field)
133
- add_error(field, :missing, nil, nil)
134
- end
135
-
136
- def convert(field, type, collection, namespace, provide)
137
- if collection
138
- @data[field].map { |v|
139
- convert_single(type, v, namespace, provide)
140
- }
141
- else
142
- convert_single(type, @data[field], namespace, provide)
143
- end
144
- end
145
-
146
- def convert_single(type, value, namespace, provide)
147
- if namespace
148
- { namespace => convert_value(converter(type), value[namespace], provide) }
149
- else
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)
159
- end
160
- end
161
-
162
- def converter(type)
163
- type.respond_to?(:call) ? type : self.class.converters[type]
164
- end
165
-
166
- def symbolize_keys(data)
167
- data.inject({}) do |memo, kv|
168
- memo[kv.first.to_sym] = kv.last
169
- memo
170
- end
171
- end
172
-
173
- def self.set_keys_to_type(keys, type)
174
- opts = extract_options!(keys)
175
- keys.each do |key|
176
- fields[key] = { :type => type, :options => opts }
177
- end
178
- end
179
- end
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
- require 'input_sanitizer/extended_converters'
3
-
4
- describe InputSanitizer::AllowNil do
5
- it "passes blanks" do
6
- lambda { |_| 1 }.extend(InputSanitizer::AllowNil).call("").should be_nil
7
- end
8
-
9
- it "passes things the extended sanitizer passes" do
10
- lambda { |_| :something }.extend(InputSanitizer::AllowNil).call(:stuff).
11
- should eq(:something)
12
- end
13
-
14
- it "raises error if the extended sanitizer raises error" do
15
- action = lambda do
16
- lambda { |_| raise "Some error" }.extend(InputSanitizer::AllowNil).call(:stuff)
17
- end
18
-
19
- action.should raise_error
20
- end
21
- end
22
-
23
- describe InputSanitizer::PositiveIntegerConverter do
24
- let(:converter) { InputSanitizer::PositiveIntegerConverter.new }
25
-
26
- it "casts string to integer" do
27
- converter.call("3").should == 3
28
- end
29
-
30
- it "raises error if integer less than zero" do
31
- lambda { converter.call("-3") }.should raise_error(InputSanitizer::ConversionError)
32
- end
33
-
34
- it "raises error if integer equals zero" do
35
- lambda { converter.call("0") }.should raise_error(InputSanitizer::ConversionError)
36
- end
37
- end
38
-
39
- describe InputSanitizer::CommaJoinedIntegersConverter do
40
- let(:converter) { InputSanitizer::CommaJoinedIntegersConverter.new }
41
-
42
- it "parses to array of ids" do
43
- converter.call("1,2,3,5").should == [1, 2, 3, 5]
44
- end
45
-
46
- it "raises on invalid character" do
47
- lambda { converter.call(":") }.should raise_error(InputSanitizer::ConversionError)
48
- end
49
- end
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
-
63
- describe InputSanitizer::SpecificValuesConverter do
64
- let(:converter) { InputSanitizer::SpecificValuesConverter.new([:a, :b]) }
65
-
66
- it "converts valid value to symbol" do
67
- converter.call("b").should == :b
68
- end
69
-
70
- it "raises on invalid value" do
71
- lambda { converter.call("c") }.should raise_error(InputSanitizer::ConversionError)
72
- end
73
-
74
- it "converts valid value to string" do
75
- converter = InputSanitizer::SpecificValuesConverter.new(["a", "b"])
76
- converter.call("a").should == "a"
77
- end
78
- end