sinatra-my-params 0.0.9 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8391c458014310cc388fc93e899320baa879567e50ca8cb875e3a19e20ae5984
4
- data.tar.gz: a928880e81d7f1cfb6d91d2da10347398f8eca11b85bf808eca3c12af8f7a675
3
+ metadata.gz: e8b53c81f8e6331c9f40d1b36cd45393132c6345478ae80132def61c44f5e6bd
4
+ data.tar.gz: d049d0ec154d84196d13ac5db1ffe7d07ca8926e8c9a8e84f1ae0d7707b9a79c
5
5
  SHA512:
6
- metadata.gz: c18faa501799088dcf64962e616789b38a78176f4b0fdfc3b0c123e157b3f799ee7638243d6fb522ab55de945d296206d0c71e46436c4ff30444e5ee95ba8300
7
- data.tar.gz: 257127aa4473a46ee13031cef5251f9b94cbb7195444130e0cb6011fc25376bab83a09e52a5c5b03c1e2e02e7a923d3fe942702c88370b65824b3ecf87fb3a11
6
+ metadata.gz: c424f3ff45c18ada07d4e9377177793849c665ae69b6198ab1742c533b3c1dc647ff302b69e987e1d10caa87024787fa9197c940807e8c2367e54db9a31f1661
7
+ data.tar.gz: 4ea6c14c51e42bc649ad6251503173a9765188f3281bd7c56e3cd7aa2bb8aa62dc2c0e24b2fb8560c0fe06ed6868ad60675b0ea34c3462bae4cb520823250309
data/lib/permit_params.rb CHANGED
@@ -66,14 +66,23 @@ module PermitParams
66
66
  end
67
67
 
68
68
  def coerce_array(param, options = {})
69
- Array(param.split(options[:delimiter] || ',').map(&:strip))
69
+ delimiter = valid_delimiter?(param, options[:delimiter])
70
+ return unless delimiter
71
+
72
+ Array(param.split(delimiter).map(&:strip))
70
73
  end
71
74
 
72
75
  def coerce_hash(param, options = {})
73
76
  return param if param.is_a?(Hash)
74
77
 
75
- key_value = param.split(options[:delimiter] || ',').map(&:strip).map do |c|
76
- c.split(options[:separator] || ':').map(&:strip)
78
+ delimiter = valid_delimiter?(param, options[:delimiter])
79
+ return unless delimiter
80
+
81
+ separator = valid_separator?(param, options[:separator])
82
+ return unless separator
83
+
84
+ key_value = param.split(delimiter).map(&:strip).map do |c|
85
+ c.split(separator).map(&:strip)
77
86
  end
78
87
  Hash[key_value]
79
88
  end
@@ -89,6 +98,16 @@ module PermitParams
89
98
  coerced
90
99
  end
91
100
 
101
+ def valid_delimiter?(param, delimiter)
102
+ delimiter ||= ','
103
+ delimiter if delimiter && param.include?(delimiter)
104
+ end
105
+
106
+ def valid_separator?(param, separator)
107
+ separator ||= ':'
108
+ separator if separator && param.include?(separator)
109
+ end
110
+
92
111
  def coerce_shape(param, options = {})
93
112
  hash = coerce_hash(param)
94
113
  has_shape?(hash, options[:shape]) ? hash : nil
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test/unit'
4
+ require 'permit_params'
5
+ require 'rspec'
6
+ require 'rack/test'
7
+
8
+ include PermitParams
9
+
10
+ describe 'shape behaviours' do
11
+ it 'should return a hash when a pemitted is of shape' do
12
+ input = { param: { a: 1, b: 2 } }
13
+ expect(input).to eq permitted_params(
14
+ input,
15
+ { param: Shape },
16
+ false,
17
+ { shape: { a: Integer, b: Integer } }
18
+ )
19
+ end
20
+
21
+ it 'should return a hash when a pemitted is of Integer shape(deep)' do
22
+ input = { param: { a: { b: 2 } } }
23
+ expect(input).to eq permitted_params(
24
+ input,
25
+ { param: Shape },
26
+ false,
27
+ { shape: { a: { b: Integer } } }
28
+ )
29
+ end
30
+
31
+ it 'should return a hash when a pemitted is of Array shape(deep)' do
32
+ input = { param: { a: { b: [1, 2] } } }
33
+ expect(input).to eq permitted_params(
34
+ input,
35
+ { param: Shape },
36
+ false,
37
+ { shape: { a: { b: Array } } }
38
+ )
39
+ end
40
+
41
+ it 'should return a empty object when a pemitted is shape not defined' do
42
+ input = { param: { a: 1, b: 2 } }
43
+ expect({}).to eq permitted_params(
44
+ input,
45
+ { param: Shape },
46
+ false,
47
+ { shape: { a: Integer } }
48
+ )
49
+ end
50
+ end
@@ -7,140 +7,128 @@ require 'rack/test'
7
7
 
8
8
  include PermitParams
9
9
 
10
- describe 'exceptions' do
10
+ describe 'behaviours' do
11
11
  it 'should raise error when at least one param is invalid' do
12
- input = { param_1: 'a' }
12
+ input = { param: 'a' }
13
13
  expect do
14
- permitted_params(input, { param_1: Integer }, true)
14
+ permitted_params(input, { param: Integer }, true)
15
15
  end.to raise_error(InvalidParameterError, "'a' is not a valid Integer")
16
16
  end
17
17
 
18
18
  it 'should allow all params when no restriction is given' do
19
- input = { param_1: 'a string' }
19
+ input = { param: 'a string' }
20
20
  expect(input).to eq permitted_params(input)
21
21
  end
22
22
 
23
- it 'should remove a string when a pemitted is integer' do
24
- input = { param_1: 'a string' }
23
+ it 'should remove a string when permitted param is integer' do
24
+ input = { param: 'a string' }
25
25
  output = {}
26
- expect(output).to eq permitted_params(input, { param_1: Integer })
26
+ expect(output).to eq permitted_params(input, { param: Integer })
27
27
  end
28
28
 
29
- it 'should return an integer when a pemitted is integer' do
30
- input = { param_1: 1 }
31
- expect(input).to eq permitted_params(input, { param_1: Integer })
29
+ it 'should return an integer when permitted param is integer' do
30
+ input = { param: 1 }
31
+ expect(input).to eq permitted_params(input, { param: Integer })
32
32
  end
33
33
 
34
- it 'should return an integer when a pemitted can be cast into integer' do
35
- input = { param_1: '1' }
36
- output = { param_1: 1 }
37
- expect(output).to eq permitted_params(input, { param_1: Integer })
34
+ it 'should return an integer when permitted param can be cast into integer' do
35
+ input = { param: '1' }
36
+ output = { param: 1 }
37
+ expect(output).to eq permitted_params(input, { param: Integer })
38
38
  end
39
39
 
40
- it 'should return a string when a pemitted is string' do
41
- input = { param_1: 'a string' }
42
- expect(input).to eq permitted_params(input, { param_1: String })
40
+ it 'should return a string when permitted param is string' do
41
+ input = { param: 'a string' }
42
+ expect(input).to eq permitted_params(input, { param: String })
43
43
  end
44
44
 
45
- it 'should return a float when a pemitted is float' do
46
- input = { param_1: 10.0 }
47
- expect(input).to eq permitted_params(input, { param_1: Float })
45
+ it 'should return a float when permitted param is float' do
46
+ input = { param: 10.0 }
47
+ expect(input).to eq permitted_params(input, { param: Float })
48
48
  end
49
49
 
50
- it 'should return a date when a pemitted is date' do
51
- input = { param_1: Date.new }
52
- expect(input).to eq permitted_params(input, { param_1: Date })
50
+ it 'should return a date when permitted param is date' do
51
+ input = { param: Date.new }
52
+ expect(input).to eq permitted_params(input, { param: Date })
53
53
  end
54
54
 
55
- it 'should return a time when a pemitted is time' do
56
- input = { param_1: Time.new }
57
- expect(input).to eq permitted_params(input, { param_1: Time })
55
+ it 'should return a time when permitted param is time' do
56
+ input = { param: Time.new }
57
+ expect(input).to eq permitted_params(input, { param: Time })
58
58
  end
59
59
 
60
- it 'should return a false(boolean) when a pemitted is boolean' do
61
- input = { param_1: 'false' }
62
- output = { param_1: false }
63
- expect(output).to eq permitted_params(input, { param_1: Boolean })
60
+ it 'should return a false(boolean) when permitted param is boolean' do
61
+ input = { param: 'false' }
62
+ output = { param: false }
63
+ expect(output).to eq permitted_params(input, { param: Boolean })
64
64
  end
65
65
 
66
- it 'should return a true(boolean) when a pemitted is boolean' do
67
- input = { param_1: 'true' }
68
- output = { param_1: true }
69
- expect(output).to eq permitted_params(input, { param_1: Boolean })
66
+ it 'should return a true(boolean) when permitted param is boolean' do
67
+ input = { param: 'true' }
68
+ output = { param: true }
69
+ expect(output).to eq permitted_params(input, { param: Boolean })
70
70
  end
71
71
 
72
- it 'should return an array when a pemitted is array' do
73
- input = { param_1: [1, 2] }
74
- expect(input).to eq permitted_params(input, { param_1: Array })
72
+ it 'should return an array when permitted param is array' do
73
+ input = { param: [1, 2] }
74
+ expect(input).to eq permitted_params(input, { param: Array })
75
75
  end
76
76
 
77
- it 'should return an array when a pemitted is array' do
78
- input = { param_1: '1, 2' }
79
- output = { param_1: %w[1 2] }
80
- expect(output).to eq permitted_params(input, { param_1: Array })
77
+ it 'should return an array when permitted param is array' do
78
+ input = { param: '1, 2' }
79
+ output = { param: %w[1 2] }
80
+ expect(output).to eq permitted_params(input, { param: Array })
81
81
  end
82
82
 
83
- it 'should return an array when a pemitted is array' do
84
- input = { param_1: '1; 2' }
85
- output = { param_1: %w[1 2] }
86
- expect(output).to eq permitted_params(input, { param_1: Array }, false, { delimiter: ';' })
83
+ it 'should return an array when permitted param is array' do
84
+ input = { param: '1; 2' }
85
+ output = { param: %w[1 2] }
86
+ expect(output).to eq permitted_params(input, { param: Array }, false, { delimiter: ';' })
87
87
  end
88
88
 
89
- it 'should return a hash when a pemitted is hash' do
90
- input = { param_1: 'a: 1, b: 2' }
91
- output = { param_1: { 'a' => '1', 'b' => '2' } }
92
- expect(output).to eq permitted_params(input, { param_1: Hash })
89
+ it 'should return an empty hash when permitted param is array with wrong delimiter' do
90
+ input = { param: '1; 2' }
91
+ output = {}
92
+ expect(output).to eq permitted_params(input, { param: Array }, false, { delimiter: ',' })
93
93
  end
94
94
 
95
- it 'should return a hash when a pemitted is hash' do
96
- input = { param_1: 'a: 1; b: 2' }
97
- output = { param_1: { 'a' => '1', 'b' => '2' } }
98
- expect(output).to eq permitted_params(input, { param_1: Hash }, false, { delimiter: ';' })
95
+ it 'should return a hash when permitted param is hash' do
96
+ input = { param: 'a: 1, b: 2' }
97
+ output = { param: { 'a' => '1', 'b' => '2' } }
98
+ expect(output).to eq permitted_params(input, { param: Hash })
99
99
  end
100
100
 
101
- it 'should return a hash when a pemitted is hash' do
102
- input = { param_1: { a: 1 } }
103
- expect(input).to eq permitted_params(input, { param_1: Hash })
101
+ it 'should return a hash when permitted param is hash' do
102
+ input = { param: 'a: 1; b: 2' }
103
+ output = { param: { 'a' => '1', 'b' => '2' } }
104
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { delimiter: ';' })
104
105
  end
105
106
 
106
- it 'should return a hash when a pemitted is shape' do
107
- input = { param_1: { a: 1, b: 2 } }
108
- expect(input).to eq permitted_params(
109
- input,
110
- { param_1: Shape },
111
- false,
112
- { shape: { a: Integer, b: Integer } }
113
- )
107
+ it 'should return an empty hash when permitted param is hash with wrong separator' do
108
+ input = { param: 'a: 1; b: 2' }
109
+ output = {}
110
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { separator: '.' })
114
111
  end
115
112
 
116
- it 'should return a hash when a pemitted is shape(deep)' do
117
- input = { param_1: { a: { b: 2 } } }
118
- expect(input).to eq permitted_params(
119
- input,
120
- { param_1: Shape },
121
- false,
122
- { shape: { a: { b: Integer } } }
123
- )
113
+ it 'should return an empty hash when permitted param is hash with wrong delimiter' do
114
+ input = { param: 'a: 1; b: 2' }
115
+ output = {}
116
+ expect(output).to eq permitted_params(input, { param: Hash }, false, { delimiter: ',' })
124
117
  end
125
118
 
126
- it 'should return a empty when a pemitted is shape not defined' do
127
- input = { param_1: { a: 1, b: 2 } }
128
- expect({}).to eq permitted_params(
129
- input,
130
- { param_1: Shape },
131
- false,
132
- { shape: { a: Integer } }
133
- )
119
+ it 'should return a hash when permitted param is hash' do
120
+ input = { param: { a: 1 } }
121
+ expect(input).to eq permitted_params(input, { param: Hash })
134
122
  end
135
123
 
136
- it 'should return a hash when a pemitted is hash' do
137
- input = { param_1: { a: { b: 1 } } }
138
- expect(input).to eq permitted_params(input, { param_1: Hash })
124
+ it 'should return a hash when permitted param is hash' do
125
+ input = { param: { a: { b: 1 } } }
126
+ expect(input).to eq permitted_params(input, { param: Hash })
139
127
  end
140
128
 
141
- it 'should return a hash when a pemitted is hash' do
142
- input = { param_1: { "a": 1 } }
143
- expect(input).to eq permitted_params(input, { param_1: Hash })
129
+ it 'should return a hash when permitted param is hash' do
130
+ input = { param: { "a": 1 } }
131
+ expect(input).to eq permitted_params(input, { param: Hash })
144
132
  end
145
133
 
146
134
  it 'should return a several types for several inputs' do
@@ -167,21 +155,21 @@ describe 'exceptions' do
167
155
  end
168
156
  end
169
157
 
170
- input = { param_1: '1' }
171
- output = { param_1: '1' }
172
- expect(output).to eq permitted_params(input, { param_1: Any })
158
+ input = { param: '1' }
159
+ output = { param: '1' }
160
+ expect(output).to eq permitted_params(input, { param: Any })
173
161
 
174
162
  input = {
175
- param_1: TestClass.new(some_attribute: 'a string')
163
+ param: TestClass.new(some_attribute: 'a string')
176
164
  }
177
- output = permitted_params(input, { param_1: Any })
178
- expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute
165
+ output = permitted_params(input, { param: Any })
166
+ expect(input[:param].some_attribute).to eq output[:param].some_attribute
179
167
 
180
168
  input = {
181
- param_1: TestClass.new(some_attribute: 1),
182
- param_2: 2
169
+ param: TestClass.new(some_attribute: 1),
170
+ antoher_param: 2
183
171
  }
184
- output = permitted_params(input, { param_1: Any })
185
- expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute
172
+ output = permitted_params(input, { param: Any })
173
+ expect(input[:param].some_attribute).to eq output[:param].some_attribute
186
174
  end
187
175
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-my-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Aviles
@@ -10,7 +10,7 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-09-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple params sanitizer(originally created for sinatra)
13
+ description: A simple params sanitizer (originally created for sinatra)
14
14
  email: gdmarav374@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -19,6 +19,7 @@ files:
19
19
  - Rakefile
20
20
  - bin/permit_params
21
21
  - lib/permit_params.rb
22
+ - spec/permit_params_shape_spec.rb
22
23
  - spec/permit_params_spec.rb
23
24
  homepage: https://github.com/mhero/sinatra-my-params
24
25
  licenses: []
@@ -44,3 +45,4 @@ specification_version: 3
44
45
  summary: permit_params!
45
46
  test_files:
46
47
  - spec/permit_params_spec.rb
48
+ - spec/permit_params_shape_spec.rb