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 +4 -4
- data/lib/permit_params.rb +22 -3
- data/spec/permit_params_shape_spec.rb +50 -0
- data/spec/permit_params_spec.rb +84 -96
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8b53c81f8e6331c9f40d1b36cd45393132c6345478ae80132def61c44f5e6bd
|
4
|
+
data.tar.gz: d049d0ec154d84196d13ac5db1ffe7d07ca8926e8c9a8e84f1ae0d7707b9a79c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
76
|
-
|
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
|
data/spec/permit_params_spec.rb
CHANGED
@@ -7,140 +7,128 @@ require 'rack/test'
|
|
7
7
|
|
8
8
|
include PermitParams
|
9
9
|
|
10
|
-
describe '
|
10
|
+
describe 'behaviours' do
|
11
11
|
it 'should raise error when at least one param is invalid' do
|
12
|
-
input = {
|
12
|
+
input = { param: 'a' }
|
13
13
|
expect do
|
14
|
-
permitted_params(input, {
|
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 = {
|
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
|
24
|
-
input = {
|
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, {
|
26
|
+
expect(output).to eq permitted_params(input, { param: Integer })
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'should return an integer when
|
30
|
-
input = {
|
31
|
-
expect(input).to eq permitted_params(input, {
|
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
|
35
|
-
input = {
|
36
|
-
output = {
|
37
|
-
expect(output).to eq permitted_params(input, {
|
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
|
41
|
-
input = {
|
42
|
-
expect(input).to eq permitted_params(input, {
|
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
|
46
|
-
input = {
|
47
|
-
expect(input).to eq permitted_params(input, {
|
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
|
51
|
-
input = {
|
52
|
-
expect(input).to eq permitted_params(input, {
|
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
|
56
|
-
input = {
|
57
|
-
expect(input).to eq permitted_params(input, {
|
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
|
61
|
-
input = {
|
62
|
-
output = {
|
63
|
-
expect(output).to eq permitted_params(input, {
|
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
|
67
|
-
input = {
|
68
|
-
output = {
|
69
|
-
expect(output).to eq permitted_params(input, {
|
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
|
73
|
-
input = {
|
74
|
-
expect(input).to eq permitted_params(input, {
|
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
|
78
|
-
input = {
|
79
|
-
output = {
|
80
|
-
expect(output).to eq permitted_params(input, {
|
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
|
84
|
-
input = {
|
85
|
-
output = {
|
86
|
-
expect(output).to eq permitted_params(input, {
|
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
|
90
|
-
input = {
|
91
|
-
output = {
|
92
|
-
expect(output).to eq permitted_params(input, {
|
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
|
96
|
-
input = {
|
97
|
-
output = {
|
98
|
-
expect(output).to eq permitted_params(input, {
|
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
|
102
|
-
input = {
|
103
|
-
|
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
|
107
|
-
input = {
|
108
|
-
|
109
|
-
|
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
|
117
|
-
input = {
|
118
|
-
|
119
|
-
|
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
|
127
|
-
input = {
|
128
|
-
expect(
|
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
|
137
|
-
input = {
|
138
|
-
expect(input).to eq permitted_params(input, {
|
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
|
142
|
-
input = {
|
143
|
-
expect(input).to eq permitted_params(input, {
|
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 = {
|
171
|
-
output = {
|
172
|
-
expect(output).to eq permitted_params(input, {
|
158
|
+
input = { param: '1' }
|
159
|
+
output = { param: '1' }
|
160
|
+
expect(output).to eq permitted_params(input, { param: Any })
|
173
161
|
|
174
162
|
input = {
|
175
|
-
|
163
|
+
param: TestClass.new(some_attribute: 'a string')
|
176
164
|
}
|
177
|
-
output = permitted_params(input, {
|
178
|
-
expect(input[:
|
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
|
-
|
182
|
-
|
169
|
+
param: TestClass.new(some_attribute: 1),
|
170
|
+
antoher_param: 2
|
183
171
|
}
|
184
|
-
output = permitted_params(input, {
|
185
|
-
expect(input[:
|
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.
|
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
|