compel 0.3.7 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/compel.gemspec +0 -1
- data/lib/compel.rb +0 -1
- data/lib/compel/builder/schema.rb +1 -1
- data/lib/compel/coercion/types/hash.rb +12 -1
- data/lib/compel/errors.rb +1 -1
- data/lib/compel/validators/hash_validator.rb +1 -3
- data/lib/compel/version.rb +1 -1
- data/spec/compel/builder_spec.rb +27 -27
- data/spec/compel/coercion_spec.rb +8 -8
- data/spec/compel/compel_spec.rb +69 -84
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 032ba6894d54a63ee956025716cb973156d601b0
|
4
|
+
data.tar.gz: 0121184a327f472d7750180db68411253f91d465
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fb359e62420fc5a8b780297bfc05a9ffcc986034d362e4cace62626a4f8e67c0e75a38013335f8fe1eb48c93a560ba2bbc41b9ecdfd49eeaa591443fcfb1f71
|
7
|
+
data.tar.gz: f933cdda27071b04278ff0812f91c7956a4a92e478c08c6912cef70878de9bddb88860e5b7f074bedcdcd3620c002c919c4b13a69ae9ce71abe2f58371d0480e
|
data/README.md
CHANGED
data/compel.gemspec
CHANGED
@@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ['lib']
|
20
20
|
|
21
|
-
gem.add_runtime_dependency 'hashie', '~> 3.4'
|
22
21
|
gem.add_development_dependency 'rspec', '~> 3.2'
|
23
22
|
gem.add_development_dependency 'rake', '~> 0'
|
24
23
|
gem.add_development_dependency 'pry', '~> 0'
|
data/lib/compel.rb
CHANGED
@@ -4,9 +4,20 @@ module Compel
|
|
4
4
|
class Hash < Type
|
5
5
|
|
6
6
|
def coerce_value
|
7
|
-
|
7
|
+
if ::Hash.try_convert(value)
|
8
|
+
symbolyze_keys(value)
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
12
|
+
private
|
13
|
+
|
14
|
+
def symbolyze_keys(hash)
|
15
|
+
{}.tap do |symbolyzed_hash|
|
16
|
+
hash.each do |key, value|
|
17
|
+
symbolyzed_hash[key.to_sym] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
10
21
|
end
|
11
22
|
|
12
23
|
end
|
data/lib/compel/errors.rb
CHANGED
@@ -20,8 +20,6 @@ module Compel
|
|
20
20
|
return self
|
21
21
|
end
|
22
22
|
|
23
|
-
@input = Hashie::Mash.new(input)
|
24
|
-
|
25
23
|
keys_validator = \
|
26
24
|
HashKeysValidator.validate(input, keys_schemas)
|
27
25
|
|
@@ -32,7 +30,7 @@ module Compel
|
|
32
30
|
end
|
33
31
|
|
34
32
|
def serialize
|
35
|
-
coerced = output.is_a?(Hash) ? input.merge(output) :
|
33
|
+
coerced = output.is_a?(Hash) ? input.merge(output) : {}
|
36
34
|
|
37
35
|
coerced.tap do |hash|
|
38
36
|
if !errors.empty?
|
data/lib/compel/version.rb
CHANGED
data/spec/compel/builder_spec.rb
CHANGED
@@ -4,11 +4,11 @@ describe Compel::Builder do
|
|
4
4
|
|
5
5
|
context 'Build' do
|
6
6
|
|
7
|
-
it 'should build new Schema for
|
7
|
+
it 'should build new Schema for given type' do
|
8
8
|
builder = Compel.string
|
9
9
|
|
10
10
|
expect(builder.type).to be(Compel::Coercion::String)
|
11
|
-
expect(builder.options.keys).to include(
|
11
|
+
expect(builder.options.keys).to include(:required)
|
12
12
|
expect(builder.required?).to be false
|
13
13
|
expect(builder.default_value).to be nil
|
14
14
|
end
|
@@ -298,14 +298,14 @@ describe Compel::Builder do
|
|
298
298
|
|
299
299
|
keys_schemas = schema.options[:keys]
|
300
300
|
|
301
|
-
expect(keys_schemas
|
302
|
-
expect(keys_schemas
|
303
|
-
expect(keys_schemas
|
304
|
-
expect(keys_schemas
|
305
|
-
expect(keys_schemas
|
306
|
-
expect(keys_schemas
|
307
|
-
expect(keys_schemas
|
308
|
-
expect(keys_schemas
|
301
|
+
expect(keys_schemas[:a].type).to be Compel::Coercion::Float
|
302
|
+
expect(keys_schemas[:b].type).to be Compel::Coercion::String
|
303
|
+
expect(keys_schemas[:c].type).to be Compel::Coercion::Hash
|
304
|
+
expect(keys_schemas[:d].type).to be Compel::Coercion::JSON
|
305
|
+
expect(keys_schemas[:e].type).to be Compel::Coercion::Time
|
306
|
+
expect(keys_schemas[:f].type).to be Compel::Coercion::DateTime
|
307
|
+
expect(keys_schemas[:g].type).to be Compel::Coercion::Date
|
308
|
+
expect(keys_schemas[:h].type).to be Compel::Coercion::Integer
|
309
309
|
end
|
310
310
|
|
311
311
|
it 'should raise error for invalid #keys' do
|
@@ -557,19 +557,19 @@ describe Compel::Builder do
|
|
557
557
|
|
558
558
|
expect(result.value).to \
|
559
559
|
eq({
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
560
|
+
first_name: 'Joaquim',
|
561
|
+
birth_date: '1989-0',
|
562
|
+
address: {
|
563
|
+
line_one: 'Lisboa',
|
564
|
+
post_code: '1100',
|
565
|
+
country_code: 'PT',
|
566
|
+
line_two: '-'
|
567
567
|
},
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
568
|
+
errors: {
|
569
|
+
last_name: ['is required'],
|
570
|
+
birth_date: ["'1989-0' is not a parsable date with format: %Y-%m-%d"],
|
571
|
+
address: {
|
572
|
+
post_code: ["must match format ^\\d{4}-\\d{3}$"]
|
573
573
|
}
|
574
574
|
}
|
575
575
|
})
|
@@ -580,7 +580,7 @@ describe Compel::Builder do
|
|
580
580
|
a: Compel.float.required
|
581
581
|
})
|
582
582
|
|
583
|
-
expect(schema.validate({ a: nil }).errors
|
583
|
+
expect(schema.validate({ a: nil }).errors[:a]).to \
|
584
584
|
include('is required')
|
585
585
|
end
|
586
586
|
|
@@ -613,7 +613,7 @@ describe Compel::Builder do
|
|
613
613
|
result = schema.validate({ a: 1, b: 2, c: 3 })
|
614
614
|
|
615
615
|
expect(result.errors[:base]).to \
|
616
|
-
include("must be {
|
616
|
+
include("must be #{value.to_hash}")
|
617
617
|
end
|
618
618
|
|
619
619
|
it 'should validate without errors' do
|
@@ -763,9 +763,9 @@ describe Compel::Builder do
|
|
763
763
|
expect(result.valid?).to be true
|
764
764
|
expect(result.value).to eq \
|
765
765
|
[
|
766
|
-
|
767
|
-
|
768
|
-
|
766
|
+
{ a: 'A', b: 1 },
|
767
|
+
{ a: 'B' },
|
768
|
+
{ a: 'C', b: 3 }
|
769
769
|
]
|
770
770
|
end
|
771
771
|
|
@@ -149,8 +149,8 @@ describe Compel::Coercion do
|
|
149
149
|
}, Compel::Coercion::Hash)
|
150
150
|
|
151
151
|
expect(value).to eq({
|
152
|
-
|
153
|
-
|
152
|
+
first_name: 'Joaquim',
|
153
|
+
last_name: 'Adráz'
|
154
154
|
})
|
155
155
|
end
|
156
156
|
|
@@ -161,20 +161,20 @@ describe Compel::Coercion do
|
|
161
161
|
}, Compel::Coercion::Hash)
|
162
162
|
|
163
163
|
expect(value).to eq({
|
164
|
-
|
165
|
-
|
164
|
+
first_name: 'Joaquim',
|
165
|
+
last_name: 'Adráz'
|
166
166
|
})
|
167
167
|
end
|
168
168
|
|
169
169
|
it 'should coerce 2' do
|
170
|
-
value = Compel::Coercion.coerce!(
|
170
|
+
value = Compel::Coercion.coerce!({
|
171
171
|
first_name: 'Joaquim',
|
172
172
|
last_name: 'Adráz'
|
173
|
-
}
|
173
|
+
}, Compel::Coercion::Hash)
|
174
174
|
|
175
175
|
expect(value).to eq({
|
176
|
-
|
177
|
-
|
176
|
+
first_name: 'Joaquim',
|
177
|
+
last_name: 'Adráz'
|
178
178
|
})
|
179
179
|
end
|
180
180
|
|
data/spec/compel/compel_spec.rb
CHANGED
@@ -39,18 +39,16 @@ describe Compel do
|
|
39
39
|
|
40
40
|
expect(result.valid?).to be true
|
41
41
|
expect(result.value).to eq \
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
admin: false
|
51
|
-
}
|
42
|
+
user: {
|
43
|
+
first_name: 'Joaquim',
|
44
|
+
last_name: 'Adráz',
|
45
|
+
birth_date: DateTime.parse('1989-08-06T09:00:00'),
|
46
|
+
age: 26,
|
47
|
+
admin: false,
|
48
|
+
blog_role: {
|
49
|
+
admin: false
|
52
50
|
}
|
53
|
-
}
|
51
|
+
}
|
54
52
|
end
|
55
53
|
|
56
54
|
end
|
@@ -69,17 +67,15 @@ describe Compel do
|
|
69
67
|
|
70
68
|
expect(result.valid?).to be false
|
71
69
|
expect(result.value).to eq \
|
72
|
-
|
73
|
-
|
70
|
+
other_param: 1,
|
71
|
+
user: {
|
72
|
+
first_name: 'Joaquim',
|
73
|
+
},
|
74
|
+
errors: {
|
74
75
|
user: {
|
75
|
-
|
76
|
-
},
|
77
|
-
errors: {
|
78
|
-
user: {
|
79
|
-
last_name: ['is required']
|
80
|
-
}
|
76
|
+
last_name: ['is required']
|
81
77
|
}
|
82
|
-
}
|
78
|
+
}
|
83
79
|
end
|
84
80
|
|
85
81
|
it 'should not compel for invalid hash' do
|
@@ -114,16 +110,14 @@ describe Compel do
|
|
114
110
|
|
115
111
|
expect(result.valid?).to be false
|
116
112
|
expect(result.value).to eq \
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
last_name: ['is required']
|
124
|
-
}
|
113
|
+
user:{
|
114
|
+
first_name: 'Joaquim',
|
115
|
+
},
|
116
|
+
errors: {
|
117
|
+
user: {
|
118
|
+
last_name: ['is required']
|
125
119
|
}
|
126
|
-
}
|
120
|
+
}
|
127
121
|
end
|
128
122
|
|
129
123
|
end
|
@@ -181,17 +175,15 @@ describe Compel do
|
|
181
175
|
|
182
176
|
expect(result.valid?).to be false
|
183
177
|
expect(result.value).to eq \
|
184
|
-
|
178
|
+
address: {
|
179
|
+
line_two: 'Portugal'
|
180
|
+
},
|
181
|
+
errors: {
|
185
182
|
address: {
|
186
|
-
|
187
|
-
|
188
|
-
errors: {
|
189
|
-
address: {
|
190
|
-
line_one: ['is required'],
|
191
|
-
post_code: ['is required']
|
192
|
-
}
|
183
|
+
line_one: ['is required'],
|
184
|
+
post_code: ['is required']
|
193
185
|
}
|
194
|
-
}
|
186
|
+
}
|
195
187
|
end
|
196
188
|
|
197
189
|
it 'should not compel missing key and length invalid' do
|
@@ -211,25 +203,23 @@ describe Compel do
|
|
211
203
|
|
212
204
|
expect(result.valid?).to be false
|
213
205
|
expect(result.value).to eq \
|
214
|
-
|
206
|
+
address: {
|
207
|
+
line_two: 'Portugal',
|
208
|
+
post_code: {
|
209
|
+
prefix: 1,
|
210
|
+
county: {
|
211
|
+
code: 'LX'
|
212
|
+
}
|
213
|
+
}
|
214
|
+
},
|
215
|
+
errors: {
|
215
216
|
address: {
|
216
|
-
|
217
|
+
line_one: ['is required'],
|
217
218
|
post_code: {
|
218
|
-
prefix:
|
219
|
-
county: {
|
220
|
-
code: 'LX'
|
221
|
-
}
|
222
|
-
}
|
223
|
-
},
|
224
|
-
errors: {
|
225
|
-
address: {
|
226
|
-
line_one: ['is required'],
|
227
|
-
post_code: {
|
228
|
-
prefix: ['cannot have length different than 4']
|
229
|
-
}
|
219
|
+
prefix: ['cannot have length different than 4']
|
230
220
|
}
|
231
221
|
}
|
232
|
-
}
|
222
|
+
}
|
233
223
|
end
|
234
224
|
|
235
225
|
it 'should not compel for givin invalid optional value' do
|
@@ -248,25 +238,23 @@ describe Compel do
|
|
248
238
|
|
249
239
|
expect(result.valid?).to be false
|
250
240
|
expect(result.value).to eq \
|
251
|
-
|
241
|
+
address: {
|
242
|
+
line_one: 'Line',
|
243
|
+
post_code: {
|
244
|
+
prefix: 1100,
|
245
|
+
suffix: 100,
|
246
|
+
county: {}
|
247
|
+
}
|
248
|
+
},
|
249
|
+
errors: {
|
252
250
|
address: {
|
253
|
-
line_one: 'Line',
|
254
251
|
post_code: {
|
255
|
-
|
256
|
-
|
257
|
-
county: {}
|
258
|
-
}
|
259
|
-
},
|
260
|
-
errors: {
|
261
|
-
address: {
|
262
|
-
post_code: {
|
263
|
-
county: {
|
264
|
-
code: ['is required']
|
265
|
-
}
|
252
|
+
county: {
|
253
|
+
code: ['is required']
|
266
254
|
}
|
267
255
|
}
|
268
256
|
}
|
269
|
-
}
|
257
|
+
}
|
270
258
|
|
271
259
|
end
|
272
260
|
|
@@ -279,12 +267,10 @@ describe Compel do
|
|
279
267
|
|
280
268
|
expect(result.valid?).to be false
|
281
269
|
expect(result.value).to eq \
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
}
|
287
|
-
})
|
270
|
+
address: nil,
|
271
|
+
errors: {
|
272
|
+
address: ['is required']
|
273
|
+
}
|
288
274
|
end
|
289
275
|
|
290
276
|
it 'should not compel for empty object' do
|
@@ -292,11 +278,9 @@ describe Compel do
|
|
292
278
|
|
293
279
|
expect(result.valid?).to be false
|
294
280
|
expect(result.value).to eq \
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
}
|
299
|
-
})
|
281
|
+
errors: {
|
282
|
+
address: ['is required']
|
283
|
+
}
|
300
284
|
end
|
301
285
|
|
302
286
|
end
|
@@ -516,11 +500,9 @@ describe Compel do
|
|
516
500
|
|
517
501
|
expect(make_the_call(:run!, hash)).to \
|
518
502
|
eq \
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
birth_date: DateTime.new(1988, 12, 24)
|
523
|
-
})
|
503
|
+
first_name: 'Joaquim',
|
504
|
+
last_name: 'Adráz',
|
505
|
+
birth_date: DateTime.new(1988, 12, 24)
|
524
506
|
end
|
525
507
|
|
526
508
|
it 'should raise InvalidObjectError exception for missing required key' do
|
@@ -539,7 +521,10 @@ describe Compel do
|
|
539
521
|
|
540
522
|
expect{ make_the_call(:run!, hash) }.to raise_error do |exception|
|
541
523
|
expect(exception.object).to eq \
|
542
|
-
|
524
|
+
first_name: 'Joaquim',
|
525
|
+
errors: {
|
526
|
+
last_name: ['is required']
|
527
|
+
}
|
543
528
|
end
|
544
529
|
end
|
545
530
|
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: compel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joaquim Adráz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: hashie
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '3.4'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '3.4'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rspec
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|