compel 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -17
- data/lib/compel/builder/common.rb +2 -2
- data/lib/compel/builder/common_value.rb +44 -4
- data/lib/compel/builder/date.rb +2 -0
- data/lib/compel/builder/datetime.rb +2 -0
- data/lib/compel/builder/schema.rb +16 -2
- data/lib/compel/builder/string.rb +18 -0
- data/lib/compel/builder/time.rb +2 -0
- data/lib/compel/coercion/coercion.rb +1 -0
- data/lib/compel/coercion/types/date.rb +5 -24
- data/lib/compel/coercion/types/date_type.rb +36 -0
- data/lib/compel/coercion/types/datetime.rb +5 -24
- data/lib/compel/coercion/types/time.rb +5 -24
- data/lib/compel/coercion/types/type.rb +8 -0
- data/lib/compel/validation/conditions/in.rb +0 -2
- data/lib/compel/validation/validation.rb +0 -1
- data/lib/compel/version.rb +1 -1
- data/lib/compel.rb +1 -0
- data/spec/compel/builder_spec.rb +271 -63
- data/spec/compel/validation_spec.rb +5 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd80b5474b8218a24a20053cbf9a52645965acd3
|
4
|
+
data.tar.gz: c793b3161901f6aa6872b0763a7fb9570c9864d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78f27f5b101c0255540f8a99018d750247ddae79eca4847b1a656fbbd527e4af1445ce12567f5049b201fc1309bdd1b594af225876937b450689fc269f2b92c6
|
7
|
+
data.tar.gz: 6c1d7772d9cd20a21f6e36fe0f1c717bebb358a4a98ed2c3fb6831f986770895d4040901bf4a8ccc25e3b0ccd16cd72ae34ed0b142e9c02330bb83a4946c4ebf
|
data/README.md
CHANGED
@@ -37,29 +37,38 @@ schema = Compel.hash.keys({
|
|
37
37
|
})
|
38
38
|
})
|
39
39
|
|
40
|
-
Compel.run(object, schema)
|
40
|
+
Compel.run(object, schema) # or schema.validate(object)
|
41
41
|
```
|
42
42
|
|
43
|
-
Will return
|
43
|
+
Will return a `Compel::Result` object:
|
44
44
|
|
45
45
|
```ruby
|
46
|
-
|
47
|
-
|
48
|
-
"birth_date" => "1989-0",
|
49
|
-
"address" => {
|
50
|
-
"line_one" => "Lisboa",
|
51
|
-
"line_two" => "-",
|
52
|
-
"post_code" => "1100",
|
53
|
-
"country_code" => "PT"
|
54
|
-
},
|
55
|
-
"errors" => {
|
46
|
+
=> <Compel::Result
|
47
|
+
@errors={
|
56
48
|
"last_name" => ["is required"],
|
57
|
-
"birth_date" => ["'1989-0' is not a parsable
|
49
|
+
"birth_date" => ["'1989-0' is not a parsable datetime with format: %FT%T"],
|
58
50
|
"address" => {
|
59
|
-
"post_code" => ["must match format
|
51
|
+
"post_code" => ["must match format ^\\d{4}-\\d{3}$"]
|
60
52
|
}
|
61
|
-
}
|
62
|
-
|
53
|
+
},
|
54
|
+
@valid=false,
|
55
|
+
@value={
|
56
|
+
"first_name" => "Joaquim",
|
57
|
+
"birth_date" => "1989-0",
|
58
|
+
"address" => {
|
59
|
+
"line_one" => "Lisboa",
|
60
|
+
"post_code" => "1100",
|
61
|
+
"country_code" => "PT",
|
62
|
+
"line_two" => "-"
|
63
|
+
},
|
64
|
+
"errors" => {
|
65
|
+
"last_name" => ["is required"],
|
66
|
+
"birth_date" => ["'1989-0' is not a parsable datetime with format: %FT%T"],
|
67
|
+
"address" => {
|
68
|
+
"post_code" => ["must match format ^\\d{4}-\\d{3}$"]
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}>
|
63
72
|
```
|
64
73
|
|
65
74
|
There are 4 ways to run validations:
|
@@ -259,7 +268,7 @@ end
|
|
259
268
|
|
260
269
|
Add this line to your application's Gemfile:
|
261
270
|
|
262
|
-
gem 'compel', '~> 0.3.
|
271
|
+
gem 'compel', '~> 0.3.7'
|
263
272
|
|
264
273
|
And then execute:
|
265
274
|
|
@@ -4,7 +4,7 @@ module Compel
|
|
4
4
|
module Common
|
5
5
|
|
6
6
|
def is(value)
|
7
|
-
options[:is] = value
|
7
|
+
options[:is] = Coercion.coerce!(value, self.type)
|
8
8
|
self
|
9
9
|
end
|
10
10
|
|
@@ -14,7 +14,7 @@ module Compel
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def default(value)
|
17
|
-
options[:default] = value
|
17
|
+
options[:default] = Coercion.coerce!(value, self.type)
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
@@ -4,25 +4,65 @@ module Compel
|
|
4
4
|
module CommonValue
|
5
5
|
|
6
6
|
def in(value)
|
7
|
-
options[:in] = value
|
7
|
+
options[:in] = coerce_values_ary!(value, :in)
|
8
8
|
self
|
9
9
|
end
|
10
10
|
|
11
11
|
def range(value)
|
12
|
-
options[:range] = value
|
12
|
+
options[:range] = coerce_values_ary!(value, :range)
|
13
13
|
self
|
14
14
|
end
|
15
15
|
|
16
16
|
def min(value)
|
17
|
-
options[:min] = value
|
17
|
+
options[:min] = coerce_value!(value, :min)
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
21
21
|
def max(value)
|
22
|
-
options[:max] = value
|
22
|
+
options[:max] = coerce_value!(value, :max)
|
23
23
|
self
|
24
24
|
end
|
25
25
|
|
26
|
+
def coerce_values_ary!(values, method)
|
27
|
+
begin
|
28
|
+
fail if values.nil?
|
29
|
+
|
30
|
+
Coercion.coerce!(values, Coercion::Array)
|
31
|
+
rescue
|
32
|
+
raise_array_error(method)
|
33
|
+
end
|
34
|
+
|
35
|
+
values.map{ |value| Coercion.coerce!(value, self.type) }
|
36
|
+
|
37
|
+
rescue
|
38
|
+
raise_array_values_error(method)
|
39
|
+
end
|
40
|
+
|
41
|
+
def coerce_value!(value, method)
|
42
|
+
begin
|
43
|
+
fail if value.nil?
|
44
|
+
|
45
|
+
Coercion.coerce!(value, self.type)
|
46
|
+
rescue
|
47
|
+
raise_value_error(method)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def raise_array_error(method)
|
52
|
+
raise TypeError, "#{self.class.human_name} ##{method} " \
|
53
|
+
"value must an Array"
|
54
|
+
end
|
55
|
+
|
56
|
+
def raise_array_values_error(method)
|
57
|
+
raise TypeError, "All #{self.class.human_name} ##{method} values " \
|
58
|
+
"must be a valid #{self.type.human_name}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def raise_value_error(method)
|
62
|
+
raise TypeError, "#{self.class.human_name} ##{method} value " \
|
63
|
+
"must be a valid #{self.type.human_name}"
|
64
|
+
end
|
65
|
+
|
26
66
|
end
|
27
67
|
|
28
68
|
end
|
data/lib/compel/builder/date.rb
CHANGED
@@ -10,11 +10,11 @@ module Compel
|
|
10
10
|
|
11
11
|
def initialize(type)
|
12
12
|
@type = type
|
13
|
-
@options =
|
13
|
+
@options = default_options
|
14
14
|
end
|
15
15
|
|
16
16
|
def required?
|
17
|
-
|
17
|
+
options[:required]
|
18
18
|
end
|
19
19
|
|
20
20
|
def default_value
|
@@ -25,6 +25,20 @@ module Compel
|
|
25
25
|
Contract.new(object, self).validate
|
26
26
|
end
|
27
27
|
|
28
|
+
class << self
|
29
|
+
|
30
|
+
def human_name
|
31
|
+
"#{self.name.split('::')[1..-1].join('::')}"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def default_options
|
39
|
+
Hashie::Mash.new(required: false)
|
40
|
+
end
|
41
|
+
|
28
42
|
end
|
29
43
|
|
30
44
|
end
|
@@ -3,6 +3,14 @@ module Compel
|
|
3
3
|
|
4
4
|
class String < Schema
|
5
5
|
|
6
|
+
# Taken from ruby_regex gem by @eparreno
|
7
|
+
# https://github.com/eparreno/ruby_regex
|
8
|
+
URL_REGEX = /(\A\z)|(\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\z)/ix
|
9
|
+
|
10
|
+
# Taken from Michael Hartl's 'The Ruby on Rails Tutorial'
|
11
|
+
# https://www.railstutorial.org/book
|
12
|
+
EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
|
13
|
+
|
6
14
|
include CommonValue
|
7
15
|
|
8
16
|
def initialize
|
@@ -14,6 +22,16 @@ module Compel
|
|
14
22
|
self
|
15
23
|
end
|
16
24
|
|
25
|
+
def url
|
26
|
+
options[:format] = URL_REGEX
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def email
|
31
|
+
options[:format] = EMAIL_REGEX
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
17
35
|
end
|
18
36
|
|
19
37
|
end
|
data/lib/compel/builder/time.rb
CHANGED
@@ -1,33 +1,14 @@
|
|
1
1
|
module Compel
|
2
2
|
module Coercion
|
3
3
|
|
4
|
-
class Date <
|
4
|
+
class Date < DateType
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
def coerce_value
|
9
|
-
@format = options[:format] || '%Y-%m-%d'
|
10
|
-
|
11
|
-
if value.is_a?(::Date)
|
12
|
-
@value = value.strftime(format)
|
13
|
-
end
|
14
|
-
|
15
|
-
coerced = ::Date.strptime(value, format)
|
16
|
-
|
17
|
-
if coerced.strftime(format) == value
|
18
|
-
return coerced
|
19
|
-
end
|
20
|
-
|
21
|
-
build_error_result
|
22
|
-
|
23
|
-
rescue
|
24
|
-
build_error_result
|
6
|
+
def klass
|
7
|
+
::Date
|
25
8
|
end
|
26
9
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
Result.new(nil, value, self.class, custom_error)
|
10
|
+
def default_format
|
11
|
+
'%Y-%m-%d'
|
31
12
|
end
|
32
13
|
|
33
14
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Compel
|
2
|
+
module Coercion
|
3
|
+
|
4
|
+
class DateType < Type
|
5
|
+
|
6
|
+
attr_reader :format
|
7
|
+
|
8
|
+
def coerce_value
|
9
|
+
@format = options[:format] || default_format
|
10
|
+
|
11
|
+
if value.is_a?(klass)
|
12
|
+
@value = value.strftime(format)
|
13
|
+
end
|
14
|
+
|
15
|
+
coerced = klass.strptime(value, format)
|
16
|
+
|
17
|
+
if coerced.strftime(format) == value
|
18
|
+
return coerced
|
19
|
+
end
|
20
|
+
|
21
|
+
build_error_result
|
22
|
+
|
23
|
+
rescue
|
24
|
+
build_error_result
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_error_result
|
28
|
+
custom_error = "'#{value}' is not a parsable #{klass.to_s.downcase} with format: #{format}"
|
29
|
+
|
30
|
+
Result.new(nil, value, self.class, custom_error)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -1,33 +1,14 @@
|
|
1
1
|
module Compel
|
2
2
|
module Coercion
|
3
3
|
|
4
|
-
class DateTime <
|
4
|
+
class DateTime < DateType
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
def coerce_value
|
9
|
-
@format = options[:format] || '%FT%T'
|
10
|
-
|
11
|
-
if value.is_a?(::DateTime)
|
12
|
-
@value = value.strftime(format)
|
13
|
-
end
|
14
|
-
|
15
|
-
coerced = ::DateTime.strptime(value, format)
|
16
|
-
|
17
|
-
if coerced.strftime(format) == value
|
18
|
-
return coerced
|
19
|
-
end
|
20
|
-
|
21
|
-
build_error_result
|
22
|
-
|
23
|
-
rescue
|
24
|
-
build_error_result
|
6
|
+
def klass
|
7
|
+
::DateTime
|
25
8
|
end
|
26
9
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
Result.new(nil, value, self.class, custom_error)
|
10
|
+
def default_format
|
11
|
+
'%FT%T'
|
31
12
|
end
|
32
13
|
|
33
14
|
end
|
@@ -1,33 +1,14 @@
|
|
1
1
|
module Compel
|
2
2
|
module Coercion
|
3
3
|
|
4
|
-
class Time <
|
4
|
+
class Time < DateType
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
def coerce_value
|
9
|
-
@format = options[:format] || '%FT%T'
|
10
|
-
|
11
|
-
if value.is_a?(::Time)
|
12
|
-
@value = value.strftime(format)
|
13
|
-
end
|
14
|
-
|
15
|
-
coerced = ::Time.strptime(value, format)
|
16
|
-
|
17
|
-
if coerced.strftime(format) == value
|
18
|
-
return coerced
|
19
|
-
end
|
20
|
-
|
21
|
-
build_error_result
|
22
|
-
|
23
|
-
rescue
|
24
|
-
build_error_result
|
6
|
+
def klass
|
7
|
+
::Time
|
25
8
|
end
|
26
9
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
Result.new(nil, value, self.class, custom_error)
|
10
|
+
def default_format
|
11
|
+
'%FT%T'
|
31
12
|
end
|
32
13
|
|
33
14
|
end
|
data/lib/compel/version.rb
CHANGED
data/lib/compel.rb
CHANGED
data/spec/compel/builder_spec.rb
CHANGED
@@ -8,19 +8,32 @@ describe Compel::Builder do
|
|
8
8
|
builder = Compel.string
|
9
9
|
|
10
10
|
expect(builder.type).to be(Compel::Coercion::String)
|
11
|
-
expect(builder.options.keys).to
|
11
|
+
expect(builder.options.keys).to include('required')
|
12
12
|
expect(builder.required?).to be false
|
13
13
|
expect(builder.default_value).to be nil
|
14
14
|
end
|
15
15
|
|
16
16
|
context 'Builder::CommonValue' do
|
17
17
|
|
18
|
-
context '#in, #range
|
18
|
+
context '#in, #range' do
|
19
19
|
|
20
20
|
subject(:builder) { Compel.string }
|
21
21
|
|
22
22
|
it 'should have value' do
|
23
|
-
[:in, :range
|
23
|
+
[:in, :range].each do |method|
|
24
|
+
builder.send(method, ["#{method}"])
|
25
|
+
expect(builder.options[method]).to eq(["#{method}"])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context '#min, #max' do
|
32
|
+
|
33
|
+
subject(:builder) { Compel.string }
|
34
|
+
|
35
|
+
it 'should have value' do
|
36
|
+
[:min, :max].each do |method|
|
24
37
|
builder.send(method, "#{method}")
|
25
38
|
expect(builder.options[method]).to eq("#{method}")
|
26
39
|
end
|
@@ -97,12 +110,129 @@ describe Compel::Builder do
|
|
97
110
|
|
98
111
|
end
|
99
112
|
|
113
|
+
context '#max' do
|
114
|
+
|
115
|
+
it 'should set max value with string and coerce' do
|
116
|
+
builder.max('2016-01-01')
|
117
|
+
|
118
|
+
expect(builder.options[:max]).to eq(Date.new(2016, 1, 1))
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
context '#in' do
|
124
|
+
|
125
|
+
it 'should set max value with string and coerce' do
|
126
|
+
builder.in(['2016-01-01', '2016-01-02'])
|
127
|
+
|
128
|
+
expect(builder.options[:in]).to include(Date.new(2016, 1, 1))
|
129
|
+
expect(builder.options[:in]).to include(Date.new(2016, 1, 2))
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'Date, DateTime and Time' do
|
137
|
+
|
138
|
+
def each_date_builder
|
139
|
+
[Date, DateTime, Time].each do |klass|
|
140
|
+
builder = Compel.send("#{klass.to_s.downcase}")
|
141
|
+
|
142
|
+
yield builder, klass
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'in' do
|
147
|
+
|
148
|
+
def expect_raise_error_for(builder, values, klass)
|
149
|
+
expect{ builder.in(values) }.to \
|
150
|
+
raise_error \
|
151
|
+
Compel::TypeError,
|
152
|
+
"All Builder::#{klass} #in values must be a valid #{klass}"
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'should set in value' do
|
156
|
+
each_date_builder do |builder, klass|
|
157
|
+
builder.in([klass.new(2016, 1, 1), klass.new(2016, 1, 2)])
|
158
|
+
|
159
|
+
expect(builder.options[:in].length).to eq 2
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should raise exception for invalid #in values' do
|
164
|
+
each_date_builder do |builder, klass|
|
165
|
+
expect_raise_error_for(builder, [klass.new(2016, 1, 1), 'invalid_date'], klass)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
context '#max' do
|
172
|
+
|
173
|
+
it 'should set max value' do
|
174
|
+
each_date_builder do |builder, klass|
|
175
|
+
builder.max(klass.new(2016, 1, 1))
|
176
|
+
|
177
|
+
expect(builder.options[:max]).to eq(klass.new(2016, 1, 1))
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should raise exception for invalid value' do
|
182
|
+
each_date_builder do |builder, klass|
|
183
|
+
expect{ builder.max(1) }.to \
|
184
|
+
raise_error \
|
185
|
+
Compel::TypeError,
|
186
|
+
"Builder::#{klass} #max value must be a valid #{klass}"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
|
192
|
+
context '#min' do
|
193
|
+
|
194
|
+
it 'should set min value' do
|
195
|
+
each_date_builder do |builder, klass|
|
196
|
+
builder.min(klass.new(2016, 1, 1))
|
197
|
+
|
198
|
+
expect(builder.options[:min]).to eq(klass.new(2016, 1, 1))
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'should raise exception for invalid value' do
|
203
|
+
each_date_builder do |builder, klass|
|
204
|
+
expect{ builder.min(1) }.to \
|
205
|
+
raise_error \
|
206
|
+
Compel::TypeError,
|
207
|
+
"Builder::#{klass} #min value must be a valid #{klass}"
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
100
213
|
end
|
101
214
|
|
102
215
|
context 'String' do
|
103
216
|
|
104
217
|
subject(:builder) { Compel.string }
|
105
218
|
|
219
|
+
context 'in' do
|
220
|
+
|
221
|
+
it 'should set in value' do
|
222
|
+
builder.in(['a', 'b'])
|
223
|
+
|
224
|
+
expect(builder.options[:in].length).to eq 2
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'should raise exception for invalid item on array' do
|
228
|
+
expect{ builder.in([1, 'b']) }.to \
|
229
|
+
raise_error \
|
230
|
+
Compel::TypeError,
|
231
|
+
"All Builder::String #in values must be a valid String"
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
106
236
|
context '#format' do
|
107
237
|
|
108
238
|
it 'should raise exception for invalid type' do
|
@@ -195,6 +325,46 @@ describe Compel::Builder do
|
|
195
325
|
|
196
326
|
end
|
197
327
|
|
328
|
+
context 'Array' do
|
329
|
+
|
330
|
+
it 'should raise exception for invalid type' do
|
331
|
+
expect { Compel.array.items('a') }.to \
|
332
|
+
raise_error Compel::TypeError, "#items must be a valid Schema"
|
333
|
+
end
|
334
|
+
|
335
|
+
it 'should raise exception for invalid type' do
|
336
|
+
expect { Compel.array.items('a') }.to \
|
337
|
+
raise_error Compel::TypeError, "#items must be a valid Schema"
|
338
|
+
end
|
339
|
+
|
340
|
+
it 'should have value' do
|
341
|
+
builder = Compel.array.items(Compel.integer)
|
342
|
+
|
343
|
+
expect(builder.options[:items].class).to be(Compel::Builder::Integer)
|
344
|
+
end
|
345
|
+
|
346
|
+
end
|
347
|
+
|
348
|
+
context 'Integer' do
|
349
|
+
|
350
|
+
context 'min' do
|
351
|
+
|
352
|
+
it 'should build schema' do
|
353
|
+
builder = Compel.integer.min(10)
|
354
|
+
|
355
|
+
expect(builder.options[:min]).to eq(10)
|
356
|
+
end
|
357
|
+
|
358
|
+
it 'should raise exception for invalid value' do
|
359
|
+
expect{ Compel.integer.min('ten') }.to \
|
360
|
+
raise_error \
|
361
|
+
Compel::TypeError, 'Builder::Integer #min value must be a valid Integer'
|
362
|
+
end
|
363
|
+
|
364
|
+
end
|
365
|
+
|
366
|
+
end
|
367
|
+
|
198
368
|
end
|
199
369
|
|
200
370
|
context 'Validate' do
|
@@ -414,6 +584,26 @@ describe Compel::Builder do
|
|
414
584
|
include('is required')
|
415
585
|
end
|
416
586
|
|
587
|
+
context '#required' do
|
588
|
+
|
589
|
+
it 'should validate empty keys option' do
|
590
|
+
schema = Compel.hash.required
|
591
|
+
|
592
|
+
expect(schema.validate({ a: 1 }).valid?).to be true
|
593
|
+
end
|
594
|
+
|
595
|
+
it 'should validate nil' do
|
596
|
+
schema = Compel.hash.required
|
597
|
+
|
598
|
+
result = schema.validate(nil)
|
599
|
+
|
600
|
+
expect(result.valid?).to be false
|
601
|
+
expect(result.errors[:base]).to \
|
602
|
+
include('is required')
|
603
|
+
end
|
604
|
+
|
605
|
+
end
|
606
|
+
|
417
607
|
context '#is' do
|
418
608
|
|
419
609
|
it 'should validate with errors' do
|
@@ -438,6 +628,18 @@ describe Compel::Builder do
|
|
438
628
|
|
439
629
|
end
|
440
630
|
|
631
|
+
context '#length' do
|
632
|
+
|
633
|
+
it 'should validate empty keys with errors' do
|
634
|
+
result = Compel.hash.length(2).validate({ a: 1 })
|
635
|
+
|
636
|
+
expect(result.valid?).to be false
|
637
|
+
expect(result.errors[:base]).to \
|
638
|
+
include('cannot have length different than 2')
|
639
|
+
end
|
640
|
+
|
641
|
+
end
|
642
|
+
|
441
643
|
end
|
442
644
|
|
443
645
|
context 'String' do
|
@@ -450,26 +652,74 @@ describe Compel::Builder do
|
|
450
652
|
include("must match format ^\\d{4}-\\d{3}$")
|
451
653
|
end
|
452
654
|
|
655
|
+
context '#url' do
|
656
|
+
|
657
|
+
it 'should validate' do
|
658
|
+
result = Compel.string.url.validate('http://example.com')
|
659
|
+
|
660
|
+
expect(result.valid?).to be true
|
661
|
+
end
|
662
|
+
|
663
|
+
it 'should validate' do
|
664
|
+
result = Compel.string.url.validate('http://app.com/posts/1/comments')
|
665
|
+
|
666
|
+
expect(result.valid?).to be true
|
667
|
+
end
|
668
|
+
|
669
|
+
it 'should not validate' do
|
670
|
+
result = Compel.string.url.validate('www.example.com')
|
671
|
+
|
672
|
+
expect(result.valid?).to be false
|
673
|
+
end
|
674
|
+
|
675
|
+
it 'should not validate' do
|
676
|
+
result = Compel.string.url.validate('url')
|
677
|
+
|
678
|
+
expect(result.valid?).to be false
|
679
|
+
end
|
680
|
+
|
681
|
+
end
|
682
|
+
|
683
|
+
context '#email' do
|
684
|
+
|
685
|
+
it 'should validate' do
|
686
|
+
result = Compel.string.email.validate('example@gmail.com')
|
687
|
+
|
688
|
+
expect(result.valid?).to be true
|
689
|
+
end
|
690
|
+
|
691
|
+
it 'should not validate' do
|
692
|
+
result = Compel.string.email.validate('example@gmail')
|
693
|
+
|
694
|
+
expect(result.valid?).to be false
|
695
|
+
end
|
696
|
+
|
697
|
+
it 'should not validate' do
|
698
|
+
result = Compel.string.email.validate('email')
|
699
|
+
|
700
|
+
expect(result.valid?).to be false
|
701
|
+
end
|
702
|
+
|
703
|
+
end
|
704
|
+
|
453
705
|
end
|
454
706
|
|
455
707
|
context 'Array' do
|
456
708
|
|
457
|
-
subject(:builder) { Compel.array }
|
458
|
-
|
459
709
|
it 'should validate nil without errors' do
|
460
|
-
result =
|
710
|
+
result = Compel.array.validate(nil)
|
461
711
|
|
462
712
|
expect(result.valid?).to be true
|
463
713
|
end
|
464
714
|
|
465
715
|
it 'should validate nil with errors' do
|
466
|
-
result =
|
716
|
+
result = Compel.array.required.validate(nil)
|
467
717
|
|
468
718
|
expect(result.errors[:base]).to include('is required')
|
469
719
|
end
|
470
720
|
|
471
721
|
it 'should validate with errors for invalid array' do
|
472
|
-
result =
|
722
|
+
result = Compel.array.required.validate(1)
|
473
723
|
|
474
724
|
expect(result.errors[:base]).to include("'1' is not a valid Array")
|
475
725
|
end
|
@@ -477,41 +727,21 @@ describe Compel::Builder do
|
|
477
727
|
context '#items' do
|
478
728
|
|
479
729
|
it 'should validate without items' do
|
480
|
-
result =
|
730
|
+
result = Compel.array.validate([1, 2, 3])
|
481
731
|
|
482
732
|
expect(result.valid?).to be true
|
483
733
|
expect(result.value).to eq([1, 2, 3])
|
484
734
|
end
|
485
735
|
|
486
|
-
it 'should raise exception for invalid type' do
|
487
|
-
expect { builder.items('a') }.to \
|
488
|
-
raise_error Compel::TypeError, "#items must be a valid Schema"
|
489
|
-
end
|
490
|
-
|
491
|
-
it 'should raise exception for invalid type' do
|
492
|
-
expect { builder.items('a') }.to \
|
493
|
-
raise_error Compel::TypeError, "#items must be a valid Schema"
|
494
|
-
end
|
495
|
-
|
496
|
-
it 'should have value' do
|
497
|
-
builder.items(Compel.integer)
|
498
|
-
|
499
|
-
expect(builder.options[:items].class).to be(Compel::Builder::Integer)
|
500
|
-
end
|
501
|
-
|
502
736
|
it 'should validate all items' do
|
503
|
-
|
504
|
-
|
505
|
-
result = builder.validate([1, '2', nil])
|
737
|
+
result = Compel.array.items(Compel.integer).validate([1, '2', nil])
|
506
738
|
|
507
739
|
expect(result.valid?).to be true
|
508
740
|
expect(result.value).to eq([1, 2])
|
509
741
|
end
|
510
742
|
|
511
743
|
it 'should validate all items with errors' do
|
512
|
-
|
513
|
-
|
514
|
-
result = builder.validate([1, 'a', nil])
|
744
|
+
result = Compel.array.items(Compel.float.required).validate([1, 'a', nil])
|
515
745
|
|
516
746
|
expect(result.valid?).to be false
|
517
747
|
expect(result.errors['1']).to include("'a' is not a valid Float")
|
@@ -519,7 +749,7 @@ describe Compel::Builder do
|
|
519
749
|
end
|
520
750
|
|
521
751
|
it 'should coerce all hash items' do
|
522
|
-
builder.items(Compel.hash.keys({
|
752
|
+
builder = Compel.array.items(Compel.hash.keys({
|
523
753
|
a: Compel.string.required,
|
524
754
|
b: Compel.integer
|
525
755
|
}))
|
@@ -540,7 +770,7 @@ describe Compel::Builder do
|
|
540
770
|
end
|
541
771
|
|
542
772
|
it 'should coerce all hash items with errors' do
|
543
|
-
builder.items(Compel.hash.keys({
|
773
|
+
builder = Compel.array.items(Compel.hash.keys({
|
544
774
|
a: Compel.string.required,
|
545
775
|
b: Compel.string.format(/^abc$/).required
|
546
776
|
}))
|
@@ -569,7 +799,7 @@ describe Compel::Builder do
|
|
569
799
|
end
|
570
800
|
|
571
801
|
it 'should coerce array with hash items and nested array keys with errors' do
|
572
|
-
builder.items(Compel.hash.keys({
|
802
|
+
builder = Compel.array.items(Compel.hash.keys({
|
573
803
|
a: Compel.string,
|
574
804
|
b: Compel.array.items(Compel.integer.required).required
|
575
805
|
}))
|
@@ -626,16 +856,14 @@ describe Compel::Builder do
|
|
626
856
|
|
627
857
|
it 'should validate with errors' do
|
628
858
|
value = [1, 2, 3]
|
629
|
-
|
630
|
-
result = schema.validate([1, 2])
|
859
|
+
result = Compel.array.is(value).validate([1, 2])
|
631
860
|
|
632
861
|
expect(result.valid?).to be false
|
633
862
|
expect(result.errors[:base]).to include("must be #{value}")
|
634
863
|
end
|
635
864
|
|
636
865
|
it 'should validate without errors' do
|
637
|
-
|
638
|
-
result = schema.validate(['a', 'b', 'c'])
|
866
|
+
result = Compel.array.is(['a', 'b', 'c']).validate(['a', 'b', 'c'])
|
639
867
|
|
640
868
|
expect(result.valid?).to be true
|
641
869
|
end
|
@@ -644,34 +872,14 @@ describe Compel::Builder do
|
|
644
872
|
|
645
873
|
end
|
646
874
|
|
647
|
-
context '
|
875
|
+
context 'DateTime' do
|
648
876
|
|
649
|
-
|
650
|
-
|
651
|
-
it 'should validate empty keys option' do
|
652
|
-
schema = builder.required
|
653
|
-
|
654
|
-
expect(schema.validate({ a: 1 }).valid?).to be true
|
655
|
-
end
|
656
|
-
|
657
|
-
it 'should validate nil' do
|
658
|
-
schema = builder.required
|
659
|
-
|
660
|
-
result = schema.validate(nil)
|
661
|
-
|
662
|
-
expect(result.valid?).to be false
|
663
|
-
expect(result.errors[:base]).to \
|
664
|
-
include('is required')
|
665
|
-
end
|
666
|
-
|
667
|
-
it 'should validate empty keys with errors' do
|
668
|
-
schema = builder.required.length(2)
|
669
|
-
|
670
|
-
result = schema.validate({ a: 1 })
|
877
|
+
it 'should validate with errors' do
|
878
|
+
result = Compel.datetime.validate('1989-0')
|
671
879
|
|
672
880
|
expect(result.valid?).to be false
|
673
|
-
expect(result.errors
|
674
|
-
include('
|
881
|
+
expect(result.errors).to \
|
882
|
+
include("'1989-0' is not a parsable datetime with format: %FT%T")
|
675
883
|
end
|
676
884
|
|
677
885
|
end
|
@@ -27,23 +27,23 @@ describe Compel::Validation do
|
|
27
27
|
|
28
28
|
end
|
29
29
|
|
30
|
-
context 'in,
|
30
|
+
context 'in, range' do
|
31
31
|
|
32
|
-
def
|
33
|
-
[:in, :
|
32
|
+
def expect_be_in_range(range, value)
|
33
|
+
[:in, :range].each do |key|
|
34
34
|
errors = Compel::Validation.validate(value, Compel::Coercion::String, { key => range })
|
35
35
|
yield errors
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should validate without errors' do
|
40
|
-
|
40
|
+
expect_be_in_range(['PT', 'UK'], 'PT') do |errors|
|
41
41
|
expect(errors.empty?).to eq(true)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should validate with errors' do
|
46
|
-
|
46
|
+
expect_be_in_range(['PT', 'UK'], 'US') do |errors|
|
47
47
|
expect(errors).to include('must be within ["PT", "UK"]')
|
48
48
|
end
|
49
49
|
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
|
+
version: 0.3.7
|
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-
|
11
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- lib/compel/coercion/types/array.rb
|
105
105
|
- lib/compel/coercion/types/boolean.rb
|
106
106
|
- lib/compel/coercion/types/date.rb
|
107
|
+
- lib/compel/coercion/types/date_type.rb
|
107
108
|
- lib/compel/coercion/types/datetime.rb
|
108
109
|
- lib/compel/coercion/types/float.rb
|
109
110
|
- lib/compel/coercion/types/hash.rb
|