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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a1f7f7726f5d92878f2f65847f2d853d3ab3033b3dfaf4e84784d0bb56bd9b3
4
- data.tar.gz: 4ebf4d253a1286535ff93503433695867faf4ab4c3b3fd08076e807ba40535f9
3
+ metadata.gz: 8391c458014310cc388fc93e899320baa879567e50ca8cb875e3a19e20ae5984
4
+ data.tar.gz: a928880e81d7f1cfb6d91d2da10347398f8eca11b85bf808eca3c12af8f7a675
5
5
  SHA512:
6
- metadata.gz: b87393662b298d9acf0c127427ed77894c2c378fc05f4f737c0412c60180eae68f99ba55c7e6bafdfd42afc8428971482c19cac9bb3c0524ca289f741488364c
7
- data.tar.gz: d9a899475bb3e5d6af6cd2c5d539b645a5770e9d4de99cf37797899058bd0ad18c2bec8799905aab8dd3c46cbacf8915d3ad97679684dfce15e56583d9d5c554
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.keys.map(&:to_s).include?(key.to_s) && !value.nil?
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
@@ -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
- test_class = TestClass.new
141
- input = { param_1: test_class }
142
- output = { param_1: test_class }
143
- expect(output).to eq permitted_params(input, { param_1: Any })
144
-
145
- test_class = TestClass.new
146
- input = { param_1: test_class, param_2: 2 }
147
- output = { param_1: test_class }
148
- expect(output).to eq permitted_params(input, { param_1: Any })
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.8
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 sinatra params sanitizer
13
+ description: A simple params sanitizer(originally created for sinatra)
14
14
  email: gdmarav374@gmail.com
15
15
  executables: []
16
16
  extensions: []