compel 0.4.0 → 0.4.2

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
  SHA1:
3
- metadata.gz: 032ba6894d54a63ee956025716cb973156d601b0
4
- data.tar.gz: 0121184a327f472d7750180db68411253f91d465
3
+ metadata.gz: 97cfa78c4892af6b318eb14948f7b0defccc5863
4
+ data.tar.gz: d30bc04383159e49098a7a861995ee61f1d1e7f0
5
5
  SHA512:
6
- metadata.gz: 4fb359e62420fc5a8b780297bfc05a9ffcc986034d362e4cace62626a4f8e67c0e75a38013335f8fe1eb48c93a560ba2bbc41b9ecdfd49eeaa591443fcfb1f71
7
- data.tar.gz: f933cdda27071b04278ff0812f91c7956a4a92e478c08c6912cef70878de9bddb88860e5b7f074bedcdcd3620c002c919c4b13a69ae9ce71abe2f58371d0480e
6
+ metadata.gz: 90ee036e1d29802696e42c90c2c2a8b179de2ccc609b71d597e75fce23389273ebbf39fed245e1dd6811ece2f7b7eb74e2f52faa3bee2abb51025e0640dfe988
7
+ data.tar.gz: 8848f0337a8c43b8b012e70b6c117c7352e2c5418dc7c8891063e93d6468825e4b323ccb367e9873960e7b188af4454dfde1b129978e04f18015877d3dd4e012
data/.travis.yml CHANGED
@@ -1,3 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 1.9.3
3
4
  - 2.2.3
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 Joaquim Adráz
3
+ Copyright (c) 2016 Joaquim Adráz
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -217,6 +217,27 @@ Method | Behaviour
217
217
  `#valid?` | `true` or `false`
218
218
  `#raise?` | raises a `Compel::InvalidObjectError` if invalid, otherwise returns `#value`
219
219
 
220
+ #### Custom Options
221
+
222
+ `Custom error message`
223
+
224
+ Examples:
225
+ ```ruby
226
+ schema = Compel.string.required(message: 'this is really required')
227
+
228
+ result = schema.validate(nil)
229
+
230
+ p result.errors
231
+ => ["this is really required"]
232
+
233
+ schema = Compel.string.is('Hello', message: 'give me an Hello!')
234
+
235
+ result = schema.validate(nil)
236
+
237
+ p result.errors
238
+ => ["give me an Hello!"]
239
+ ```
240
+
220
241
  ==========================
221
242
 
222
243
  ### Sinatra Integration
@@ -7,18 +7,16 @@ module Compel
7
7
  super(Coercion::Array)
8
8
  end
9
9
 
10
- def items(schema)
10
+ def items(schema, options = {})
11
11
  if !schema.is_a?(Schema)
12
12
  raise Compel::TypeError, '#items must be a valid Schema'
13
13
  end
14
14
 
15
- options[:items] = schema
16
- self
15
+ build_option :items, schema, options
17
16
  end
18
17
 
19
18
  def is(value)
20
- options[:is] = Coercion.coerce!(value, Coercion::Array)
21
- self
19
+ build_option :is, Coercion.coerce!(value, Coercion::Array)
22
20
  end
23
21
 
24
22
  end
@@ -7,11 +7,10 @@ module Compel
7
7
  super(Coercion::Boolean)
8
8
  end
9
9
 
10
- def is(value)
10
+ def is(value, options = {})
11
11
  Coercion.coerce!(value, Coercion::Boolean)
12
- options[:is] = value
13
12
 
14
- self
13
+ build_option :is, value, options
15
14
  end
16
15
 
17
16
  end
@@ -3,34 +3,28 @@ module Compel
3
3
 
4
4
  module Common
5
5
 
6
- def is(value)
7
- options[:is] = Coercion.coerce!(value, self.type)
8
- self
6
+ def is(value, options = {})
7
+ build_option :is, Coercion.coerce!(value, self.type), options
9
8
  end
10
9
 
11
- def required
12
- options[:required] = true
13
- self
10
+ def required(options = {})
11
+ build_option :required, true, options
14
12
  end
15
13
 
16
- def default(value)
17
- options[:default] = Coercion.coerce!(value, self.type)
18
- self
14
+ def default(value, options = {})
15
+ build_option :default, Coercion.coerce!(value, self.type), options
19
16
  end
20
17
 
21
- def length(value)
22
- options[:length] = Coercion.coerce!(value, Coercion::Integer)
23
- self
18
+ def length(value, options = {})
19
+ build_option :length, Coercion.coerce!(value, Coercion::Integer), options
24
20
  end
25
21
 
26
- def min_length(value)
27
- options[:min_length] = Coercion.coerce!(value, Coercion::Integer)
28
- self
22
+ def min_length(value, options = {})
23
+ build_option :min_length, Coercion.coerce!(value, Coercion::Integer), options
29
24
  end
30
25
 
31
- def max_length(value)
32
- options[:max_length] = Coercion.coerce!(value, Coercion::Integer)
33
- self
26
+ def max_length(value, options = {})
27
+ build_option :max_length, Coercion.coerce!(value, Coercion::Integer), options
34
28
  end
35
29
 
36
30
  end
@@ -3,24 +3,20 @@ module Compel
3
3
 
4
4
  module CommonValue
5
5
 
6
- def in(value)
7
- options[:in] = coerce_values_ary!(value, :in)
8
- self
6
+ def in(value, options = {})
7
+ build_option :in, coerce_values_ary!(value, :in), options
9
8
  end
10
9
 
11
- def range(value)
12
- options[:range] = coerce_values_ary!(value, :range)
13
- self
10
+ def range(value, options = {})
11
+ build_option :range, coerce_values_ary!(value, :range), options
14
12
  end
15
13
 
16
- def min(value)
17
- options[:min] = coerce_value!(value, :min)
18
- self
14
+ def min(value, options = {})
15
+ build_option :min, coerce_value!(value, :min), options
19
16
  end
20
17
 
21
- def max(value)
22
- options[:max] = coerce_value!(value, :max)
23
- self
18
+ def max(value, options = {})
19
+ build_option :max, coerce_value!(value, :max), options
24
20
  end
25
21
 
26
22
  def coerce_values_ary!(values, method)
@@ -9,14 +9,12 @@ module Compel
9
9
  super(Coercion::Date)
10
10
  end
11
11
 
12
- def format(value)
13
- options[:format] = value
14
- self
12
+ def format(value, options = {})
13
+ build_option :format, value, options
15
14
  end
16
15
 
17
- def iso8601
18
- options[:format] = '%Y-%m-%d'
19
- self
16
+ def iso8601(options = {})
17
+ build_option :format, '%Y-%m-%d', options
20
18
  end
21
19
 
22
20
  end
@@ -9,14 +9,12 @@ module Compel
9
9
  super(Coercion::DateTime)
10
10
  end
11
11
 
12
- def format(value)
13
- options[:format] = value
14
- self
12
+ def format(value, options = {})
13
+ build_option :format, value, options
15
14
  end
16
15
 
17
- def iso8601
18
- options[:format] = '%FT%T'
19
- self
16
+ def iso8601(options = {})
17
+ build_option :format, '%FT%T', options
20
18
  end
21
19
 
22
20
  end
@@ -6,12 +6,11 @@ module Compel
6
6
  def initialize
7
7
  super(Coercion::Hash)
8
8
 
9
- options[:keys] = {}
9
+ options[:keys] = { value: {} }
10
10
  end
11
11
 
12
- def keys(object)
13
- options[:keys] = coerce_keys_schemas(object)
14
- self
12
+ def keys(object, options = {})
13
+ build_option :keys, coerce_keys_schemas(object), options
15
14
  end
16
15
 
17
16
  private
@@ -8,35 +8,35 @@ module Compel
8
8
  attr_reader :type,
9
9
  :options
10
10
 
11
+ def self.human_name
12
+ "#{self.name.split('::')[1..-1].join('::')}"
13
+ end
14
+
11
15
  def initialize(type)
12
16
  @type = type
13
17
  @options = default_options
14
18
  end
15
19
 
16
20
  def required?
17
- options[:required]
21
+ options[:required][:value]
18
22
  end
19
23
 
20
24
  def default_value
21
- options[:default]
25
+ options[:default][:value] if options[:default]
22
26
  end
23
27
 
24
28
  def validate(object)
25
29
  Contract.new(object, self).validate
26
30
  end
27
31
 
28
- class << self
29
-
30
- def human_name
31
- "#{self.name.split('::')[1..-1].join('::')}"
32
- end
32
+ def build_option(name, value, extra_options = {})
33
+ options[name] = { value: value }.merge(extra_options)
33
34
 
35
+ self
34
36
  end
35
37
 
36
- protected
37
-
38
38
  def default_options
39
- { required: false }
39
+ { required: { value: false } }
40
40
  end
41
41
 
42
42
  end
@@ -17,19 +17,16 @@ module Compel
17
17
  super(Coercion::String)
18
18
  end
19
19
 
20
- def format(regex)
21
- options[:format] = Coercion.coerce!(regex, Coercion::Regexp)
22
- self
20
+ def format(regex, options = {})
21
+ build_option :format, Coercion.coerce!(regex, Coercion::Regexp), options
23
22
  end
24
23
 
25
- def url
26
- options[:format] = URL_REGEX
27
- self
24
+ def url(options = {})
25
+ build_option :format, URL_REGEX, options
28
26
  end
29
27
 
30
- def email
31
- options[:format] = EMAIL_REGEX
32
- self
28
+ def email(options = {})
29
+ build_option :format, EMAIL_REGEX, options
33
30
  end
34
31
 
35
32
  end
@@ -9,14 +9,12 @@ module Compel
9
9
  super(Coercion::Time)
10
10
  end
11
11
 
12
- def format(value)
13
- options[:format] = value
14
- self
12
+ def format(value, options = {})
13
+ build_option :format, value, options
15
14
  end
16
15
 
17
- def iso8601
18
- options[:format] = '%FT%T'
19
- self
16
+ def iso8601(options = {})
17
+ build_option :format, '%FT%T', options
20
18
  end
21
19
 
22
20
  end
@@ -8,7 +8,7 @@ module Compel
8
8
  end
9
9
 
10
10
  def default_format
11
- '%Y-%m-%d'
11
+ '%Y-%m-%d'
12
12
  end
13
13
 
14
14
  end
@@ -6,7 +6,11 @@ module Compel
6
6
  attr_reader :format
7
7
 
8
8
  def coerce_value
9
- @format = options[:format] || default_format
9
+ @format = default_format
10
+
11
+ if options[:format]
12
+ @format = options[:format][:value]
13
+ end
10
14
 
11
15
  if value.is_a?(klass)
12
16
  @value = value.strftime(format)
@@ -15,14 +15,21 @@ module Compel
15
15
  def initialize(value, option_value, options = {})
16
16
  @value = value
17
17
  @value_type = options.delete(:type) || Coercion::Types::Any
18
+ @options = options
18
19
  @option_value = option_value
19
20
  end
20
21
 
21
22
  def validate
22
- # result is an error message
23
- result = validate_value
23
+ Validation::Result.new \
24
+ value, value_type, validate_value_with_error_message
25
+ end
26
+
27
+ private
28
+
29
+ def validate_value_with_error_message
30
+ error_message = validate_value
24
31
 
25
- Validation::Result.new(value, value_type, result)
32
+ options[:message] || error_message
26
33
  end
27
34
 
28
35
  end
@@ -27,16 +27,21 @@ module Compel
27
27
  }
28
28
 
29
29
  def validate(value, type, options)
30
- if value.nil? && !!options[:required]
31
- return ['is required']
30
+ if value.nil? && !!(options[:required] && options[:required][:value])
31
+ return [options[:required][:message] || 'is required']
32
32
  end
33
33
 
34
34
  errors = Errors.new
35
35
 
36
- options.each do |option, option_value|
37
- next unless condition_exists?(option)
36
+ options.each do |name, option_values|
37
+ next unless condition_exists?(name)
38
38
 
39
- result = condition_klass(option).validate(value, option_value, type: type)
39
+ cloned_options = option_values.dup
40
+
41
+ option_value = cloned_options.delete(:value)
42
+
43
+ result = condition_klass(name).validate \
44
+ value, option_value, cloned_options.merge(type: type)
40
45
 
41
46
  unless result.valid?
42
47
  errors.add :base, result.error_message
@@ -46,12 +51,12 @@ module Compel
46
51
  errors.to_hash[:base] || []
47
52
  end
48
53
 
49
- def condition_exists?(option)
50
- CONDITIONS.keys.include?(option.to_sym)
54
+ def condition_exists?(option_name)
55
+ CONDITIONS.keys.include?(option_name.to_sym)
51
56
  end
52
57
 
53
- def condition_klass(option)
54
- CONDITIONS[option.to_sym]
58
+ def condition_klass(option_name)
59
+ CONDITIONS[option_name.to_sym]
55
60
  end
56
61
 
57
62
  extend self
@@ -10,7 +10,7 @@ module Compel
10
10
 
11
11
  @errors = Errors.new
12
12
  @output = []
13
- @items_schema = schema.options[:items]
13
+ @items_schema = schema.options[:items][:value] if schema.options[:items]
14
14
  end
15
15
 
16
16
  def validate
@@ -12,7 +12,7 @@ module Compel
12
12
  super
13
13
 
14
14
  @errors = Errors.new
15
- @keys_schemas = schema.options[:keys]
15
+ @keys_schemas = schema.options[:keys][:value]
16
16
  end
17
17
 
18
18
  def validate
@@ -1,3 +1,3 @@
1
1
  module Compel
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.2'
3
3
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  describe Compel::Builder do
2
4
 
3
5
  context 'Schema' do
@@ -22,7 +24,7 @@ describe Compel::Builder do
22
24
  it 'should have value' do
23
25
  [:in, :range].each do |method|
24
26
  builder.send(method, ["#{method}"])
25
- expect(builder.options[method]).to eq(["#{method}"])
27
+ expect(builder.options[method][:value]).to eq(["#{method}"])
26
28
  end
27
29
  end
28
30
 
@@ -35,7 +37,7 @@ describe Compel::Builder do
35
37
  it 'should have value' do
36
38
  [:min, :max].each do |method|
37
39
  builder.send(method, "#{method}")
38
- expect(builder.options[method]).to eq("#{method}")
40
+ expect(builder.options[method][:value]).to eq("#{method}")
39
41
  end
40
42
  end
41
43
 
@@ -50,10 +52,9 @@ describe Compel::Builder do
50
52
  context '#is, #default' do
51
53
 
52
54
  it 'should have value' do
53
-
54
55
  [:is, :default].each do |method|
55
56
  builder.send(method, "#{method}")
56
- expect(builder.options[method]).to eq("#{method}")
57
+ expect(builder.options[method][:value]).to eq("#{method}")
57
58
  end
58
59
  end
59
60
 
@@ -78,7 +79,7 @@ describe Compel::Builder do
78
79
  it 'should have value' do
79
80
  builder.length(5)
80
81
 
81
- expect(builder.options[:length]).to be 5
82
+ expect(builder.options[:length][:value]).to be 5
82
83
  end
83
84
 
84
85
  it 'should raise exception for invalid type' do
@@ -99,13 +100,13 @@ describe Compel::Builder do
99
100
  it 'should have value' do
100
101
  builder.format('%d-%m-%Y')
101
102
 
102
- expect(builder.options[:format]).to eq('%d-%m-%Y')
103
+ expect(builder.options[:format][:value]).to eq('%d-%m-%Y')
103
104
  end
104
105
 
105
106
  it 'should have value' do
106
107
  builder.iso8601
107
108
 
108
- expect(builder.options[:format]).to eq('%Y-%m-%d')
109
+ expect(builder.options[:format][:value]).to eq('%Y-%m-%d')
109
110
  end
110
111
 
111
112
  end
@@ -115,7 +116,7 @@ describe Compel::Builder do
115
116
  it 'should set max value with string and coerce' do
116
117
  builder.max('2016-01-01')
117
118
 
118
- expect(builder.options[:max]).to eq(Date.new(2016, 1, 1))
119
+ expect(builder.options[:max][:value]).to eq(Date.new(2016, 1, 1))
119
120
  end
120
121
 
121
122
  end
@@ -125,8 +126,8 @@ describe Compel::Builder do
125
126
  it 'should set max value with string and coerce' do
126
127
  builder.in(['2016-01-01', '2016-01-02'])
127
128
 
128
- expect(builder.options[:in]).to include(Date.new(2016, 1, 1))
129
- expect(builder.options[:in]).to include(Date.new(2016, 1, 2))
129
+ expect(builder.options[:in][:value]).to include(Date.new(2016, 1, 1))
130
+ expect(builder.options[:in][:value]).to include(Date.new(2016, 1, 2))
130
131
  end
131
132
 
132
133
  end
@@ -156,7 +157,7 @@ describe Compel::Builder do
156
157
  each_date_builder do |builder, klass|
157
158
  builder.in([klass.new(2016, 1, 1), klass.new(2016, 1, 2)])
158
159
 
159
- expect(builder.options[:in].length).to eq 2
160
+ expect(builder.options[:in][:value].length).to eq 2
160
161
  end
161
162
  end
162
163
 
@@ -174,7 +175,7 @@ describe Compel::Builder do
174
175
  each_date_builder do |builder, klass|
175
176
  builder.max(klass.new(2016, 1, 1))
176
177
 
177
- expect(builder.options[:max]).to eq(klass.new(2016, 1, 1))
178
+ expect(builder.options[:max][:value]).to eq(klass.new(2016, 1, 1))
178
179
  end
179
180
  end
180
181
 
@@ -195,7 +196,7 @@ describe Compel::Builder do
195
196
  each_date_builder do |builder, klass|
196
197
  builder.min(klass.new(2016, 1, 1))
197
198
 
198
- expect(builder.options[:min]).to eq(klass.new(2016, 1, 1))
199
+ expect(builder.options[:min][:value]).to eq(klass.new(2016, 1, 1))
199
200
  end
200
201
  end
201
202
 
@@ -221,7 +222,7 @@ describe Compel::Builder do
221
222
  it 'should set in value' do
222
223
  builder.in(['a', 'b'])
223
224
 
224
- expect(builder.options[:in].length).to eq 2
225
+ expect(builder.options[:in][:value].length).to eq 2
225
226
  end
226
227
 
227
228
  it 'should raise exception for invalid item on array' do
@@ -243,7 +244,7 @@ describe Compel::Builder do
243
244
  it 'should have value' do
244
245
  builder.format(/1/)
245
246
 
246
- expect(builder.options[:format]).to eq(/1/)
247
+ expect(builder.options[:format][:value]).to eq(/1/)
247
248
  end
248
249
 
249
250
  end
@@ -258,7 +259,7 @@ describe Compel::Builder do
258
259
  it 'should have value' do
259
260
  builder.min_length(4)
260
261
 
261
- expect(builder.options[:min_length]).to eq(4)
262
+ expect(builder.options[:min_length][:value]).to eq(4)
262
263
  end
263
264
 
264
265
  end
@@ -273,7 +274,7 @@ describe Compel::Builder do
273
274
  it 'should have value' do
274
275
  builder.max_length(10)
275
276
 
276
- expect(builder.options[:max_length]).to eq(10)
277
+ expect(builder.options[:max_length][:value]).to eq(10)
277
278
  end
278
279
 
279
280
  end
@@ -296,7 +297,7 @@ describe Compel::Builder do
296
297
  h: Compel.integer
297
298
  })
298
299
 
299
- keys_schemas = schema.options[:keys]
300
+ keys_schemas = schema.options[:keys][:value]
300
301
 
301
302
  expect(keys_schemas[:a].type).to be Compel::Coercion::Float
302
303
  expect(keys_schemas[:b].type).to be Compel::Coercion::String
@@ -340,7 +341,7 @@ describe Compel::Builder do
340
341
  it 'should have value' do
341
342
  builder = Compel.array.items(Compel.integer)
342
343
 
343
- expect(builder.options[:items].class).to be(Compel::Builder::Integer)
344
+ expect(builder.options[:items][:value].class).to be(Compel::Builder::Integer)
344
345
  end
345
346
 
346
347
  end
@@ -352,7 +353,7 @@ describe Compel::Builder do
352
353
  it 'should build schema' do
353
354
  builder = Compel.integer.min(10)
354
355
 
355
- expect(builder.options[:min]).to eq(10)
356
+ expect(builder.options[:min][:value]).to eq(10)
356
357
  end
357
358
 
358
359
  it 'should raise exception for invalid value' do
@@ -382,6 +383,21 @@ describe Compel::Builder do
382
383
  include('is required')
383
384
  end
384
385
 
386
+ it 'should have an array for error messages' do
387
+ schema = Compel.any.required(message: 'this is required')
388
+ expect(schema.validate(nil).errors.class).to eq Array
389
+
390
+ schema = Compel.any.required
391
+ expect(schema.validate(nil).errors.class).to eq Array
392
+ end
393
+
394
+ it 'should use custom error message' do
395
+ schema = Compel.any.required(message: 'this is required')
396
+
397
+ expect(schema.validate(nil).errors).to \
398
+ include('this is required')
399
+ end
400
+
385
401
  end
386
402
 
387
403
  context 'valid' do
@@ -468,6 +484,13 @@ describe Compel::Builder do
468
484
  include('must be [1, 2, 3]')
469
485
  end
470
486
 
487
+ it 'should use custom error message' do
488
+ schema = Compel.any.is(1, message: 'not one')
489
+
490
+ expect(schema.validate('two').errors).to \
491
+ include('not one')
492
+ end
493
+
471
494
  end
472
495
 
473
496
  context 'valid' do
@@ -502,6 +525,13 @@ describe Compel::Builder do
502
525
  include('cannot have length different than 1')
503
526
  end
504
527
 
528
+ it 'should use custom error message' do
529
+ schema = Compel.any.length(2, message: 'not the same size')
530
+
531
+ expect(schema.validate(12).errors).to \
532
+ include('not the same size')
533
+ end
534
+
505
535
  end
506
536
 
507
537
  context 'valid' do
@@ -526,6 +556,36 @@ describe Compel::Builder do
526
556
 
527
557
  end
528
558
 
559
+ context '#min_length' do
560
+
561
+ context 'invalid' do
562
+
563
+ it 'should use custom error message' do
564
+ schema = Compel.any.min_length(2, message: 'min is two')
565
+
566
+ expect(schema.validate(1).errors).to \
567
+ include('min is two')
568
+ end
569
+
570
+ end
571
+
572
+ end
573
+
574
+ context '#max_length' do
575
+
576
+ context 'invalid' do
577
+
578
+ it 'should use custom error message' do
579
+ schema = Compel.any.max_length(2, message: 'max is two')
580
+
581
+ expect(schema.validate(123).errors).to \
582
+ include('max is two')
583
+ end
584
+
585
+ end
586
+
587
+ end
588
+
529
589
  end
530
590
 
531
591
  context 'Hash' do
@@ -678,6 +738,12 @@ describe Compel::Builder do
678
738
  expect(result.valid?).to be false
679
739
  end
680
740
 
741
+ it 'should not validate and use custom error' do
742
+ result = Compel.string.url(message: 'not an URL').validate('url')
743
+
744
+ expect(result.errors).to include('not an URL')
745
+ end
746
+
681
747
  end
682
748
 
683
749
  context '#email' do
@@ -700,6 +766,12 @@ describe Compel::Builder do
700
766
  expect(result.valid?).to be false
701
767
  end
702
768
 
769
+ it 'should not validate and use custom error' do
770
+ result = Compel.string.email(message: 'not an EMAIL').validate('email')
771
+
772
+ expect(result.errors).to include('not an EMAIL')
773
+ end
774
+
703
775
  end
704
776
 
705
777
  end
@@ -884,6 +956,70 @@ describe Compel::Builder do
884
956
 
885
957
  end
886
958
 
959
+ context 'Integer' do
960
+
961
+ context '#in' do
962
+
963
+ context 'invalid' do
964
+
965
+ it 'should use custom error message' do
966
+ schema = Compel.integer.in([1, 2], message: 'not in 1 or 2')
967
+
968
+ expect(schema.validate(3).errors).to \
969
+ include('not in 1 or 2')
970
+ end
971
+
972
+ end
973
+
974
+ end
975
+
976
+ context '#range' do
977
+
978
+ context 'invalid' do
979
+
980
+ it 'should use custom error message' do
981
+ schema = Compel.integer.range([1, 2], message: 'not between 1 or 2')
982
+
983
+ expect(schema.validate(3).errors).to \
984
+ include('not between 1 or 2')
985
+ end
986
+
987
+ end
988
+
989
+ end
990
+
991
+ context '#min' do
992
+
993
+ context 'invalid' do
994
+
995
+ it 'should use custom error message' do
996
+ schema = Compel.integer.min(5, message: 'min is five')
997
+
998
+ expect(schema.validate(4).errors).to \
999
+ include('min is five')
1000
+ end
1001
+
1002
+ end
1003
+
1004
+ end
1005
+
1006
+ context '#max' do
1007
+
1008
+ context 'invalid' do
1009
+
1010
+ it 'should use custom error message' do
1011
+ schema = Compel.integer.max(5, message: 'max is five')
1012
+
1013
+ expect(schema.validate(6).errors).to \
1014
+ include('max is five')
1015
+ end
1016
+
1017
+ end
1018
+
1019
+ end
1020
+
1021
+ end
1022
+
887
1023
  end
888
1024
 
889
1025
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  describe Compel::Coercion do
2
4
 
3
5
  context 'Type coercion' do
@@ -67,7 +69,8 @@ describe Compel::Coercion do
67
69
  end
68
70
 
69
71
  it 'should coerce with format' do
70
- value = Compel::Coercion.coerce!('22-12-2015', Compel::Coercion::Date, { format: '%d-%m-%Y' })
72
+ value = Compel::Coercion.coerce! \
73
+ '22-12-2015', Compel::Coercion::Date, { format: { value: '%d-%m-%Y' } }
71
74
 
72
75
  expect(value).to eq(Date.strptime('22-12-2015', '%d-%m-%Y'))
73
76
  end
@@ -98,7 +101,7 @@ describe Compel::Coercion do
98
101
  it 'should coerce' do
99
102
  [DateTime, Time].each do |time_klass|
100
103
  value = Compel::Coercion.coerce! \
101
- '2015-12-22', Compel::Coercion.const_get("#{time_klass}"), format: '%Y-%m-%d'
104
+ '2015-12-22', Compel::Coercion.const_get("#{time_klass}"), format: { value: '%Y-%m-%d' }
102
105
 
103
106
  expect(value).to be_a time_klass
104
107
  expect(value.year).to eq(2015)
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  describe Compel do
2
4
 
3
5
  context '#run' do
@@ -360,6 +362,14 @@ describe Compel do
360
362
  expect(result.errors).to include('must match format ^\\d{4}-\\d{3}$')
361
363
  end
362
364
 
365
+ it 'should not compel with custom message' do
366
+ schema = Compel.string.format(/^\d{4}-\d{3}$/, message: 'this format is not good')
367
+
368
+ result = Compel.run('110-100', schema)
369
+
370
+ expect(result.errors).to include('this format is not good')
371
+ end
372
+
363
373
  end
364
374
 
365
375
  end
@@ -3,13 +3,13 @@ describe Compel::Validation do
3
3
  context 'required' do
4
4
 
5
5
  it 'should validate without errors' do
6
- errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { required: true })
6
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { required: { value: true } })
7
7
 
8
8
  expect(errors.empty?).to eq(true)
9
9
  end
10
10
 
11
11
  it 'should validate with error' do
12
- errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, { required: true })
12
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, { required: { value: true } })
13
13
 
14
14
  expect(errors.empty?).to eq(false)
15
15
  expect(errors).to eq(['is required'])
@@ -20,7 +20,7 @@ describe Compel::Validation do
20
20
  context 'length' do
21
21
 
22
22
  it 'should validate without errors' do
23
- errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { length: 3 })
23
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, { length: { value: 3 } })
24
24
 
25
25
  expect(errors.empty?).to eq(true)
26
26
  end
@@ -31,7 +31,7 @@ describe Compel::Validation do
31
31
 
32
32
  def expect_be_in_range(range, value)
33
33
  [:in, :range].each do |key|
34
- errors = Compel::Validation.validate(value, Compel::Coercion::String, { key => range })
34
+ errors = Compel::Validation.validate(value, Compel::Coercion::String, { key => { value: range } })
35
35
  yield errors
36
36
  end
37
37
  end
@@ -51,13 +51,13 @@ describe Compel::Validation do
51
51
  context 'range' do
52
52
 
53
53
  it 'should validate without errors' do
54
- errors = Compel::Validation.validate(2, Compel::Coercion::Integer, range: (1..3))
54
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, range: { value: (1..3) })
55
55
 
56
56
  expect(errors.empty?).to eq(true)
57
57
  end
58
58
 
59
59
  it 'should validate with errors' do
60
- errors = Compel::Validation.validate(4, Compel::Coercion::Integer, range: (1..3))
60
+ errors = Compel::Validation.validate(4, Compel::Coercion::Integer, range: { value: (1..3) })
61
61
 
62
62
  expect(errors).to include('must be within 1..3')
63
63
  end
@@ -70,14 +70,14 @@ describe Compel::Validation do
70
70
 
71
71
  it 'should validate with errors' do
72
72
  format = /^abcd/
73
- errors = Compel::Validation.validate('acb', Compel::Coercion::String, format: format)
73
+ errors = Compel::Validation.validate('acb', Compel::Coercion::String, format: { value: format })
74
74
 
75
75
  expect(errors).to include("must match format ^abcd")
76
76
  end
77
77
 
78
78
  it 'should validate without errors' do
79
79
  format = /abcd/
80
- errors = Compel::Validation.validate('abcd', Compel::Coercion::String, format: format)
80
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::String, format: { value: format })
81
81
  expect(errors.empty?).to eq(true)
82
82
  end
83
83
 
@@ -86,17 +86,17 @@ describe Compel::Validation do
86
86
  context 'is' do
87
87
 
88
88
  it 'should validate with errors' do
89
- errors = Compel::Validation.validate('abcd', Compel::Coercion::Integer, is: 123)
89
+ errors = Compel::Validation.validate('abcd', Compel::Coercion::Integer, is: { value: 123 })
90
90
  expect(errors).to include('must be 123')
91
91
  end
92
92
 
93
93
  it 'should validate with errors 1' do
94
- errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, is: 123)
94
+ errors = Compel::Validation.validate(nil, Compel::Coercion::Integer, is: { value: 123 })
95
95
  expect(errors).to include('must be 123')
96
96
  end
97
97
 
98
98
  it 'should validate without errors' do
99
- errors = Compel::Validation.validate(123, Compel::Coercion::Integer, is: 123)
99
+ errors = Compel::Validation.validate(123, Compel::Coercion::Integer, is: { value: 123 })
100
100
  expect(errors.empty?).to eq(true)
101
101
  end
102
102
 
@@ -105,31 +105,31 @@ describe Compel::Validation do
105
105
  context 'min' do
106
106
 
107
107
  it 'should validate without errors for Integer' do
108
- errors = Compel::Validation.validate(2, Compel::Coercion::Integer, min: 1)
108
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, min: { value: 1 })
109
109
 
110
110
  expect(errors.empty?).to be true
111
111
  end
112
112
 
113
113
  it 'should validate without errors for String' do
114
- errors = Compel::Validation.validate('b', Compel::Coercion::String, min: 'a')
114
+ errors = Compel::Validation.validate('b', Compel::Coercion::String, min: { value: 'a' })
115
115
 
116
116
  expect(errors.empty?).to be true
117
117
  end
118
118
 
119
119
  it 'should validate with errors' do
120
- errors = Compel::Validation.validate(1, Compel::Coercion::Integer, min: 3)
120
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, min: { value: 3 })
121
121
 
122
122
  expect(errors).to include('cannot be less than 3')
123
123
  end
124
124
 
125
125
  it 'should validate with errors for String' do
126
- errors = Compel::Validation.validate('a', Compel::Coercion::String, min: 'b')
126
+ errors = Compel::Validation.validate('a', Compel::Coercion::String, min: { value: 'b' })
127
127
 
128
128
  expect(errors).to include('cannot be less than b')
129
129
  end
130
130
 
131
131
  it 'should validate with errors for Float' do
132
- errors = Compel::Validation.validate(1.54, Compel::Coercion::Float, min: 1.55)
132
+ errors = Compel::Validation.validate(1.54, Compel::Coercion::Float, min: { value: 1.55 })
133
133
 
134
134
  expect(errors).to include('cannot be less than 1.55')
135
135
  end
@@ -139,37 +139,37 @@ describe Compel::Validation do
139
139
  context 'max' do
140
140
 
141
141
  it 'should validate without errors' do
142
- errors = Compel::Validation.validate(5, Compel::Coercion::Integer, min: 5)
142
+ errors = Compel::Validation.validate(5, Compel::Coercion::Integer, min: { value: 5 })
143
143
 
144
144
  expect(errors.empty?).to be true
145
145
  end
146
146
 
147
147
  it 'should validate with errors' do
148
- errors = Compel::Validation.validate(3, Compel::Coercion::Integer, max: 2)
148
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, max: { value: 2 })
149
149
 
150
150
  expect(errors).to include('cannot be greater than 2')
151
151
  end
152
152
 
153
153
  it 'should validate without errors for Integer' do
154
- errors = Compel::Validation.validate(2, Compel::Coercion::Integer, max: 3)
154
+ errors = Compel::Validation.validate(2, Compel::Coercion::Integer, max: { value: 3 })
155
155
 
156
156
  expect(errors.empty?).to be true
157
157
  end
158
158
 
159
159
  it 'should validate without errors for String' do
160
- errors = Compel::Validation.validate('b', Compel::Coercion::String, max: 'd')
160
+ errors = Compel::Validation.validate('b', Compel::Coercion::String, max: { value: 'd' })
161
161
 
162
162
  expect(errors.empty?).to be true
163
163
  end
164
164
 
165
165
  it 'should validate with errors for String' do
166
- errors = Compel::Validation.validate('c', Compel::Coercion::String, max: 'b')
166
+ errors = Compel::Validation.validate('c', Compel::Coercion::String, max: { value: 'b' })
167
167
 
168
168
  expect(errors).to include('cannot be greater than b')
169
169
  end
170
170
 
171
171
  it 'should validate with errors for Float' do
172
- errors = Compel::Validation.validate(1.56, Compel::Coercion::Float, max: 1.55)
172
+ errors = Compel::Validation.validate(1.56, Compel::Coercion::Float, max: { value: 1.55 })
173
173
 
174
174
  expect(errors).to include('cannot be greater than 1.55')
175
175
  end
@@ -179,19 +179,19 @@ describe Compel::Validation do
179
179
  context 'min_length' do
180
180
 
181
181
  it 'should validate with errors 1' do
182
- errors = Compel::Validation.validate('a', Compel::Coercion::String, min_length: 2)
182
+ errors = Compel::Validation.validate('a', Compel::Coercion::String, min_length: { value: 2 })
183
183
 
184
184
  expect(errors).to include('cannot have length less than 2')
185
185
  end
186
186
 
187
187
  it 'should validate without errors' do
188
- errors = Compel::Validation.validate('ab', Compel::Coercion::String, min_length: 2)
188
+ errors = Compel::Validation.validate('ab', Compel::Coercion::String, min_length: { value: 2 })
189
189
 
190
190
  expect(errors.empty?).to eq(true)
191
191
  end
192
192
 
193
193
  it 'should validate without errors 1' do
194
- errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: 2)
194
+ errors = Compel::Validation.validate(3, Compel::Coercion::Integer, min_length: { value: 2 })
195
195
 
196
196
  expect(errors).to include('cannot have length less than 2')
197
197
  end
@@ -201,19 +201,19 @@ describe Compel::Validation do
201
201
  context 'max_length' do
202
202
 
203
203
  it 'should validate with errors 1' do
204
- errors = Compel::Validation.validate('abcdef', Compel::Coercion::String, max_length: 5)
204
+ errors = Compel::Validation.validate('abcdef', Compel::Coercion::String, max_length: { value: 5 })
205
205
 
206
206
  expect(errors).to include('cannot have length greater than 5')
207
207
  end
208
208
 
209
209
  it 'should validate without errors' do
210
- errors = Compel::Validation.validate('abcde', Compel::Coercion::String, max_length: 5)
210
+ errors = Compel::Validation.validate('abcde', Compel::Coercion::String, max_length: { value: 5 })
211
211
 
212
212
  expect(errors.empty?).to eq(true)
213
213
  end
214
214
 
215
215
  it 'should validate without errors 1' do
216
- errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: 2)
216
+ errors = Compel::Validation.validate(1, Compel::Coercion::Integer, max_length: { value: 2 })
217
217
 
218
218
  expect(errors.empty?).to eq(true)
219
219
  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.4.0
4
+ version: 0.4.2
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-03-04 00:00:00.000000000 Z
11
+ date: 2016-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  requirements: []
150
150
  rubyforge_project:
151
- rubygems_version: 2.5.1
151
+ rubygems_version: 2.4.6
152
152
  signing_key:
153
153
  specification_version: 4
154
154
  summary: Ruby Object Coercion and Validation