validates_type 0.0.1 → 0.1.0
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 +4 -4
- data/README.md +8 -0
- data/lib/validates_type.rb +7 -1
- data/spec/active_model/types_spec.rb +52 -0
- data/spec/activerecord/types_spec.rb +61 -7
- data/validates_type.gemspec +3 -1
- data/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f49fb46c37a3674822b3d22fa2419aaf4c0fe481
|
4
|
+
data.tar.gz: dea07a48bc5642340721e3503423de1c4b7fe9e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18aada637c9cb2671fa7c1be8ff377f867df2802740b7022f2e548bc0a5958094e0de0576f98c41e1a6e758ddb78289d3cf07e672dbe70e4e64ba8cc5bf1a0d2
|
7
|
+
data.tar.gz: 4455148f55376189879f6e899de21447deeca450baa7cc5fd0b55b2dcf565ef4d9f06f48d6847fe3ad9426e04d78724cdeaeb8d6b1e3a06e3bce03f627466415
|
data/README.md
CHANGED
@@ -31,6 +31,12 @@ class Foo < ActiveRecord::Base
|
|
31
31
|
|
32
32
|
# validate that attribute :thing is a Float or nil
|
33
33
|
validates_type :thing, :float, allow_nil: true
|
34
|
+
|
35
|
+
# validate that attribute :when is a Time
|
36
|
+
validates_type :when, :time
|
37
|
+
|
38
|
+
# validate that attribute :birthday is a Date or blank
|
39
|
+
validates_type :birthday, :date, allow_blank: true
|
34
40
|
end
|
35
41
|
```
|
36
42
|
|
@@ -88,3 +94,5 @@ end
|
|
88
94
|
- `:integer`
|
89
95
|
- `:string`
|
90
96
|
- `:symbol`
|
97
|
+
- `:time`
|
98
|
+
- `:date`
|
data/lib/validates_type.rb
CHANGED
@@ -62,9 +62,11 @@ module ActiveModel
|
|
62
62
|
# to raise an error or the actual error to raise
|
63
63
|
# return: custom error, ActiveModel::StrictValidationFailed, or nil
|
64
64
|
def options_error(strict_error)
|
65
|
+
return if strict_error.nil?
|
66
|
+
|
65
67
|
if strict_error == true
|
66
68
|
ActiveModel::StrictValidationFailed
|
67
|
-
elsif strict_error.
|
69
|
+
elsif strict_error.ancestors.include?(Exception)
|
68
70
|
strict_error
|
69
71
|
end
|
70
72
|
end
|
@@ -88,6 +90,8 @@ module ActiveModel
|
|
88
90
|
:integer => Integer,
|
89
91
|
:string => String,
|
90
92
|
:symbol => Symbol,
|
93
|
+
:time => Time,
|
94
|
+
:date => Date,
|
91
95
|
}[symbol] || fail(ValidatesType::UnsupportedType,
|
92
96
|
"Unsupported type #{ symbol.to_s.camelize } given for validates_type.")
|
93
97
|
end
|
@@ -113,6 +117,8 @@ module ActiveModel
|
|
113
117
|
# - :integer
|
114
118
|
# - :string
|
115
119
|
# - :symbol
|
120
|
+
# - :time
|
121
|
+
# - :date
|
116
122
|
#
|
117
123
|
# class Foo
|
118
124
|
# include ActiveModel::Validations
|
@@ -188,6 +188,58 @@ describe 'ValidatesType' do
|
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
191
|
+
describe 'Date' do
|
192
|
+
|
193
|
+
subject { ActiveModel::TypeValidationTestClass.set_accessor_and_long_validator(:date) }
|
194
|
+
|
195
|
+
context 'field value is a Date' do
|
196
|
+
let(:value) { Date.new }
|
197
|
+
|
198
|
+
specify do
|
199
|
+
expect(subject).to be_valid
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'field value is not a Date' do
|
204
|
+
let(:value) { :foo }
|
205
|
+
specify do
|
206
|
+
expect(subject).to_not be_valid
|
207
|
+
end
|
208
|
+
|
209
|
+
specify do
|
210
|
+
subject.validate
|
211
|
+
expect(subject.errors).to_not be_empty
|
212
|
+
expect(subject.errors.messages[:attribute][0]).to match(/is expected to be a Date and is not/)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
describe 'Time' do
|
218
|
+
|
219
|
+
subject { ActiveModel::TypeValidationTestClass.set_accessor_and_long_validator(:time) }
|
220
|
+
|
221
|
+
context 'field value is a Time' do
|
222
|
+
let(:value) { Time.new }
|
223
|
+
|
224
|
+
specify do
|
225
|
+
expect(subject).to be_valid
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
context 'field value is not a Time' do
|
230
|
+
let(:value) { 123456 }
|
231
|
+
specify do
|
232
|
+
expect(subject).to_not be_valid
|
233
|
+
end
|
234
|
+
|
235
|
+
specify do
|
236
|
+
subject.validate
|
237
|
+
expect(subject.errors).to_not be_empty
|
238
|
+
expect(subject.errors.messages[:attribute][0]).to match(/is expected to be a Time and is not/)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
191
243
|
context 'passing in a custom message' do
|
192
244
|
subject { ActiveModel::TypeValidationTestClass.set_accessor_and_long_validator(:string, message: 'is not a String!') }
|
193
245
|
|
@@ -7,7 +7,7 @@ describe 'ValidatesType' do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
describe 'Array' do
|
10
|
-
|
10
|
+
drop_and_create_column_with_type(:string)
|
11
11
|
|
12
12
|
subject { TypeValidationTest.set_accessor_and_long_validator(:array) }
|
13
13
|
|
@@ -48,7 +48,7 @@ describe 'ValidatesType' do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
describe 'Boolean' do
|
51
|
-
|
51
|
+
drop_and_create_column_with_type(:boolean)
|
52
52
|
|
53
53
|
subject { TypeValidationTest.set_accessor_and_long_validator(:boolean) }
|
54
54
|
|
@@ -89,7 +89,7 @@ describe 'ValidatesType' do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
describe 'Float' do
|
92
|
-
|
92
|
+
drop_and_create_column_with_type(:float)
|
93
93
|
|
94
94
|
subject { TypeValidationTest.set_accessor_and_long_validator(:float) }
|
95
95
|
|
@@ -130,7 +130,7 @@ describe 'ValidatesType' do
|
|
130
130
|
end
|
131
131
|
|
132
132
|
describe 'Hash' do
|
133
|
-
|
133
|
+
drop_and_create_column_with_type(:string)
|
134
134
|
|
135
135
|
subject { TypeValidationTest.set_accessor_and_long_validator(:hash) }
|
136
136
|
|
@@ -171,7 +171,7 @@ describe 'ValidatesType' do
|
|
171
171
|
end
|
172
172
|
|
173
173
|
describe 'Integer' do
|
174
|
-
|
174
|
+
drop_and_create_column_with_type(:integer)
|
175
175
|
|
176
176
|
subject { TypeValidationTest.set_accessor_and_long_validator(:integer) }
|
177
177
|
|
@@ -212,7 +212,7 @@ describe 'ValidatesType' do
|
|
212
212
|
end
|
213
213
|
|
214
214
|
describe 'String' do
|
215
|
-
|
215
|
+
drop_and_create_column_with_type(:string)
|
216
216
|
|
217
217
|
subject { TypeValidationTest.set_accessor_and_long_validator(:string) }
|
218
218
|
|
@@ -252,7 +252,7 @@ describe 'ValidatesType' do
|
|
252
252
|
end
|
253
253
|
|
254
254
|
describe 'Symbol' do
|
255
|
-
|
255
|
+
drop_and_create_column_with_type(:string)
|
256
256
|
|
257
257
|
subject { TypeValidationTest.set_accessor_and_long_validator(:symbol) }
|
258
258
|
|
@@ -290,6 +290,60 @@ describe 'ValidatesType' do
|
|
290
290
|
end
|
291
291
|
end
|
292
292
|
end
|
293
|
+
|
294
|
+
describe 'Date' do
|
295
|
+
drop_and_create_column_with_type(:string)
|
296
|
+
|
297
|
+
subject { TypeValidationTest.set_accessor_and_long_validator(:date) }
|
298
|
+
|
299
|
+
context 'field value is a Date' do
|
300
|
+
let(:value) { Date.new }
|
301
|
+
|
302
|
+
specify do
|
303
|
+
expect(subject).to be_valid
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
context 'field value is not a Date' do
|
308
|
+
let(:value) { :foo }
|
309
|
+
specify do
|
310
|
+
expect(subject).to_not be_valid
|
311
|
+
end
|
312
|
+
|
313
|
+
specify do
|
314
|
+
subject.validate
|
315
|
+
expect(subject.errors).to_not be_empty
|
316
|
+
expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Date and is not/)
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
describe 'Time' do
|
322
|
+
drop_and_create_column_with_type(:string)
|
323
|
+
|
324
|
+
subject { TypeValidationTest.set_accessor_and_long_validator(:time) }
|
325
|
+
|
326
|
+
context 'field value is a Time' do
|
327
|
+
let(:value) { Time.new }
|
328
|
+
|
329
|
+
specify do
|
330
|
+
expect(subject).to be_valid
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
context 'field value is not a Time' do
|
335
|
+
let(:value) { 123456 }
|
336
|
+
specify do
|
337
|
+
expect(subject).to_not be_valid
|
338
|
+
end
|
339
|
+
|
340
|
+
specify do
|
341
|
+
subject.validate
|
342
|
+
expect(subject.errors).to_not be_empty
|
343
|
+
expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Time and is not/)
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
293
347
|
end
|
294
348
|
|
295
349
|
context 'unsupported types' do
|
data/validates_type.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = 'validates_type'
|
7
7
|
s.version = ValidatesType::VERSION
|
8
8
|
s.summary = %q{Type validation for ActiveModel/ActiveRecord attributes}
|
9
|
-
s.description = %q{}
|
9
|
+
s.description = %q{This library helps validate attributes to specific types in the same way that ActiveModel valiations work. Able to chain additional modifiers to each validation.}
|
10
10
|
s.authors = ['Jake Yesbeck']
|
11
11
|
s.email = 'yesbeckjs@gmail.com'
|
12
12
|
s.homepage = 'http://rubygems.org/gems/validates_type'
|
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
s.test_files = s.files.grep(/^spec\//)
|
18
18
|
|
19
|
+
s.required_ruby_version = '>= 1.8.7'
|
20
|
+
|
19
21
|
s.add_dependency 'ruby-boolean', '>= 1.0.0'
|
20
22
|
s.add_dependency 'activemodel', '>= 3.0.0'
|
21
23
|
|
data/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jake Yesbeck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-boolean
|
@@ -122,7 +122,9 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
-
description:
|
125
|
+
description: This library helps validate attributes to specific types in the same
|
126
|
+
way that ActiveModel valiations work. Able to chain additional modifiers to each
|
127
|
+
validation.
|
126
128
|
email: yesbeckjs@gmail.com
|
127
129
|
executables: []
|
128
130
|
extensions: []
|
@@ -161,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
161
163
|
requirements:
|
162
164
|
- - ">="
|
163
165
|
- !ruby/object:Gem::Version
|
164
|
-
version:
|
166
|
+
version: 1.8.7
|
165
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
168
|
requirements:
|
167
169
|
- - ">="
|