compel 0.2.0 → 0.3.1

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +88 -20
  3. data/lib/compel/builder/array.rb +31 -0
  4. data/lib/compel/builder/boolean.rb +7 -0
  5. data/lib/compel/builder/common.rb +2 -2
  6. data/lib/compel/builder/common_value.rb +4 -4
  7. data/lib/compel/builder/hash.rb +3 -1
  8. data/lib/compel/builder/methods.rb +5 -0
  9. data/lib/compel/builder/schema.rb +4 -0
  10. data/lib/compel/coercion/coercion.rb +47 -0
  11. data/lib/compel/coercion/nil_result.rb +13 -0
  12. data/lib/compel/coercion/result.rb +37 -0
  13. data/lib/compel/coercion/types/array.rb +15 -0
  14. data/lib/compel/coercion/{boolean.rb → types/boolean.rb} +1 -3
  15. data/lib/compel/coercion/types/date.rb +36 -0
  16. data/lib/compel/coercion/types/datetime.rb +36 -0
  17. data/lib/compel/coercion/{float.rb → types/float.rb} +2 -2
  18. data/lib/compel/coercion/{hash.rb → types/hash.rb} +2 -2
  19. data/lib/compel/coercion/{integer.rb → types/integer.rb} +2 -2
  20. data/lib/compel/coercion/{json.rb → types/json.rb} +2 -2
  21. data/lib/compel/coercion/{regexp.rb → types/regexp.rb} +2 -4
  22. data/lib/compel/coercion/{string.rb → types/string.rb} +3 -5
  23. data/lib/compel/coercion/types/time.rb +36 -0
  24. data/lib/compel/coercion/types/type.rb +29 -0
  25. data/lib/compel/contract.rb +14 -42
  26. data/lib/compel/errors.rb +13 -17
  27. data/lib/compel/exceptions/invalid_object_error.rb +9 -0
  28. data/lib/compel/result.rb +30 -0
  29. data/lib/compel/validation.rb +6 -4
  30. data/lib/compel/validators/array_validator.rb +107 -0
  31. data/lib/compel/validators/base.rb +11 -1
  32. data/lib/compel/validators/hash_validator.rb +77 -19
  33. data/lib/compel/validators/type_validator.rb +42 -11
  34. data/lib/compel/version.rb +1 -1
  35. data/lib/compel.rb +6 -15
  36. data/spec/compel/builder_spec.rb +354 -144
  37. data/spec/compel/coercion_spec.rb +16 -1
  38. data/spec/compel/compel_spec.rb +315 -302
  39. data/spec/compel/validation_spec.rb +97 -115
  40. data/spec/support/sinatra_app.rb +2 -2
  41. metadata +21 -15
  42. data/lib/compel/coercion/date.rb +0 -30
  43. data/lib/compel/coercion/datetime.rb +0 -30
  44. data/lib/compel/coercion/time.rb +0 -30
  45. data/lib/compel/coercion/type.rb +0 -17
  46. data/lib/compel/coercion.rb +0 -31
  47. data/lib/compel/exceptions/invalid_hash_error.rb +0 -9
@@ -4,8 +4,8 @@ describe Compel do
4
4
 
5
5
  context 'User validation example' do
6
6
 
7
- def make_the_call(method, hash)
8
- schema = Compel.hash.keys({
7
+ def user_schema
8
+ Compel.hash.keys({
9
9
  user: Compel.hash.keys({
10
10
  first_name: Compel.string.required,
11
11
  last_name: Compel.string.required,
@@ -16,98 +16,124 @@ describe Compel do
16
16
  admin: Compel.boolean.required
17
17
  })
18
18
  }).required
19
- })
20
-
21
- Compel.send(method, hash, schema)
19
+ }).required
22
20
  end
23
21
 
24
- it 'should compel returning coerced values' do
25
- hash = {
26
- user: {
27
- first_name: 'Joaquim',
28
- last_name: 'Adráz',
29
- birth_date: '1989-08-06T09:00:00',
30
- age: '26',
31
- admin: 'f',
32
- blog_role: {
33
- admin: '0'
34
- }
35
- }
36
- }
22
+ context 'valid' do
37
23
 
38
- expect(make_the_call(:run, hash)).to eq \
39
- Hashie::Mash.new({
24
+ it 'return coerced values' do
25
+ object = {
40
26
  user: {
41
27
  first_name: 'Joaquim',
42
28
  last_name: 'Adráz',
43
- birth_date: DateTime.parse('1989-08-06T09:00:00'),
44
- age: 26,
45
- admin: false,
29
+ birth_date: '1989-08-06T09:00:00',
30
+ age: '26',
31
+ admin: 'f',
46
32
  blog_role: {
47
- admin: false
33
+ admin: '0'
48
34
  }
49
35
  }
50
- })
51
- end
52
-
53
- it 'should not compel and leave other hash untouched' do
54
- hash = {
55
- other_param: 1,
56
- user: {
57
- first_name: 'Joaquim'
58
36
  }
59
- }
60
37
 
61
- expect(make_the_call(:run, hash)).to eq \
62
- Hashie::Mash.new({
63
- other_param: 1,
64
- user: {
65
- first_name: 'Joaquim',
66
- },
67
- errors: {
38
+ result = Compel.run(object, user_schema)
39
+
40
+ expect(result.valid?).to be true
41
+ expect(result.value).to eq \
42
+ Hashie::Mash.new({
68
43
  user: {
69
- last_name: ['is required']
44
+ first_name: 'Joaquim',
45
+ last_name: 'Adráz',
46
+ birth_date: DateTime.parse('1989-08-06T09:00:00'),
47
+ age: 26,
48
+ admin: false,
49
+ blog_role: {
50
+ admin: false
51
+ }
70
52
  }
71
- }
72
- })
73
- end
53
+ })
54
+ end
74
55
 
75
- it 'should not compel for invalid hash' do
76
- expect{ make_the_call(:run, 1) }.to \
77
- raise_error Compel::TypeError, 'must be an Hash'
78
56
  end
79
57
 
80
- it 'should not compel for invalid hash 1' do
81
- expect{ make_the_call(:run, nil) }.to \
82
- raise_error Compel::TypeError, 'must be an Hash'
83
- end
58
+ context 'invalid' do
84
59
 
85
- it 'should not compel' do
86
- hash = {
87
- user: {
88
- first_name: 'Joaquim'
60
+ it 'should not compel and leave other keys untouched' do
61
+ object = {
62
+ other_param: 1,
63
+ user: {
64
+ first_name: 'Joaquim'
65
+ }
89
66
  }
90
- }
91
67
 
92
- expect(make_the_call(:run, hash)).to eq \
93
- Hashie::Mash.new({
94
- user:{
95
- first_name: 'Joaquim',
96
- },
97
- errors: {
68
+ result = Compel.run(object, user_schema)
69
+
70
+ expect(result.valid?).to be false
71
+ expect(result.value).to eq \
72
+ Hashie::Mash.new({
73
+ other_param: 1,
98
74
  user: {
99
- last_name: ['is required']
75
+ first_name: 'Joaquim',
76
+ },
77
+ errors: {
78
+ user: {
79
+ last_name: ['is required']
80
+ }
100
81
  }
82
+ })
83
+ end
84
+
85
+ it 'should not compel for invalid hash' do
86
+ result = Compel.run(1, user_schema)
87
+
88
+ expect(result.valid?).to be false
89
+ expect(result.errors[:base]).to include("'1' is not a valid Hash")
90
+ end
91
+
92
+ it 'should not compel for missing hash' do
93
+ result = Compel.run(nil, user_schema)
94
+
95
+ expect(result.valid?).to be false
96
+ expect(result.errors[:base]).to include('is required')
97
+ end
98
+
99
+ it 'should not compel for empty hash' do
100
+ result = Compel.run({}, user_schema)
101
+
102
+ expect(result.valid?).to be false
103
+ expect(result.errors[:user]).to include('is required')
104
+ end
105
+
106
+ it 'should not compel for missing required key' do
107
+ object = {
108
+ user: {
109
+ first_name: 'Joaquim'
101
110
  }
102
- })
111
+ }
112
+
113
+ result = Compel.run(object, user_schema)
114
+
115
+ expect(result.valid?).to be false
116
+ expect(result.value).to eq \
117
+ Hashie::Mash.new({
118
+ user:{
119
+ first_name: 'Joaquim',
120
+ },
121
+ errors: {
122
+ user: {
123
+ last_name: ['is required']
124
+ }
125
+ }
126
+ })
127
+ end
128
+
103
129
  end
104
130
 
105
131
  end
106
132
 
107
133
  context 'Address validation example' do
108
134
 
109
- def make_the_call(method, hash)
110
- schema = Compel.hash.keys({
135
+ def address_schema
136
+ Compel.hash.keys({
111
137
  address: Compel.hash.keys({
112
138
  line_one: Compel.string.required,
113
139
  line_two: Compel.string,
@@ -121,199 +147,196 @@ describe Compel do
121
147
  }).required
122
148
  }).required
123
149
  })
124
-
125
- Compel.send(method, hash, schema)
126
150
  end
127
151
 
128
- it 'should run?' do
129
- hash = {
130
- address: {
131
- line_one: 'Lisbon',
132
- line_two: 'Portugal',
133
- post_code: {
134
- prefix: 1100,
135
- suffix: 100
152
+ context 'valid' do
153
+
154
+ it 'should compel with #run?' do
155
+ object = {
156
+ address: {
157
+ line_one: 'Lisbon',
158
+ line_two: 'Portugal',
159
+ post_code: {
160
+ prefix: 1100,
161
+ suffix: 100
162
+ }
136
163
  }
137
164
  }
138
- }
139
165
 
140
- expect(make_the_call(:run?, hash)).to eq(true)
166
+ expect(Compel.run?(object, address_schema)).to eq(true)
167
+ end
168
+
141
169
  end
142
170
 
143
- it 'should not compel' do
144
- hash = {
145
- address: {
146
- line_two: 'Portugal'
147
- }
148
- }
171
+ context 'invalid' do
149
172
 
150
- expect(make_the_call(:run, hash)).to eq \
151
- Hashie::Mash.new({
173
+ it 'should not compel for missing required keys' do
174
+ object = {
152
175
  address: {
153
176
  line_two: 'Portugal'
154
- },
155
- errors: {
156
- address: {
157
- line_one: ['is required'],
158
- post_code: ['is required']
159
- }
160
177
  }
161
- })
162
- end
178
+ }
163
179
 
164
- it 'should not compel 1' do
165
- hash = {
166
- address: {
167
- line_two: 'Portugal',
168
- post_code: {
169
- prefix: '1',
170
- county: {
171
- code: 'LX'
180
+ result = Compel.run(object, address_schema)
181
+
182
+ expect(result.valid?).to be false
183
+ expect(result.value).to eq \
184
+ Hashie::Mash.new({
185
+ address: {
186
+ line_two: 'Portugal'
187
+ },
188
+ errors: {
189
+ address: {
190
+ line_one: ['is required'],
191
+ post_code: ['is required']
192
+ }
172
193
  }
173
- }
174
- }
175
- }
194
+ })
195
+ end
176
196
 
177
- expect(make_the_call(:run, hash)).to eq \
178
- Hashie::Mash.new({
197
+ it 'should not compel missing key and length invalid' do
198
+ object = {
179
199
  address: {
180
200
  line_two: 'Portugal',
181
201
  post_code: {
182
- prefix: 1,
202
+ prefix: '1',
183
203
  county: {
184
204
  code: 'LX'
185
205
  }
186
206
  }
187
- },
188
- errors: {
189
- address: {
190
- line_one: ['is required'],
191
- post_code: {
192
- prefix: ['cannot have length different than 4']
193
- }
194
- }
195
- }
196
- })
197
- end
198
-
199
- it 'should not compel 2' do
200
- hash = {
201
- address: {
202
- post_code: {
203
- suffix: '1234'
204
207
  }
205
208
  }
206
- }
207
209
 
208
- expect(make_the_call(:run, hash)).to eq \
209
- Hashie::Mash.new({
210
- address: {
211
- post_code: {
212
- suffix: 1234
213
- }
214
- },
215
- errors: {
210
+ result = Compel.run(object, address_schema)
211
+
212
+ expect(result.valid?).to be false
213
+ expect(result.value).to eq \
214
+ Hashie::Mash.new({
216
215
  address: {
217
- line_one: ['is required'],
216
+ line_two: 'Portugal',
218
217
  post_code: {
219
- prefix: ['is required'],
220
- suffix: ['cannot have length different than 3']
218
+ prefix: 1,
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
+ }
221
230
  }
222
231
  }
223
- }
224
- })
225
- end
226
-
227
- it 'should not compel 3' do
228
- hash = {
229
- address: {
230
- post_code: {
231
- prefix: '1100',
232
- suffix: '100',
233
- county: {}
234
- },
235
- }
236
- }
232
+ })
233
+ end
237
234
 
238
- expect(make_the_call(:run, hash)).to eq \
239
- Hashie::Mash.new({
235
+ it 'should not compel for givin invalid optional value' do
236
+ object = {
240
237
  address: {
238
+ line_one: 'Line',
241
239
  post_code: {
242
- prefix: 1100,
243
- suffix: 100,
240
+ prefix: '1100',
241
+ suffix: '100',
244
242
  county: {}
245
- }
246
- },
247
- errors: {
243
+ },
244
+ }
245
+ }
246
+
247
+ result = Compel.run(object, address_schema)
248
+
249
+ expect(result.valid?).to be false
250
+ expect(result.value).to eq \
251
+ Hashie::Mash.new({
248
252
  address: {
249
- line_one: ['is required'],
253
+ line_one: 'Line',
250
254
  post_code: {
251
- county: {
252
- code: ['is required']
255
+ prefix: 1100,
256
+ suffix: 100,
257
+ county: {}
258
+ }
259
+ },
260
+ errors: {
261
+ address: {
262
+ post_code: {
263
+ county: {
264
+ code: ['is required']
265
+ }
253
266
  }
254
267
  }
255
268
  }
256
- }
257
- })
269
+ })
258
270
 
259
- end
271
+ end
260
272
 
261
- it 'should not compel 4' do
262
- hash = {
263
- address: nil
264
- }
273
+ it 'should not compel for missing required root key' do
274
+ object = {
275
+ address: nil
276
+ }
265
277
 
266
- expect(make_the_call(:run, hash)).to eq \
267
- Hashie::Mash.new({
268
- address: nil,
269
- errors: {
270
- address: ['is required']
271
- }
272
- })
273
- end
278
+ result = Compel.run(object, address_schema)
279
+
280
+ expect(result.valid?).to be false
281
+ expect(result.value).to eq \
282
+ Hashie::Mash.new({
283
+ address: nil,
284
+ errors: {
285
+ address: ['is required']
286
+ }
287
+ })
288
+ end
289
+
290
+ it 'should not compel for empty object' do
291
+ result = Compel.run({}, address_schema)
292
+
293
+ expect(result.valid?).to be false
294
+ expect(result.value).to eq \
295
+ Hashie::Mash.new({
296
+ errors: {
297
+ address: ['is required']
298
+ }
299
+ })
300
+ end
274
301
 
275
- it 'should not compel 5' do
276
- expect(make_the_call(:run, {})).to eq \
277
- Hashie::Mash.new({
278
- errors: {
279
- address: ['is required']
280
- }
281
- })
282
302
  end
283
303
 
284
304
  end
285
305
 
286
306
  context 'Boolean' do
287
307
 
288
- context 'required option' do
308
+ it 'it not compel for invalid boolean' do
309
+ result = Compel.run(nil, Compel.boolean.required)
289
310
 
290
- it 'should compel' do
291
- schema = Compel.hash.keys({
292
- admin: Compel.boolean.required
293
- })
311
+ expect(result.valid?).to be false
312
+ expect(result.errors).to include('is required')
313
+ end
294
314
 
295
- expect(Compel.run?({ admin: 1 }, schema)).to be true
315
+ context 'required option' do
316
+
317
+ it 'should compel with valid option' do
318
+ expect(Compel.run?(1, Compel.boolean.required)).to be true
296
319
  end
297
320
 
298
- it 'should not compel' do
321
+ it 'should not compel for missing required boolean' do
299
322
  schema = Compel.hash.keys({
300
323
  admin: Compel.boolean.required
301
324
  })
302
325
 
303
- expect(Compel.run({ admin: nil }, schema).errors[:admin]).to \
304
- include('is required')
326
+ result = Compel.run({ admin: nil }, schema)
327
+
328
+ expect(result.valid?).to be false
329
+ expect(result.errors[:admin]).to include('is required')
305
330
  end
306
331
 
307
332
  end
308
333
 
309
334
  context 'default option' do
310
335
 
311
- it 'should compel' do
312
- schema = Compel.hash.keys({
313
- admin: Compel.boolean.default(false)
314
- })
336
+ it 'should compel with default option set' do
337
+ result = Compel.run(nil, Compel.boolean.default(false))
315
338
 
316
- expect(Compel.run({ admin: nil }, schema).admin).to be false
339
+ expect(result.value).to be false
317
340
  end
318
341
 
319
342
  end
@@ -321,20 +344,14 @@ describe Compel do
321
344
  context 'is option' do
322
345
 
323
346
  it 'should compel' do
324
- schema = Compel.hash.keys({
325
- admin: Compel.boolean.is(1)
326
- })
327
-
328
- expect(Compel.run?({ admin: 1 }, schema)).to be true
347
+ expect(Compel.run?(1, Compel.boolean.is(1))).to be true
329
348
  end
330
349
 
331
350
  it 'should not compel' do
332
- schema = Compel.hash.keys({
333
- admin: Compel.boolean.is(1)
334
- })
351
+ result = Compel.run(0, Compel.boolean.is(1))
335
352
 
336
- expect(Compel.run({ admin: 0 }, schema).errors[:admin]).to \
337
- include('must be 1')
353
+ expect(result.valid?).to be false
354
+ expect(result.errors).to include('must be 1')
338
355
  end
339
356
 
340
357
  end
@@ -346,20 +363,17 @@ describe Compel do
346
363
  context 'format option' do
347
364
 
348
365
  it 'should compel' do
349
- schema = Compel.hash.keys({
350
- post_code: Compel.string.format(/^\d{4}-\d{3}$/)
351
- })
366
+ schema = Compel.string.format(/^\d{4}-\d{3}$/)
352
367
 
353
- expect(Compel.run?({ post_code: '1100-100' }, schema)).to be true
368
+ expect(Compel.run?('1100-100', schema)).to be true
354
369
  end
355
370
 
356
371
  it 'should not compel' do
357
- schema = Compel.hash.keys({
358
- post_code: Compel.string.format(/^\d{4}-\d{3}$/)
359
- })
372
+ schema = Compel.string.format(/^\d{4}-\d{3}$/)
373
+
374
+ result = Compel.run('110-100', schema)
360
375
 
361
- expect(Compel.run({ post_code: '110-100' }, schema).errors[:post_code]).to \
362
- include('must match format ^\\d{4}-\\d{3}$')
376
+ expect(result.errors).to include('must match format ^\\d{4}-\\d{3}$')
363
377
  end
364
378
 
365
379
  end
@@ -371,39 +385,36 @@ describe Compel do
371
385
  context 'format option' do
372
386
 
373
387
  it 'should not compel' do
374
- schema = Compel.hash.keys({
375
- birth_date: Compel.time
376
- })
388
+ schema = Compel.time
389
+ result = Compel.run('1989-08-06', schema)
377
390
 
378
- expect(Compel.run({ birth_date: '1989-08-06' }, schema).errors.birth_date).to \
391
+ expect(result.valid?).to be false
392
+ expect(result.errors).to \
379
393
  include("'1989-08-06' is not a parsable time with format: %FT%T")
380
394
  end
381
395
 
382
396
  it 'should compel with format' do
383
- schema = Compel.hash.keys({
384
- birth_date: Compel.time.format('%Y-%m-%d')
385
- })
397
+ schema = Compel.time.format('%Y-%m-%d')
398
+ result = Compel.run('1989-08-06', schema)
386
399
 
387
- expect(Compel.run({ birth_date: '1989-08-06' }, schema).birth_date).to \
388
- eq(Time.new(1989, 8, 6))
400
+ expect(result.valid?).to be true
401
+ expect(result.value).to eq(Time.new(1989, 8, 6))
389
402
  end
390
403
 
391
404
  it 'should compel by default' do
392
- schema = Compel.hash.keys({
393
- birth_date: Compel.time
394
- })
405
+ schema = Compel.time
406
+ result = Compel.run('1989-08-06T09:00:00', schema)
395
407
 
396
- expect(Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema).birth_date).to \
397
- eq(Time.new(1989, 8, 6, 9))
408
+ expect(result.valid?).to be true
409
+ expect(result.value).to eq(Time.new(1989, 8, 6, 9))
398
410
  end
399
411
 
400
412
  it 'should compel with iso8601 format' do
401
- schema = Compel.hash.keys({
402
- birth_date: Compel.time.iso8601
403
- })
413
+ schema = Compel.time.iso8601
414
+ result = Compel.run('1989-08-06T09:00:00', schema)
404
415
 
405
- expect(Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema).birth_date).to \
406
- eq(Time.new(1989, 8, 6, 9))
416
+ expect(result.valid?).to be true
417
+ expect(result.value).to eq(Time.new(1989, 8, 6, 9))
407
418
  end
408
419
 
409
420
  end
@@ -421,7 +432,10 @@ describe Compel do
421
432
  birth_date: Compel.datetime
422
433
  })
423
434
 
424
- expect(Compel.run({ birth_date: '1989-08-06' }, schema).errors.birth_date).to \
435
+ result = Compel.run({ birth_date: '1989-08-06' }, schema)
436
+
437
+ expect(result.valid?).to be false
438
+ expect(result.errors[:birth_date]).to \
425
439
  include("'1989-08-06' is not a parsable datetime with format: %FT%T")
426
440
  end
427
441
 
@@ -430,8 +444,10 @@ describe Compel do
430
444
  birth_date: Compel.datetime.format('%Y-%m-%d')
431
445
  })
432
446
 
433
- expect(Compel.run({ birth_date: '1989-08-06' }, schema).birth_date).to \
434
- eq(DateTime.new(1989, 8, 6))
447
+ result = Compel.run({ birth_date: '1989-08-06' }, schema)
448
+
449
+ expect(result.valid?).to be true
450
+ expect(result.value[:birth_date]).to eq(DateTime.new(1989, 8, 6))
435
451
  end
436
452
 
437
453
  it 'should compel by default' do
@@ -439,8 +455,10 @@ describe Compel do
439
455
  birth_date: Compel.datetime
440
456
  })
441
457
 
442
- expect(Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema).birth_date).to \
443
- eq(DateTime.new(1989, 8, 6, 9))
458
+ result = Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema)
459
+
460
+ expect(result.valid?).to be true
461
+ expect(result.value[:birth_date]).to eq(DateTime.new(1989, 8, 6, 9))
444
462
  end
445
463
 
446
464
  it 'should compel with iso8601 format' do
@@ -448,8 +466,10 @@ describe Compel do
448
466
  birth_date: Compel.datetime.iso8601
449
467
  })
450
468
 
451
- expect(Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema).birth_date).to \
452
- eq(DateTime.new(1989, 8, 6, 9))
469
+ result = Compel.run({ birth_date: '1989-08-06T09:00:00' }, schema)
470
+
471
+ expect(result.valid?).to be true
472
+ expect(result.value[:birth_date]).to eq(DateTime.new(1989, 8, 6, 9))
453
473
  end
454
474
 
455
475
  end
@@ -458,87 +478,80 @@ describe Compel do
458
478
 
459
479
  context 'Compel methods' do
460
480
 
461
- def make_the_call(method, hash)
462
- schema = Compel.hash.keys({
463
- first_name: Compel.string.required,
464
- last_name: Compel.string.required,
465
- birth_date: Compel.datetime
466
- })
467
-
468
- Compel.send(method, hash, schema)
469
- end
470
-
471
481
  context '#run!' do
472
482
 
473
- it 'should compel' do
474
- hash = {
475
- first_name: 'Joaquim',
476
- last_name: 'Adráz',
477
- birth_date: DateTime.new(1988, 12, 24)
478
- }
483
+ context 'Other Values' do
479
484
 
480
- expect(make_the_call(:run!, hash)).to \
481
- eq \
482
- Hashie::Mash.new({
483
- first_name: 'Joaquim',
484
- last_name: 'Adráz',
485
- birth_date: DateTime.new(1988, 12, 24)
486
- })
487
- end
485
+ it 'should compel valid integer' do
486
+ result = Compel.run!(1, Compel.integer.required)
487
+
488
+ expect(result).to eq(1)
489
+ end
488
490
 
489
- it 'should raise InvalidHashError exception' do
490
- hash = {
491
- first_name: 'Joaquim'
492
- }
491
+ it 'should not compel for invalid integer' do
492
+ expect{ Compel.run!('abc', Compel.integer.required) }.to \
493
+ raise_error Compel::InvalidObjectError, 'object has errors'
494
+ end
493
495
 
494
- expect{ make_the_call(:run!, hash) }.to \
495
- raise_error Compel::InvalidHashError, 'hash has errors'
496
496
  end
497
497
 
498
- it 'should raise InvalidHashError exception with errors' do
499
- hash = {
500
- first_name: 'Joaquim'
501
- }
498
+ context 'User validation example' do
502
499
 
503
- expect{ make_the_call(:run!, hash) }.to raise_error do |exception|
504
- expect(exception.object).to eq \
505
- Hashie::Mash.new(first_name: 'Joaquim')
500
+ def make_the_call(method, hash)
501
+ schema = Compel.hash.keys({
502
+ first_name: Compel.string.required,
503
+ last_name: Compel.string.required,
504
+ birth_date: Compel.datetime
505
+ })
506
506
 
507
- expect(exception.errors).to eq \
508
- Hashie::Mash.new(last_name: ['is required'])
507
+ Compel.send(method, hash, schema)
509
508
  end
510
- end
511
509
 
512
- it 'should raise InvalidHashError exception for missing hash' do
513
- expect{ make_the_call(:run!, {}) }.to \
514
- raise_error Compel::InvalidHashError, 'hash has errors'
515
- end
510
+ it 'should compel' do
511
+ hash = {
512
+ first_name: 'Joaquim',
513
+ last_name: 'Adráz',
514
+ birth_date: DateTime.new(1988, 12, 24)
515
+ }
516
516
 
517
- end
517
+ expect(make_the_call(:run!, hash)).to \
518
+ eq \
519
+ Hashie::Mash.new({
520
+ first_name: 'Joaquim',
521
+ last_name: 'Adráz',
522
+ birth_date: DateTime.new(1988, 12, 24)
523
+ })
524
+ end
518
525
 
519
- context '#run?' do
526
+ it 'should raise InvalidObjectError exception for missing required key' do
527
+ hash = {
528
+ first_name: 'Joaquim'
529
+ }
520
530
 
521
- it 'should return true' do
522
- hash = {
523
- first_name: 'Joaquim',
524
- last_name: 'Adráz',
525
- birth_date: '1989-08-06T09:00:00'
526
- }
531
+ expect{ make_the_call(:run!, hash) }.to \
532
+ raise_error Compel::InvalidObjectError, 'object has errors'
533
+ end
527
534
 
528
- expect(make_the_call(:run?, hash)).to eq(true)
529
- end
535
+ it 'should raise InvalidObjectError exception with errors' do
536
+ hash = {
537
+ first_name: 'Joaquim'
538
+ }
539
+
540
+ expect{ make_the_call(:run!, hash) }.to raise_error do |exception|
541
+ expect(exception.object).to eq \
542
+ Hashie::Mash.new(first_name: 'Joaquim', errors: { last_name: ['is required'] })
543
+ end
544
+ end
530
545
 
531
- it 'should return false' do
532
- hash = {
533
- first_name: 'Joaquim'
534
- }
546
+ it 'should raise InvalidObjectError exception for missing hash' do
547
+ expect{ make_the_call(:run!, {}) }.to \
548
+ raise_error Compel::InvalidObjectError, 'object has errors'
549
+ end
535
550
 
536
- expect(make_the_call(:run?, hash)).to eq(false)
537
551
  end
538
552
 
539
553
  end
540
554
 
541
555
  end
542
556
 
543
-
544
557
  end