sinatra-my-params 0.0.8 → 0.0.9
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 +20 -1
- data/spec/permit_params_spec.rb +50 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8391c458014310cc388fc93e899320baa879567e50ca8cb875e3a19e20ae5984
|
4
|
+
data.tar.gz: a928880e81d7f1cfb6d91d2da10347398f8eca11b85bf808eca3c12af8f7a675
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c18faa501799088dcf64962e616789b38a78176f4b0fdfc3b0c123e157b3f799ee7638243d6fb522ab55de945d296206d0c71e46436c4ff30444e5ee95ba8300
|
7
|
+
data.tar.gz: 257127aa4473a46ee13031cef5251f9b94cbb7195444130e0cb6011fc25376bab83a09e52a5c5b03c1e2e02e7a923d3fe942702c88370b65824b3ecf87fb3a11
|
data/lib/permit_params.rb
CHANGED
@@ -11,7 +11,7 @@ module PermitParams
|
|
11
11
|
coerced_params = Hash.new({})
|
12
12
|
|
13
13
|
params.each do |key, value|
|
14
|
-
next unless permitted
|
14
|
+
next unless permitted?(permitted: permitted, key: key, value: value)
|
15
15
|
|
16
16
|
coerced = coerce(
|
17
17
|
param: value,
|
@@ -28,6 +28,11 @@ module PermitParams
|
|
28
28
|
|
29
29
|
Boolean = :boolean
|
30
30
|
Any = :any
|
31
|
+
Shape = :shape
|
32
|
+
|
33
|
+
def permitted?(permitted:, key:, value:)
|
34
|
+
permitted.keys.map(&:to_s).include?(key.to_s) && !value.nil?
|
35
|
+
end
|
31
36
|
|
32
37
|
def coerce(param:, type:, strong_validation: false, options: {})
|
33
38
|
return param if type == Any
|
@@ -46,6 +51,7 @@ module PermitParams
|
|
46
51
|
return Time.parse(param) if type == Time
|
47
52
|
return DateTime.parse(param) if type == DateTime
|
48
53
|
return coerce_array(param, options) if type == Array
|
54
|
+
return coerce_shape(param, options) if type == Shape
|
49
55
|
return coerce_hash(param, options) if type == Hash
|
50
56
|
return coerce_boolean(param) if [TrueClass, FalseClass, Boolean].include? type
|
51
57
|
|
@@ -64,6 +70,8 @@ module PermitParams
|
|
64
70
|
end
|
65
71
|
|
66
72
|
def coerce_hash(param, options = {})
|
73
|
+
return param if param.is_a?(Hash)
|
74
|
+
|
67
75
|
key_value = param.split(options[:delimiter] || ',').map(&:strip).map do |c|
|
68
76
|
c.split(options[:separator] || ':').map(&:strip)
|
69
77
|
end
|
@@ -80,4 +88,15 @@ module PermitParams
|
|
80
88
|
|
81
89
|
coerced
|
82
90
|
end
|
91
|
+
|
92
|
+
def coerce_shape(param, options = {})
|
93
|
+
hash = coerce_hash(param)
|
94
|
+
has_shape?(hash, options[:shape]) ? hash : nil
|
95
|
+
end
|
96
|
+
|
97
|
+
def has_shape?(hash, shape)
|
98
|
+
hash.all? do |k, v|
|
99
|
+
v.is_a?(Hash) ? has_shape?(v, shape[k]) : shape[k] === v
|
100
|
+
end
|
101
|
+
end
|
83
102
|
end
|
data/spec/permit_params_spec.rb
CHANGED
@@ -8,10 +8,6 @@ require 'rack/test'
|
|
8
8
|
include PermitParams
|
9
9
|
|
10
10
|
describe 'exceptions' do
|
11
|
-
before do
|
12
|
-
class TestClass; end
|
13
|
-
end
|
14
|
-
|
15
11
|
it 'should raise error when at least one param is invalid' do
|
16
12
|
input = { param_1: 'a' }
|
17
13
|
expect do
|
@@ -107,6 +103,36 @@ describe 'exceptions' do
|
|
107
103
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
108
104
|
end
|
109
105
|
|
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
|
+
)
|
114
|
+
end
|
115
|
+
|
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
|
+
)
|
124
|
+
end
|
125
|
+
|
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
|
+
)
|
134
|
+
end
|
135
|
+
|
110
136
|
it 'should return a hash when a pemitted is hash' do
|
111
137
|
input = { param_1: { a: { b: 1 } } }
|
112
138
|
expect(input).to eq permitted_params(input, { param_1: Hash })
|
@@ -133,18 +159,29 @@ describe 'exceptions' do
|
|
133
159
|
end
|
134
160
|
|
135
161
|
it 'returns the paramter without casting if Any' do
|
162
|
+
class TestClass
|
163
|
+
attr_accessor :some_attribute
|
164
|
+
|
165
|
+
def initialize(some_attribute: nil)
|
166
|
+
@some_attribute = some_attribute
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
136
170
|
input = { param_1: '1' }
|
137
171
|
output = { param_1: '1' }
|
138
172
|
expect(output).to eq permitted_params(input, { param_1: Any })
|
139
173
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
input = {
|
147
|
-
|
148
|
-
|
174
|
+
input = {
|
175
|
+
param_1: TestClass.new(some_attribute: 'a string')
|
176
|
+
}
|
177
|
+
output = permitted_params(input, { param_1: Any })
|
178
|
+
expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute
|
179
|
+
|
180
|
+
input = {
|
181
|
+
param_1: TestClass.new(some_attribute: 1),
|
182
|
+
param_2: 2
|
183
|
+
}
|
184
|
+
output = permitted_params(input, { param_1: Any })
|
185
|
+
expect(input[:param_1].some_attribute).to eq output[:param_1].some_attribute
|
149
186
|
end
|
150
187
|
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.9
|
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
|
13
|
+
description: A simple params sanitizer(originally created for sinatra)
|
14
14
|
email: gdmarav374@gmail.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|