compel 0.3.2 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +112 -63
  3. data/compel.gemspec +1 -1
  4. data/lib/compel/builder/any.rb +13 -0
  5. data/lib/compel/builder/array.rb +1 -5
  6. data/lib/compel/builder/common.rb +17 -2
  7. data/lib/compel/builder/common_value.rb +0 -5
  8. data/lib/compel/builder/hash.rb +0 -4
  9. data/lib/compel/builder/methods.rb +5 -0
  10. data/lib/compel/builder/string.rb +1 -11
  11. data/lib/compel/coercion/coercion.rb +3 -8
  12. data/lib/compel/coercion/types/any.rb +13 -0
  13. data/lib/compel/coercion/types/json.rb +2 -0
  14. data/lib/compel/coercion/types/type.rb +5 -1
  15. data/lib/compel/errors.rb +1 -1
  16. data/lib/compel/validation/conditions/condition.rb +31 -0
  17. data/lib/compel/validation/conditions/format.rb +19 -0
  18. data/lib/compel/validation/conditions/in.rb +25 -0
  19. data/lib/compel/validation/conditions/is.rb +29 -0
  20. data/lib/compel/validation/conditions/length.rb +21 -0
  21. data/lib/compel/validation/conditions/max.rb +21 -0
  22. data/lib/compel/validation/conditions/max_length.rb +21 -0
  23. data/lib/compel/validation/conditions/min.rb +21 -0
  24. data/lib/compel/validation/conditions/min_length.rb +21 -0
  25. data/lib/compel/validation/result.rb +23 -0
  26. data/lib/compel/validation/validation.rb +62 -0
  27. data/lib/compel/validators/base.rb +4 -0
  28. data/lib/compel/validators/type_validator.rb +2 -4
  29. data/lib/compel/version.rb +1 -1
  30. data/lib/compel.rb +5 -12
  31. data/spec/compel/builder_spec.rb +72 -2
  32. data/spec/compel/coercion_spec.rb +25 -30
  33. data/spec/compel/compel_spec.rb +3 -3
  34. data/spec/compel/validation_spec.rb +65 -11
  35. metadata +16 -5
  36. data/lib/compel/exceptions/validation_error.rb +0 -7
  37. data/lib/compel/validation.rb +0 -60
@@ -0,0 +1,62 @@
1
+ require 'compel/validation/conditions/condition'
2
+ require 'compel/validation/conditions/is'
3
+ require 'compel/validation/conditions/in'
4
+ require 'compel/validation/conditions/min'
5
+ require 'compel/validation/conditions/max'
6
+ require 'compel/validation/conditions/format'
7
+ require 'compel/validation/conditions/length'
8
+ require 'compel/validation/conditions/min_length'
9
+ require 'compel/validation/conditions/max_length'
10
+
11
+ require 'compel/validation/result'
12
+
13
+ module Compel
14
+
15
+ module Validation
16
+
17
+ CONDITIONS = {
18
+ is: Validation::Is,
19
+ in: Validation::In,
20
+ min: Validation::Min,
21
+ max: Validation::Max,
22
+ range: Validation::Range,
23
+ within: Validation::Within,
24
+ format: Validation::Format,
25
+ length: Validation::Length,
26
+ min_length: Validation::MinLength,
27
+ max_length: Validation::MaxLength,
28
+ }
29
+
30
+ def validate(value, type, options)
31
+ if value.nil? && !!options[:required]
32
+ return ['is required']
33
+ end
34
+
35
+ errors = Errors.new
36
+
37
+ options.each do |option, option_value|
38
+ next unless condition_exists?(option)
39
+
40
+ result = condition_klass(option).validate(value, option_value, type: type)
41
+
42
+ unless result.valid?
43
+ errors.add :base, result.error_message
44
+ end
45
+ end
46
+
47
+ errors.to_hash[:base] || []
48
+ end
49
+
50
+ def condition_exists?(option)
51
+ CONDITIONS.keys.include?(option.to_sym)
52
+ end
53
+
54
+ def condition_klass(option)
55
+ CONDITIONS[option.to_sym]
56
+ end
57
+
58
+ extend self
59
+
60
+ end
61
+
62
+ end
@@ -27,3 +27,7 @@ module Compel
27
27
 
28
28
  end
29
29
  end
30
+
31
+ require 'compel/validators/type_validator'
32
+ require 'compel/validators/hash_validator'
33
+ require 'compel/validators/array_validator'
@@ -11,10 +11,8 @@ module Compel
11
11
  return self
12
12
  end
13
13
 
14
- options = input, schema.type, schema.options
15
-
16
14
  # coerce
17
- coercion_result = Coercion.coerce(*options)
15
+ coercion_result = Coercion.coerce(input, schema.type, schema.options)
18
16
 
19
17
  unless coercion_result.valid?
20
18
  @errors = [coercion_result.error]
@@ -24,7 +22,7 @@ module Compel
24
22
  @output = coercion_result.coerced
25
23
 
26
24
  # validate
27
- @errors = Validation.validate(*options)
25
+ @errors = Validation.validate(@output, schema.type, schema.options)
28
26
 
29
27
  # validate array values
30
28
  if schema.type == Coercion::Array && errors.empty?
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.3.2'
2
+ VERSION = '0.3.4'
3
3
  end
data/lib/compel.rb CHANGED
@@ -1,23 +1,16 @@
1
- require 'json'
2
1
  require 'hashie'
3
- require 'hashie/extensions/symbolize_keys'
4
2
 
5
- require 'compel/exceptions/invalid_object_error'
6
- require 'compel/exceptions/validation_error'
7
3
  require 'compel/exceptions/type_error'
8
-
9
- require 'compel/result'
4
+ require 'compel/exceptions/invalid_object_error'
10
5
 
11
6
  require 'compel/validators/base'
12
- require 'compel/validators/type_validator'
13
- require 'compel/validators/hash_validator'
14
- require 'compel/validators/array_validator'
15
-
16
7
  require 'compel/builder/methods'
17
8
  require 'compel/coercion/coercion'
18
- require 'compel/contract'
19
- require 'compel/validation'
9
+ require 'compel/validation/validation'
10
+
11
+ require 'compel/result'
20
12
  require 'compel/errors'
13
+ require 'compel/contract'
21
14
 
22
15
  module Compel
23
16
 
@@ -180,6 +180,32 @@ describe Compel::Builder do
180
180
 
181
181
  context 'Validate' do
182
182
 
183
+ context 'Any' do
184
+
185
+ it 'should validate with errors and any type' do
186
+ schema = Compel.hash.keys({
187
+ a: Compel.any.required
188
+ })
189
+
190
+ expect(schema.validate({ a: nil }).errors.a).to \
191
+ include('is required')
192
+ end
193
+
194
+ context '#is' do
195
+
196
+ it 'should validate without errors and any type' do
197
+ schema = Compel.hash.keys({
198
+ a: Compel.any.is(123)
199
+ })
200
+
201
+ expect(schema.validate({ a: 122 }).errors.a).to \
202
+ include('must be 123')
203
+ end
204
+
205
+ end
206
+
207
+ end
208
+
183
209
  context 'Hash' do
184
210
 
185
211
  it 'should validate Hash schema' do
@@ -236,15 +262,39 @@ describe Compel::Builder do
236
262
  include('is required')
237
263
  end
238
264
 
265
+ context '#is' do
266
+
267
+ it 'should validate with errors' do
268
+ value = { a: 1, b: 2, c: { d: 3, e: 4 }}
269
+ schema = Compel.hash.is(value)
270
+
271
+ result = schema.validate({ a: 1, b: 2, c: 3 })
272
+
273
+ expect(result.errors[:base]).to \
274
+ include("must be {\"a\"=>1, \"b\"=>2, \"c\"=>{\"d\"=>3, \"e\"=>4}}")
275
+ end
276
+
277
+ it 'should validate without errors' do
278
+ schema = Compel.hash.is({ a: 1, b: 2, c: 3 })
279
+
280
+ result = schema.validate({ 'a' => 1, 'b' => 2, 'c' => 3 })
281
+ expect(result.valid?).to be true
282
+
283
+ result = schema.validate({ :a => 1, :b => 2, :c => 3 })
284
+ expect(result.valid?).to be true
285
+ end
286
+
287
+ end
288
+
239
289
  end
240
290
 
241
291
  context 'String' do
242
292
 
243
293
  it 'should validate Type schema' do
244
294
  schema = Compel.string.format(/^\d{4}-\d{3}$/).required
245
- response = schema.validate('1234')
295
+ result = schema.validate('1234')
246
296
 
247
- expect(response.errors).to \
297
+ expect(result.errors).to \
248
298
  include("must match format ^\\d{4}-\\d{3}$")
249
299
  end
250
300
 
@@ -420,6 +470,26 @@ describe Compel::Builder do
420
470
 
421
471
  end
422
472
 
473
+ context '#is' do
474
+
475
+ it 'should validate with errors' do
476
+ value = [1, 2, 3]
477
+ schema = builder.is(value)
478
+ result = schema.validate([1, 2])
479
+
480
+ expect(result.valid?).to be false
481
+ expect(result.errors[:base]).to include("must be #{value}")
482
+ end
483
+
484
+ it 'should validate without errors' do
485
+ schema = builder.is(['a', 'b', 'c'])
486
+ result = schema.validate(['a', 'b', 'c'])
487
+
488
+ expect(result.valid?).to be true
489
+ end
490
+
491
+ end
492
+
423
493
  end
424
494
 
425
495
  context 'Hash' do
@@ -5,24 +5,22 @@ describe Compel::Coercion do
5
5
  context 'Integer' do
6
6
 
7
7
  it 'should coerce' do
8
- value = Compel::Coercion.coerce!(123, Integer)
8
+ value = Compel::Coercion.coerce!(123, Compel::Coercion::Integer)
9
9
  expect(value).to eq(123)
10
10
  end
11
11
 
12
12
  it 'should coerce 1' do
13
- value = Compel::Coercion.coerce!('123', Integer)
13
+ value = Compel::Coercion.coerce!('123', Compel::Coercion::Integer)
14
14
  expect(value).to eq(123)
15
15
  end
16
16
 
17
17
  it 'should coerce 2' do
18
- value = Compel::Coercion.coerce!(123.3, Integer)
18
+ value = Compel::Coercion.coerce!(123.3, Compel::Coercion::Integer)
19
19
  expect(value).to eq(123)
20
20
  end
21
21
 
22
22
  it 'should not coerce' do
23
- expect(Compel::Coercion.valid?('123abc', Integer)).to eq(false)
24
-
25
- expect { Compel::Coercion.coerce!('123abc', Integer) }.to \
23
+ expect { Compel::Coercion.coerce!('123abc', Compel::Coercion::Integer) }.to \
26
24
  raise_error Compel::TypeError, "'123abc' is not a valid Integer"
27
25
  end
28
26
 
@@ -31,15 +29,13 @@ describe Compel::Coercion do
31
29
  context 'Float' do
32
30
 
33
31
  it 'should coerce' do
34
- value = Compel::Coercion.coerce!('1.2', Float)
32
+ value = Compel::Coercion.coerce!('1.2', Compel::Coercion::Float)
35
33
 
36
34
  expect(value).to eq(1.2)
37
35
  end
38
36
 
39
37
  it 'should not coerce' do
40
- expect(Compel::Coercion.valid?('1.a233', Float)).to eq(false)
41
-
42
- expect { Compel::Coercion.coerce!('1.a233', Float) }.to \
38
+ expect { Compel::Coercion.coerce!('1.a233', Compel::Coercion::Float) }.to \
43
39
  raise_error Compel::TypeError, "'1.a233' is not a valid Float"
44
40
  end
45
41
 
@@ -48,7 +44,7 @@ describe Compel::Coercion do
48
44
  context 'String' do
49
45
 
50
46
  it 'should coerce' do
51
- value = Compel::Coercion.coerce!('abc', String)
47
+ value = Compel::Coercion.coerce!('abc', Compel::Coercion::String)
52
48
 
53
49
  expect(value).to eq('abc')
54
50
  end
@@ -56,9 +52,7 @@ describe Compel::Coercion do
56
52
  it 'should not coerce' do
57
53
  value = 1.2
58
54
 
59
- expect(Compel::Coercion.valid?(value, String)).to eq(false)
60
-
61
- expect { Compel::Coercion.coerce!(value, String) }.to \
55
+ expect { Compel::Coercion.coerce!(value, Compel::Coercion::String) }.to \
62
56
  raise_error Compel::TypeError, "'#{value}' is not a valid String"
63
57
  end
64
58
 
@@ -67,13 +61,13 @@ describe Compel::Coercion do
67
61
  context 'Date' do
68
62
 
69
63
  it 'should coerce with default format' do
70
- value = Compel::Coercion.coerce!('2015-12-22', Date)
64
+ value = Compel::Coercion.coerce!('2015-12-22', Compel::Coercion::Date)
71
65
 
72
66
  expect(value).to eq(Date.parse('2015-12-22'))
73
67
  end
74
68
 
75
69
  it 'should coerce with format' do
76
- value = Compel::Coercion.coerce!('22-12-2015', Date, { format: '%d-%m-%Y' })
70
+ value = Compel::Coercion.coerce!('22-12-2015', Compel::Coercion::Date, { format: '%d-%m-%Y' })
77
71
 
78
72
  expect(value).to eq(Date.strptime('22-12-2015', '%d-%m-%Y'))
79
73
  end
@@ -81,9 +75,7 @@ describe Compel::Coercion do
81
75
  it 'should not coerce' do
82
76
  value = '2015-0'
83
77
 
84
- expect(Compel::Coercion.valid?(value, Date)).to eq(false)
85
-
86
- expect { Compel::Coercion.coerce!(value, Date) }.to \
78
+ expect { Compel::Coercion.coerce!(value, Compel::Coercion::Date) }.to \
87
79
  raise_error \
88
80
  Compel::TypeError,
89
81
  "'#{value}' is not a parsable date with format: %Y-%m-%d"
@@ -93,7 +85,7 @@ describe Compel::Coercion do
93
85
  default_format = '%Y-%m-%d'
94
86
  value = '22-12-2015'
95
87
 
96
- expect { Compel::Coercion.coerce!(value, Date) }.to \
88
+ expect { Compel::Coercion.coerce!(value, Compel::Coercion::Date) }.to \
97
89
  raise_error \
98
90
  Compel::TypeError,
99
91
  "'#{value}' is not a parsable date with format: #{default_format}"
@@ -105,7 +97,8 @@ describe Compel::Coercion do
105
97
 
106
98
  it 'should coerce' do
107
99
  [DateTime, Time].each do |time_klass|
108
- value = Compel::Coercion.coerce!('2015-12-22', time_klass, format: '%Y-%m-%d')
100
+ value = Compel::Coercion.coerce! \
101
+ '2015-12-22', Compel::Coercion.const_get("#{time_klass}"), format: '%Y-%m-%d'
109
102
 
110
103
  expect(value).to be_a time_klass
111
104
  expect(value.year).to eq(2015)
@@ -117,7 +110,8 @@ describe Compel::Coercion do
117
110
 
118
111
  it 'should coerce iso8601 format' do
119
112
  [DateTime, Time].each do |time_klass|
120
- value = Compel::Coercion.coerce!('2015-12-22T09:30:10', time_klass)
113
+ value = Compel::Coercion.coerce! \
114
+ '2015-12-22T09:30:10', Compel::Coercion.const_get("#{time_klass}")
121
115
 
122
116
  expect(value).to be_a time_klass
123
117
  expect(value.year).to eq(2015)
@@ -136,7 +130,7 @@ describe Compel::Coercion do
136
130
  [DateTime, Time].each do |time_klass|
137
131
  type_down_cased = "#{time_klass}".downcase
138
132
 
139
- expect { Compel::Coercion.coerce!('22-12-2015', time_klass) }.to \
133
+ expect { Compel::Coercion.coerce!('22-12-2015', Compel::Coercion.const_get("#{time_klass}")) }.to \
140
134
  raise_error \
141
135
  Compel::TypeError,
142
136
  "'#{value}' is not a parsable #{type_down_cased} with format: #{default_format}"
@@ -152,7 +146,7 @@ describe Compel::Coercion do
152
146
  value = Compel::Coercion.coerce!({
153
147
  first_name: 'Joaquim',
154
148
  last_name: 'Adráz'
155
- }, Hash)
149
+ }, Compel::Coercion::Hash)
156
150
 
157
151
  expect(value).to eq({
158
152
  'first_name' => 'Joaquim',
@@ -164,7 +158,7 @@ describe Compel::Coercion do
164
158
  value = Compel::Coercion.coerce!({
165
159
  'first_name' => 'Joaquim',
166
160
  'last_name' => 'Adráz'
167
- }, Hash)
161
+ }, Compel::Coercion::Hash)
168
162
 
169
163
  expect(value).to eq({
170
164
  'first_name' => 'Joaquim',
@@ -176,7 +170,7 @@ describe Compel::Coercion do
176
170
  value = Compel::Coercion.coerce!(Hashie::Mash.new({
177
171
  first_name: 'Joaquim',
178
172
  last_name: 'Adráz'
179
- }), Hash)
173
+ }), Compel::Coercion::Hash)
180
174
 
181
175
  expect(value).to eq({
182
176
  'first_name' => 'Joaquim',
@@ -185,17 +179,17 @@ describe Compel::Coercion do
185
179
  end
186
180
 
187
181
  it 'should not coerce' do
188
- expect { Compel::Coercion.coerce!(123, Hash) }.to \
182
+ expect { Compel::Coercion.coerce!(123, Compel::Coercion::Hash) }.to \
189
183
  raise_error Compel::TypeError, "'123' is not a valid Hash"
190
184
  end
191
185
 
192
186
  it 'should not coerce 1' do
193
- expect { Compel::Coercion.coerce!('hash', Hash) }.to \
187
+ expect { Compel::Coercion.coerce!('hash', Compel::Coercion::Hash) }.to \
194
188
  raise_error Compel::TypeError, "'hash' is not a valid Hash"
195
189
  end
196
190
 
197
191
  it 'should not coerce 2' do
198
- expect { Compel::Coercion.coerce!(['hash'], Hash) }.to \
192
+ expect { Compel::Coercion.coerce!(['hash'], Compel::Coercion::Hash) }.to \
199
193
  raise_error Compel::TypeError, "'[\"hash\"]' is not a valid Hash"
200
194
  end
201
195
 
@@ -205,7 +199,8 @@ describe Compel::Coercion do
205
199
 
206
200
  it 'should coerce' do
207
201
  value = Compel::Coercion.coerce! \
208
- "{\"first_name\":\"Joaquim\",\"last_name\":\"Adráz\"}", JSON
202
+ "{\"first_name\":\"Joaquim\",\"last_name\":\"Adráz\"}",
203
+ Compel::Coercion::JSON
209
204
 
210
205
  expect(value).to eq({
211
206
  'first_name' => 'Joaquim',
@@ -344,14 +344,14 @@ describe Compel do
344
344
  context 'is option' do
345
345
 
346
346
  it 'should compel' do
347
- expect(Compel.run?(1, Compel.boolean.is(1))).to be true
347
+ expect(Compel.run?(1, Compel.boolean.is(true))).to be true
348
348
  end
349
349
 
350
350
  it 'should not compel' do
351
- result = Compel.run(0, Compel.boolean.is(1))
351
+ result = Compel.run(0, Compel.boolean.is(true))
352
352
 
353
353
  expect(result.valid?).to be false
354
- expect(result.errors).to include('must be 1')
354
+ expect(result.errors).to include('must be true')
355
355
  end
356
356
 
357
357
  end
@@ -104,32 +104,80 @@ describe Compel::Validation do
104
104
 
105
105
  context 'min' do
106
106
 
107
+ it 'should validate without errors for Integer' do
108
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, min: 1)
109
+
110
+ expect(errors.empty?).to be true
111
+ end
112
+
113
+ it 'should validate without errors for String' do
114
+ errors = Compel::Validation.validate('b', Compel::Coercion::String, min: 'a')
115
+
116
+ expect(errors.empty?).to be true
117
+ end
118
+
107
119
  it 'should validate with errors' do
108
120
  errors = Compel::Validation.validate(1, Compel::Coercion::Integer, min: 3)
109
121
 
110
122
  expect(errors).to include('cannot be less than 3')
111
123
  end
112
124
 
125
+ it 'should validate with errors for String' do
126
+ errors = Compel::Validation.validate('a', Compel::Coercion::String, min: 'b')
127
+
128
+ expect(errors).to include('cannot be less than b')
129
+ end
130
+
131
+ it 'should validate with errors for Float' do
132
+ errors = Compel::Validation.validate(1.54, Compel::Coercion::Float, min: 1.55)
133
+
134
+ expect(errors).to include('cannot be less than 1.55')
135
+ end
136
+
113
137
  end
114
138
 
115
139
  context 'max' do
116
140
 
141
+ it 'should validate without errors' do
142
+ errors = Compel::Validation.validate(5, Compel::Coercion::Integer, min: 5)
143
+
144
+ expect(errors.empty?).to be true
145
+ end
146
+
117
147
  it 'should validate with errors' do
118
148
  errors = Compel::Validation.validate(3, Compel::Coercion::Integer, max: 2)
119
149
 
120
150
  expect(errors).to include('cannot be greater than 2')
121
151
  end
122
152
 
123
- end
153
+ it 'should validate without errors for Integer' do
154
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, max: 3)
124
155
 
125
- context 'min_length' do
156
+ expect(errors.empty?).to be true
157
+ end
126
158
 
127
- it 'should validate with errors' do
128
- errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: 2)
159
+ it 'should validate without errors for String' do
160
+ errors = Compel::Validation.validate('b', Compel::Coercion::String, max: 'd')
161
+
162
+ expect(errors.empty?).to be true
163
+ end
129
164
 
130
- expect(errors).to include('must be a string if using the min_length validation')
165
+ it 'should validate with errors for String' do
166
+ errors = Compel::Validation.validate('c', Compel::Coercion::String, max: 'b')
167
+
168
+ expect(errors).to include('cannot be greater than b')
169
+ end
170
+
171
+ it 'should validate with errors for Float' do
172
+ errors = Compel::Validation.validate(1.56, Compel::Coercion::Float, max: 1.55)
173
+
174
+ expect(errors).to include('cannot be greater than 1.55')
131
175
  end
132
176
 
177
+ end
178
+
179
+ context 'min_length' do
180
+
133
181
  it 'should validate with errors 1' do
134
182
  errors = Compel::Validation.validate('a', Compel::Coercion::String, min_length: 2)
135
183
 
@@ -142,15 +190,15 @@ describe Compel::Validation do
142
190
  expect(errors.empty?).to eq(true)
143
191
  end
144
192
 
145
- end
193
+ it 'should validate without errors 1' do
194
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: 2)
146
195
 
147
- context 'max_length' do
196
+ expect(errors).to include('cannot have length less than 2')
197
+ end
148
198
 
149
- it 'should validate with errors' do
150
- errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: 2)
199
+ end
151
200
 
152
- expect(errors).to include('must be a string if using the max_length validation')
153
- end
201
+ context 'max_length' do
154
202
 
155
203
  it 'should validate with errors 1' do
156
204
  errors = Compel::Validation.validate('abcdef', Compel::Coercion::String, max_length: 5)
@@ -164,6 +212,12 @@ describe Compel::Validation do
164
212
  expect(errors.empty?).to eq(true)
165
213
  end
166
214
 
215
+ it 'should validate without errors 1' do
216
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: 2)
217
+
218
+ expect(errors.empty?).to eq(true)
219
+ end
220
+
167
221
  end
168
222
 
169
223
  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.2
4
+ version: 0.3.4
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: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -82,6 +82,7 @@ files:
82
82
  - Rakefile
83
83
  - compel.gemspec
84
84
  - lib/compel.rb
85
+ - lib/compel/builder/any.rb
85
86
  - lib/compel/builder/array.rb
86
87
  - lib/compel/builder/boolean.rb
87
88
  - lib/compel/builder/common.rb
@@ -99,6 +100,7 @@ files:
99
100
  - lib/compel/coercion/coercion.rb
100
101
  - lib/compel/coercion/nil_result.rb
101
102
  - lib/compel/coercion/result.rb
103
+ - lib/compel/coercion/types/any.rb
102
104
  - lib/compel/coercion/types/array.rb
103
105
  - lib/compel/coercion/types/boolean.rb
104
106
  - lib/compel/coercion/types/date.rb
@@ -115,9 +117,18 @@ files:
115
117
  - lib/compel/errors.rb
116
118
  - lib/compel/exceptions/invalid_object_error.rb
117
119
  - lib/compel/exceptions/type_error.rb
118
- - lib/compel/exceptions/validation_error.rb
119
120
  - lib/compel/result.rb
120
- - lib/compel/validation.rb
121
+ - lib/compel/validation/conditions/condition.rb
122
+ - lib/compel/validation/conditions/format.rb
123
+ - lib/compel/validation/conditions/in.rb
124
+ - lib/compel/validation/conditions/is.rb
125
+ - lib/compel/validation/conditions/length.rb
126
+ - lib/compel/validation/conditions/max.rb
127
+ - lib/compel/validation/conditions/max_length.rb
128
+ - lib/compel/validation/conditions/min.rb
129
+ - lib/compel/validation/conditions/min_length.rb
130
+ - lib/compel/validation/result.rb
131
+ - lib/compel/validation/validation.rb
121
132
  - lib/compel/validators/array_validator.rb
122
133
  - lib/compel/validators/base.rb
123
134
  - lib/compel/validators/hash_validator.rb
@@ -153,7 +164,7 @@ rubyforge_project:
153
164
  rubygems_version: 2.5.1
154
165
  signing_key:
155
166
  specification_version: 4
156
- summary: Ruby Hash Coercion and Validation
167
+ summary: Ruby Object Coercion and Validation
157
168
  test_files:
158
169
  - spec/compel/builder_spec.rb
159
170
  - spec/compel/coercion_spec.rb
@@ -1,7 +0,0 @@
1
- module Compel
2
-
3
- class ValidatorError < StandardError
4
-
5
- end
6
-
7
- end
@@ -1,60 +0,0 @@
1
- module Compel
2
-
3
- module Validation
4
-
5
- def validate(value, type, options)
6
- # if a value is required and not given,
7
- # don't do any other validation
8
- if !!options[:required] && value.nil?
9
- return ['is required']
10
- end
11
-
12
- errors = []
13
-
14
- options.each do |option, option_value|
15
- # most of this code snippet is from sinatra-param gem
16
- # https://github.com/mattt/sinatra-param
17
- # by Mattt Thompson (@mattt)
18
- case option.to_sym
19
- when :format
20
- if type == Coercion::String && !value.nil?
21
- errors << "must match format #{option_value.source}" unless value =~ option_value
22
- end
23
- when :is
24
- errors << "must be #{option_value}" unless value === option_value
25
- when :in, :within, :range
26
- errors << "must be within #{option_value}" unless value.nil? || case option_value
27
- when Range
28
- option_value.include?(value)
29
- else
30
- Array(option_value).include?(value)
31
- end
32
- when :min
33
- errors << "cannot be less than #{option_value}" unless value.nil? || option_value <= value
34
- when :max
35
- errors << "cannot be greater than #{option_value}" unless value.nil? || option_value >= value
36
- when :length
37
- errors << "cannot have length different than #{option_value}" unless value.nil? || option_value == "#{value}".length
38
- when :min_length
39
- unless value.kind_of?(String)
40
- errors << 'must be a string if using the min_length validation'
41
- else
42
- errors << "cannot have length less than #{option_value}" unless value.nil? || option_value <= value.length
43
- end
44
- when :max_length
45
- unless value.kind_of?(String)
46
- errors << 'must be a string if using the max_length validation'
47
- else
48
- errors << "cannot have length greater than #{option_value}" unless value.nil? || option_value >= value.length
49
- end
50
- end
51
- end
52
-
53
- errors
54
- end
55
-
56
- extend self
57
-
58
- end
59
-
60
- end