compel 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bef1b2e1274aa8234be2702abb7267fb7663de84
4
- data.tar.gz: 2a5d3146d9b59f2aaf54c8e552b721434774011c
3
+ metadata.gz: 6e6cfd90b9d392c7d5bf4f81be726f0e5b0f1444
4
+ data.tar.gz: 9ad55dcf253ce805dc407889fcc94fe659106329
5
5
  SHA512:
6
- metadata.gz: 6b29767783dd35c1c9783499ae9f9c26dc773f7e09d37fba395ebe1d83d5fa167f496d39de056a45d7217578a973d86101a63580415cc716dcbeafb1b9af9d94
7
- data.tar.gz: 645a9ea31ccce72d524e38f98567d3153630af64475b0c0ad506fe85b7977bc93ce5bd76cd452e6276aecba09316f019736e852f3defb1fc9a80e5e34459e936
6
+ metadata.gz: 35e2a1ff640dd63f43b9826a79fa4949b3a15da09e3c3eb517b6ac64f0763ed2cc2b3c2e8b04baec04ea6e9906edbdc3af84002fef4dff7210a01433b5bea70c
7
+ data.tar.gz: d4e7c4d9443609d922ea3adb3d664d21b3c49b5b8b278d3a46786ff33acfca330afde0f6f6746b41b58f9c9b4f7f39ef2f0518542c1b0516f7b8a461827475f3
data/README.md CHANGED
@@ -77,6 +77,7 @@ Method | Behaviour
77
77
 
78
78
  #### Compel#any
79
79
  `Any` referes to any type that is available to coerce with Compel.
80
+ Methods `length`, `min_length` and `max_length` turn the object to validate into a `string` to compare the length.
80
81
 
81
82
  **Methods**:
82
83
  - `is(``value``)`
@@ -258,7 +259,7 @@ end
258
259
 
259
260
  Add this line to your application's Gemfile:
260
261
 
261
- gem 'compel', '~> 0.3.4'
262
+ gem 'compel', '~> 0.3.5'
262
263
 
263
264
  And then execute:
264
265
 
@@ -9,11 +9,29 @@ module Compel
9
9
  options[:keys] = {}
10
10
  end
11
11
 
12
- def keys(hash)
13
- options[:keys] = hash
12
+ def keys(object)
13
+ options[:keys] = coerce_keys_schemas(object)
14
14
  self
15
15
  end
16
16
 
17
+ private
18
+
19
+ def coerce_keys_schemas(object)
20
+ begin
21
+ fail if object.nil?
22
+
23
+ Coercion.coerce!(object, Coercion::Hash)
24
+ rescue
25
+ raise TypeError, 'Builder::Hash keys must be an Hash'
26
+ end
27
+
28
+ unless object.values.all?{|value| value.is_a?(Builder::Schema) }
29
+ raise TypeError, 'All Builder::Hash keys must be a valid Schema'
30
+ end
31
+
32
+ object
33
+ end
34
+
17
35
  end
18
36
 
19
37
  end
@@ -6,7 +6,7 @@ module Compel
6
6
  attr_accessor :value,
7
7
  :options
8
8
 
9
- def self.coerce(value, options)
9
+ def self.coerce(value, options = {})
10
10
  new(value, options).coerce
11
11
  end
12
12
 
@@ -91,7 +91,10 @@ module Compel
91
91
  next
92
92
  end
93
93
 
94
- if input[key].is_a?(Hash)
94
+ if schemas[key].type == Coercion::Hash &&
95
+ (schemas[key].required? ||
96
+ !input[key].nil?)
97
+
95
98
  hash_validator = HashValidator.validate(input[key], schemas[key])
96
99
 
97
100
  errors.add(key, hash_validator.errors)
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.3.4'
2
+ VERSION = '0.3.6'
3
3
  end
@@ -13,32 +13,6 @@ describe Compel::Builder do
13
13
  expect(builder.default_value).to be nil
14
14
  end
15
15
 
16
- it 'should build Schema' do
17
- schema = Compel.hash.keys({
18
- a: Compel.float,
19
- b: Compel.string,
20
- c: Compel.hash.keys({
21
- cc: Compel.integer.length(1)
22
- }),
23
- d: Compel.json,
24
- e: Compel.time,
25
- f: Compel.datetime,
26
- g: Compel.date,
27
- h: Compel.integer
28
- })
29
-
30
- keys_schemas = schema.options[:keys]
31
-
32
- expect(keys_schemas.a.type).to be Compel::Coercion::Float
33
- expect(keys_schemas.b.type).to be Compel::Coercion::String
34
- expect(keys_schemas.c.type).to be Compel::Coercion::Hash
35
- expect(keys_schemas.d.type).to be Compel::Coercion::JSON
36
- expect(keys_schemas.e.type).to be Compel::Coercion::Time
37
- expect(keys_schemas.f.type).to be Compel::Coercion::DateTime
38
- expect(keys_schemas.g.type).to be Compel::Coercion::Date
39
- expect(keys_schemas.h.type).to be Compel::Coercion::Integer
40
- end
41
-
42
16
  context 'Builder::CommonValue' do
43
17
 
44
18
  context '#in, #range, #min, #max' do
@@ -176,30 +150,208 @@ describe Compel::Builder do
176
150
 
177
151
  end
178
152
 
153
+ context 'Hash' do
154
+
155
+ it 'should build Schema' do
156
+ schema = Compel.hash.keys({
157
+ a: Compel.float,
158
+ b: Compel.string,
159
+ c: Compel.hash.keys({
160
+ cc: Compel.integer.length(1)
161
+ }),
162
+ d: Compel.json,
163
+ e: Compel.time,
164
+ f: Compel.datetime,
165
+ g: Compel.date,
166
+ h: Compel.integer
167
+ })
168
+
169
+ keys_schemas = schema.options[:keys]
170
+
171
+ expect(keys_schemas.a.type).to be Compel::Coercion::Float
172
+ expect(keys_schemas.b.type).to be Compel::Coercion::String
173
+ expect(keys_schemas.c.type).to be Compel::Coercion::Hash
174
+ expect(keys_schemas.d.type).to be Compel::Coercion::JSON
175
+ expect(keys_schemas.e.type).to be Compel::Coercion::Time
176
+ expect(keys_schemas.f.type).to be Compel::Coercion::DateTime
177
+ expect(keys_schemas.g.type).to be Compel::Coercion::Date
178
+ expect(keys_schemas.h.type).to be Compel::Coercion::Integer
179
+ end
180
+
181
+ it 'should raise error for invalid #keys' do
182
+ expect{ Compel.hash.keys(nil) }.to \
183
+ raise_error(Compel::TypeError, 'Builder::Hash keys must be an Hash')
184
+ end
185
+
186
+ it 'should raise error for invalid #keys 1' do
187
+ expect{ Compel.hash.keys(1) }.to \
188
+ raise_error(Compel::TypeError, 'Builder::Hash keys must be an Hash')
189
+ end
190
+
191
+ it 'should raise error for invalid #keys Schema' do
192
+ expect{ Compel.hash.keys({ a: 1 }) }.to \
193
+ raise_error(Compel::TypeError, 'All Builder::Hash keys must be a valid Schema')
194
+ end
195
+
196
+ end
197
+
179
198
  end
180
199
 
181
200
  context 'Validate' do
182
201
 
183
202
  context 'Any' do
184
203
 
185
- it 'should validate with errors and any type' do
186
- schema = Compel.hash.keys({
187
- a: Compel.any.required
188
- })
204
+ context '#required' do
205
+
206
+ context 'invalid' do
207
+
208
+ it 'should validate a nil object' do
209
+ schema = Compel.any.required
210
+
211
+ expect(schema.validate(nil).errors).to \
212
+ include('is required')
213
+ end
214
+
215
+ end
216
+
217
+ context 'valid' do
218
+
219
+ it 'should validate nested hash object' do
220
+ schema = Compel.hash.keys({
221
+ a: Compel.any.required
222
+ });
223
+
224
+ result = schema.validate(a: { b: 1 })
225
+
226
+ expect(result.valid?).to be true
227
+ end
228
+
229
+ it 'should validate nested hash object 1' do
230
+ schema = Compel.hash.keys({
231
+ a: Compel.any.required
232
+ });
233
+
234
+ result = schema.validate(a: [])
235
+
236
+ expect(result.valid?).to be true
237
+ end
238
+
239
+ it 'should validate nested hash object 2' do
240
+ schema = Compel.hash.keys({
241
+ a: Compel.any.required
242
+ });
243
+
244
+ result = schema.validate(a: 1)
245
+
246
+ expect(result.valid?).to be true
247
+ end
248
+
249
+ it 'should validate an array object' do
250
+ schema = Compel.any.required
251
+
252
+ result = schema.validate([1, 2])
253
+
254
+ expect(result.valid?).to be true
255
+ end
256
+
257
+ it 'should validate an array object 1' do
258
+ schema = Compel.any.required
259
+
260
+ result = schema.validate([])
261
+
262
+ expect(result.valid?).to be true
263
+ end
264
+
265
+ it 'should validate a string object' do
266
+ schema = Compel.any.required
267
+
268
+ result = schema.validate('test')
269
+
270
+ expect(result.valid?).to be true
271
+ end
272
+
273
+ end
189
274
 
190
- expect(schema.validate({ a: nil }).errors.a).to \
191
- include('is required')
192
275
  end
193
276
 
194
277
  context '#is' do
195
278
 
196
- it 'should validate without errors and any type' do
197
- schema = Compel.hash.keys({
198
- a: Compel.any.is(123)
199
- })
279
+ context 'invalid' do
280
+
281
+ it 'should validate an integer' do
282
+ schema = Compel.any.is(123)
283
+
284
+ expect(schema.validate(122).errors).to \
285
+ include('must be 123')
286
+
287
+ expect(schema.validate('onetwothree').errors).to \
288
+ include('must be 123')
289
+ end
290
+
291
+ it 'should validate an array' do
292
+ schema = Compel.any.is([1, 2, 3])
293
+
294
+ expect(schema.validate([1]).errors).to \
295
+ include('must be [1, 2, 3]')
296
+
297
+ expect(schema.validate([]).errors).to \
298
+ include('must be [1, 2, 3]')
299
+ end
300
+
301
+ end
302
+
303
+ context 'valid' do
304
+
305
+ it 'should validate an array' do
306
+ schema = Compel.any.is([1, 2, 3])
307
+
308
+ result = schema.validate([1, 2, 3])
309
+
310
+ expect(result.valid?).to be true
311
+ end
312
+
313
+ end
314
+
315
+ end
316
+
317
+ context '#length' do
318
+
319
+ context 'invalid' do
320
+
321
+ it 'should not validate an instance object' do
322
+ schema = Compel.any.length(1)
323
+
324
+ expect(schema.validate(OpenStruct).errors).to \
325
+ include('cannot have length different than 1')
326
+ end
327
+
328
+ it 'should not validate a constant object' do
329
+ schema = Compel.any.length(1)
330
+
331
+ expect(schema.validate(OpenStruct).errors).to \
332
+ include('cannot have length different than 1')
333
+ end
334
+
335
+ end
336
+
337
+ context 'valid' do
338
+
339
+ it 'should validate a constant object' do
340
+ schema = Compel.any.length(10)
341
+
342
+ result = schema.validate(OpenStruct)
343
+
344
+ expect(result.valid?).to be true
345
+ end
346
+
347
+ it 'should validate a constant object' do
348
+ schema = Compel.any.length(10)
349
+
350
+ result = schema.validate(OpenStruct)
351
+
352
+ expect(result.valid?).to be true
353
+ end
200
354
 
201
- expect(schema.validate({ a: 122 }).errors.a).to \
202
- include('must be 123')
203
355
  end
204
356
 
205
357
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.6
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-01-07 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie