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
metadata CHANGED
@@ -1,106 +1,171 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: input_sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
5
- prerelease:
4
+ version: 0.4.1
6
5
  platform: ruby
7
6
  authors:
8
- - Tomek Paczkowski
9
- - Tomasz Werbicki
10
- - Michal Bugno
7
+ - Zendesk
11
8
  autorequire:
12
9
  bindir: bin
13
10
  cert_chain: []
14
- date: 2013-05-13 00:00:00.000000000 Z
11
+ date: 2021-05-17 00:00:00.000000000 Z
15
12
  dependencies:
16
13
  - !ruby/object:Gem::Dependency
17
- name: rspec
14
+ name: method_struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
18
29
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
30
  requirements:
21
- - - ! '>='
31
+ - - ">="
22
32
  - !ruby/object:Gem::Version
23
- version: '0'
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.1
24
48
  type: :development
25
49
  prerelease: false
26
50
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
51
  requirements:
29
- - - ! '>='
52
+ - - "~>"
30
53
  - !ruby/object:Gem::Version
31
- version: '0'
54
+ version: 0.10.1
32
55
  - !ruby/object:Gem::Dependency
33
56
  name: simplecov
34
57
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
58
  requirements:
37
- - - ! '>='
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
+ - - "~>"
38
74
  - !ruby/object:Gem::Version
39
- version: '0'
75
+ version: 3.2.0
40
76
  type: :development
41
77
  prerelease: false
42
78
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
79
  requirements:
45
- - - ! '>='
80
+ - - "~>"
46
81
  - !ruby/object:Gem::Version
47
- version: '0'
82
+ version: 3.2.0
48
83
  description: Gem to sanitize hash of incoming data
49
84
  email:
50
- - tom@futuresimple.com
51
- - tomasz@futuresimple.com
52
- - michal@futuresimple.com
85
+ - opensource@zendesk.com
53
86
  executables: []
54
87
  extensions: []
55
88
  extra_rdoc_files: []
56
89
  files:
57
- - .gitignore
58
- - .rspec
59
- - .travis.yml
90
+ - ".github/workflows/ci.yaml"
91
+ - ".github/workflows/gempush.yml"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - CHANGELOG
60
95
  - Gemfile
61
96
  - LICENSE
62
97
  - README.md
63
98
  - Rakefile
64
99
  - input_sanitizer.gemspec
65
100
  - lib/input_sanitizer.rb
66
- - lib/input_sanitizer/default_converters.rb
101
+ - lib/input_sanitizer/errors.rb
67
102
  - lib/input_sanitizer/extended_converters.rb
103
+ - lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb
104
+ - lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb
105
+ - lib/input_sanitizer/extended_converters/positive_integer_converter.rb
106
+ - lib/input_sanitizer/extended_converters/specific_values_converter.rb
68
107
  - lib/input_sanitizer/restricted_hash.rb
69
- - lib/input_sanitizer/sanitizer.rb
108
+ - lib/input_sanitizer/v1.rb
109
+ - lib/input_sanitizer/v1/clean_field.rb
110
+ - lib/input_sanitizer/v1/default_converters.rb
111
+ - lib/input_sanitizer/v1/sanitizer.rb
112
+ - lib/input_sanitizer/v2.rb
113
+ - lib/input_sanitizer/v2/clean_field.rb
114
+ - lib/input_sanitizer/v2/clean_payload_collection_field.rb
115
+ - lib/input_sanitizer/v2/clean_query_collection_field.rb
116
+ - lib/input_sanitizer/v2/error_collection.rb
117
+ - lib/input_sanitizer/v2/nested_sanitizer_factory.rb
118
+ - lib/input_sanitizer/v2/payload_sanitizer.rb
119
+ - lib/input_sanitizer/v2/payload_transform.rb
120
+ - lib/input_sanitizer/v2/query_sanitizer.rb
121
+ - lib/input_sanitizer/v2/types.rb
70
122
  - lib/input_sanitizer/version.rb
71
- - spec/default_converters_spec.rb
72
- - spec/extended_converters_spec.rb
123
+ - spec/extended_converters/comma_joined_integers_converter_spec.rb
124
+ - spec/extended_converters/comma_joined_strings_converter_spec.rb
125
+ - spec/extended_converters/positive_integer_converter_spec.rb
126
+ - spec/extended_converters/specific_values_converter_spec.rb
73
127
  - spec/restricted_hash_spec.rb
74
128
  - spec/sanitizer_spec.rb
75
129
  - spec/spec_helper.rb
130
+ - spec/v1/default_converters_spec.rb
131
+ - spec/v2/converters_spec.rb
132
+ - spec/v2/payload_sanitizer_spec.rb
133
+ - spec/v2/payload_transform_spec.rb
134
+ - spec/v2/query_sanitizer_spec.rb
135
+ - v2.md
76
136
  homepage: ''
77
- licenses: []
137
+ licenses:
138
+ - Apache-2.0
139
+ metadata: {}
78
140
  post_install_message:
79
141
  rdoc_options: []
80
142
  require_paths:
81
143
  - lib
82
144
  required_ruby_version: !ruby/object:Gem::Requirement
83
- none: false
84
145
  requirements:
85
- - - ! '>='
146
+ - - ">="
86
147
  - !ruby/object:Gem::Version
87
148
  version: '0'
88
149
  required_rubygems_version: !ruby/object:Gem::Requirement
89
- none: false
90
150
  requirements:
91
- - - ! '>='
151
+ - - ">="
92
152
  - !ruby/object:Gem::Version
93
153
  version: '0'
94
154
  requirements: []
95
- rubyforge_project:
96
- rubygems_version: 1.8.23
155
+ rubygems_version: 3.0.3.1
97
156
  signing_key:
98
- specification_version: 3
157
+ specification_version: 4
99
158
  summary: Gem to sanitize hash of incoming data
100
159
  test_files:
101
- - spec/default_converters_spec.rb
102
- - spec/extended_converters_spec.rb
160
+ - spec/extended_converters/comma_joined_integers_converter_spec.rb
161
+ - spec/extended_converters/comma_joined_strings_converter_spec.rb
162
+ - spec/extended_converters/positive_integer_converter_spec.rb
163
+ - spec/extended_converters/specific_values_converter_spec.rb
103
164
  - spec/restricted_hash_spec.rb
104
165
  - spec/sanitizer_spec.rb
105
166
  - spec/spec_helper.rb
106
- has_rdoc:
167
+ - spec/v1/default_converters_spec.rb
168
+ - spec/v2/converters_spec.rb
169
+ - spec/v2/payload_sanitizer_spec.rb
170
+ - spec/v2/payload_transform_spec.rb
171
+ - spec/v2/query_sanitizer_spec.rb
data/.travis.yml DELETED
@@ -1,12 +0,0 @@
1
- language: ruby
2
- script: bundle exec rspec spec
3
- rvm:
4
- - 1.8.7
5
- - 1.9.2
6
- - 1.9.3
7
- - jruby-18mode
8
- - jruby-19mode
9
- - rbx-18mode
10
-
11
- # keeps failing
12
- #- rbx-19mode
@@ -1,140 +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
- clean_field(field, type, required)
26
- end
27
- @performed = true
28
- @cleaned.freeze
29
- end
30
-
31
- def valid?
32
- cleaned
33
- @errors.empty?
34
- end
35
-
36
- def errors
37
- cleaned
38
- @errors
39
- end
40
-
41
- def self.converters
42
- {
43
- :integer => InputSanitizer::IntegerConverter.new,
44
- :string => InputSanitizer::StringConverter.new,
45
- :date => InputSanitizer::DateConverter.new,
46
- :time => InputSanitizer::TimeConverter.new,
47
- :boolean => InputSanitizer::BooleanConverter.new,
48
- :integer_or_blank => InputSanitizer::IntegerConverter.new.extend(InputSanitizer::AllowNil),
49
- :string_or_blank => InputSanitizer::StringConverter.new.extend(InputSanitizer::AllowNil),
50
- :date_or_blank => InputSanitizer::DateConverter.new.extend(InputSanitizer::AllowNil),
51
- :time_or_blank => InputSanitizer::TimeConverter.new.extend(InputSanitizer::AllowNil),
52
- :boolean_or_blank => InputSanitizer::BooleanConverter.new.extend(InputSanitizer::AllowNil),
53
- }
54
- end
55
-
56
- def self.inherited(subclass)
57
- subclass.fields = self.fields.dup
58
- end
59
-
60
- converters.keys.each do |name|
61
- class_eval <<-END
62
- def self.#{name}(*keys)
63
- set_keys_to_type(keys, :#{name})
64
- end
65
- END
66
- end
67
-
68
- def self.custom(*keys)
69
- options = keys.pop
70
- converter = options.delete(:converter)
71
- keys.push(options)
72
- raise "You did not define a converter for a custom type" if converter == nil
73
- self.set_keys_to_type(keys, converter)
74
- end
75
-
76
- protected
77
- def self.fields
78
- @fields ||= {}
79
- end
80
-
81
- def self.fields=(new_fields)
82
- @fields = new_fields
83
- end
84
-
85
- private
86
- def self.extract_options!(array)
87
- array.last.is_a?(Hash) ? array.pop : {}
88
- end
89
-
90
- def self.extract_options(array)
91
- array.last.is_a?(Hash) ? array.last : {}
92
- end
93
-
94
- def clean_field(field, type, required)
95
- if @data.has_key?(field)
96
- begin
97
- @cleaned[field] = convert(field, type)
98
- rescue InputSanitizer::ConversionError => ex
99
- add_error(field, :invalid_value, @data[field], ex.message)
100
- end
101
- elsif required
102
- add_missing(field)
103
- end
104
- end
105
-
106
- def add_error(field, error_type, value, description = nil)
107
- @errors << {
108
- :field => field,
109
- :type => error_type,
110
- :value => value,
111
- :description => description
112
- }
113
- end
114
-
115
- def add_missing(field)
116
- add_error(field, :missing, nil, nil)
117
- end
118
-
119
- def convert(field, type)
120
- converter(type).call(@data[field])
121
- end
122
-
123
- def converter(type)
124
- type.respond_to?(:call) ? type : self.class.converters[type]
125
- end
126
-
127
- def symbolize_keys(data)
128
- data.inject({}) do |memo, kv|
129
- memo[kv.first.to_sym] = kv.last
130
- memo
131
- end
132
- end
133
-
134
- def self.set_keys_to_type(keys, type)
135
- opts = extract_options!(keys)
136
- keys.each do |key|
137
- fields[key] = { :type => type, :options => opts }
138
- end
139
- end
140
- end
@@ -1,101 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe InputSanitizer::IntegerConverter do
4
- let(:converter) { InputSanitizer::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::DateConverter do
20
- let(:converter) { InputSanitizer::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::BooleanConverter do
32
- let(:converter) { InputSanitizer::BooleanConverter.new }
33
-
34
- it "casts 'true' to true" do
35
- converter.call('true').should be_true
36
- end
37
-
38
- it "casts true to true" do
39
- converter.call(true).should be_true
40
- end
41
-
42
- it "casts '1' to true" do
43
- converter.call('1').should be_true
44
- end
45
-
46
- it "casts 'yes' to true" do
47
- converter.call('yes').should be_true
48
- end
49
-
50
- it "casts 'false' to false" do
51
- converter.call('false').should be_false
52
- end
53
-
54
- it "casts false to false" do
55
- converter.call(false).should be_false
56
- end
57
-
58
- it "casts '0' to false" do
59
- converter.call('0').should be_false
60
- end
61
-
62
- it "casts 'no' to false" do
63
- converter.call('no').should be_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
-
72
- describe InputSanitizer::TimeConverter do
73
- let(:converter) { InputSanitizer::TimeConverter.new }
74
-
75
- it "raises if timezone part given" do
76
- lambda { converter.call("2012-05-15 13:42:54 +01:00") }.should raise_error(InputSanitizer::ConversionError)
77
- end
78
-
79
- it "casts date time in iso format" do
80
- t = Time.utc(2012, 5, 15, 13, 42, 54)
81
- converter.call("2012-05-15 13:42:54").should == t
82
- converter.call("2012-05-15T13:42:54").should == t
83
- converter.call("20120515134254").should == t
84
-
85
- end
86
-
87
- it "does not require time part" do
88
- converter.call("2012-05-15 13:42").should == Time.utc(2012, 5, 15, 13, 42)
89
- converter.call("2012-05-15 13").should == Time.utc(2012, 5, 15, 13)
90
- converter.call("2012-05-15").should == Time.utc(2012, 5, 15)
91
-
92
- end
93
-
94
- it "raises error if can format is wrong" do
95
- lambda { converter.call("2/10/2031 13:44:22") }.should raise_error(InputSanitizer::ConversionError)
96
- end
97
-
98
- it "raises error if date is wrong" do
99
- lambda { converter.call("2012-02-32") }.should raise_error(InputSanitizer::ConversionError)
100
- end
101
- end