validates_type 0.0.1

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.
@@ -0,0 +1,307 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'ValidatesType' do
4
+ context 'supported types' do
5
+ before do
6
+ subject.test_attribute = value
7
+ end
8
+
9
+ describe 'Array' do
10
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:string)
11
+
12
+ subject { TypeValidationTest.set_accessor_and_long_validator(:array) }
13
+
14
+ context 'field value is an Array' do
15
+ let(:value) { [] }
16
+
17
+ specify do
18
+ expect(subject).to be_valid
19
+ end
20
+ end
21
+
22
+ context 'field value is not an Array' do
23
+ let(:value) { -1 }
24
+
25
+ specify do
26
+ expect(subject).to_not be_valid
27
+ end
28
+
29
+ specify do
30
+ subject.validate
31
+ expect(subject.errors).to_not be_empty
32
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Array and is not/)
33
+ end
34
+ end
35
+
36
+ context 'passing in a custom message' do
37
+ subject { TypeValidationTest.set_accessor_and_long_validator(:array, message: 'is not an Array!') }
38
+
39
+ context 'when validation fails' do
40
+ let(:value) { 1 }
41
+
42
+ specify do
43
+ subject.validate
44
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not an Array!/)
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ describe 'Boolean' do
51
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:boolean)
52
+
53
+ subject { TypeValidationTest.set_accessor_and_long_validator(:boolean) }
54
+
55
+ context 'field value is a Boolean' do
56
+ let(:value) { true }
57
+
58
+ specify do
59
+ expect(subject).to be_valid
60
+ end
61
+ end
62
+
63
+ context 'field value is not a Boolean' do
64
+ let(:value) { 'false' }
65
+
66
+ specify do
67
+ expect(subject).to_not be_valid
68
+ end
69
+
70
+ specify do
71
+ subject.validate
72
+ expect(subject.errors).to_not be_empty
73
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Boolean and is not/)
74
+ end
75
+ end
76
+
77
+ context 'passing in a custom message' do
78
+ subject { TypeValidationTest.set_accessor_and_long_validator(:boolean, message: 'is not a Boolean!') }
79
+
80
+ context 'when validation fails' do
81
+ let(:value) { 1 }
82
+
83
+ specify do
84
+ subject.validate
85
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a Boolean!/)
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ describe 'Float' do
92
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:float)
93
+
94
+ subject { TypeValidationTest.set_accessor_and_long_validator(:float) }
95
+
96
+ context 'field value is a Float' do
97
+ let(:value) { 1.0 }
98
+
99
+ specify do
100
+ expect(subject).to be_valid
101
+ end
102
+ end
103
+
104
+ context 'field value is not a Float' do
105
+ let(:value) { 'banana' }
106
+
107
+ specify do
108
+ expect(subject).to_not be_valid
109
+ end
110
+
111
+ specify do
112
+ subject.validate
113
+ expect(subject.errors).to_not be_empty
114
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Float and is not/)
115
+ end
116
+ end
117
+
118
+ context 'passing in a custom message' do
119
+ subject { TypeValidationTest.set_accessor_and_long_validator(:float, message: 'is not a Float!') }
120
+
121
+ context 'when validation fails' do
122
+ let(:value) { 1 }
123
+
124
+ specify do
125
+ subject.validate
126
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a Float!/)
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ describe 'Hash' do
133
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:string)
134
+
135
+ subject { TypeValidationTest.set_accessor_and_long_validator(:hash) }
136
+
137
+ context 'field value is a Hash' do
138
+ let(:value) { { :this => :here } }
139
+
140
+ specify do
141
+ expect(subject).to be_valid
142
+ end
143
+ end
144
+
145
+ context 'field value is not a Hash' do
146
+ let(:value) { 'banana' }
147
+
148
+ specify do
149
+ expect(subject).to_not be_valid
150
+ end
151
+
152
+ specify do
153
+ subject.validate
154
+ expect(subject.errors).to_not be_empty
155
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Hash and is not/)
156
+ end
157
+ end
158
+
159
+ context 'passing in a custom message' do
160
+ subject { TypeValidationTest.set_accessor_and_long_validator(:hash, message: 'is not a Hash!') }
161
+
162
+ context 'when validation fails' do
163
+ let(:value) { [] }
164
+
165
+ specify do
166
+ subject.validate
167
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a Hash!/)
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ describe 'Integer' do
174
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:integer)
175
+
176
+ subject { TypeValidationTest.set_accessor_and_long_validator(:integer) }
177
+
178
+ context 'field value is a Integer' do
179
+ let(:value) { 1 }
180
+
181
+ specify do
182
+ expect(subject).to be_valid
183
+ end
184
+ end
185
+
186
+ context 'field value is not a Integer' do
187
+ let(:value) { 'banana' }
188
+
189
+ specify do
190
+ expect(subject).to_not be_valid
191
+ end
192
+
193
+ specify do
194
+ subject.validate
195
+ expect(subject.errors).to_not be_empty
196
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Integer and is not/)
197
+ end
198
+ end
199
+
200
+ context 'passing in a custom message' do
201
+ subject { TypeValidationTest.set_accessor_and_long_validator(:integer, message: 'is not a Integer!') }
202
+
203
+ context 'when validation fails' do
204
+ let(:value) { [] }
205
+
206
+ specify do
207
+ subject.validate
208
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a Integer!/)
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ describe 'String' do
215
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:string)
216
+
217
+ subject { TypeValidationTest.set_accessor_and_long_validator(:string) }
218
+
219
+ context 'field value is a String' do
220
+ let(:value) { 'some string' }
221
+
222
+ specify do
223
+ expect(subject).to be_valid
224
+ end
225
+ end
226
+
227
+ context 'field value is not a String' do
228
+ let(:value) { -1 }
229
+ specify do
230
+ expect(subject).to_not be_valid
231
+ end
232
+
233
+ specify do
234
+ subject.validate
235
+ expect(subject.errors).to_not be_empty
236
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a String and is not/)
237
+ end
238
+ end
239
+
240
+ context 'passing in a custom message' do
241
+ subject { TypeValidationTest.set_accessor_and_long_validator(:string, message: 'is not a String!') }
242
+
243
+ context 'when validation fails' do
244
+ let(:value) { 1 }
245
+
246
+ specify do
247
+ subject.validate
248
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a String!/)
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ describe 'Symbol' do
255
+ ->(column_type){ drop_and_create_column_with_type(column_type) }.call(:string)
256
+
257
+ subject { TypeValidationTest.set_accessor_and_long_validator(:symbol) }
258
+
259
+ context 'field value is a Symbol' do
260
+ let(:value) { :something }
261
+
262
+ specify do
263
+ expect(subject).to be_valid
264
+ end
265
+ end
266
+
267
+ context 'field value is not a Symbol' do
268
+ let(:value) { -1 }
269
+ specify do
270
+ expect(subject).to_not be_valid
271
+ end
272
+
273
+ specify do
274
+ subject.validate
275
+ expect(subject.errors).to_not be_empty
276
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is expected to be a Symbol and is not/)
277
+ end
278
+ end
279
+
280
+ context 'passing in a custom message' do
281
+ subject { TypeValidationTest.set_accessor_and_long_validator(:symbol, message: 'is not a Symbol!') }
282
+
283
+ context 'when validation fails' do
284
+ let(:value) { 1 }
285
+
286
+ specify do
287
+ subject.validate
288
+ expect(subject.errors.messages[:test_attribute][0]).to match(/is not a Symbol!/)
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ context 'unsupported types' do
296
+ describe 'Foo' do
297
+ specify do
298
+ expect do
299
+ subject = TypeValidationTest.set_accessor_and_long_validator(:foo)
300
+ subject.valid?
301
+ end.to raise_error(
302
+ ValidatesType::UnsupportedType,
303
+ "Unsupported type Foo given for validates_type.")
304
+ end
305
+ end
306
+ end
307
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+ require_relative '../lib/arguments'
3
+
4
+ describe ValidatesType::Arguments do
5
+
6
+ let(:attribute_name) { :foo }
7
+ let(:attribute_type) { :string }
8
+ let(:options) { { extra: :options } }
9
+
10
+ subject { described_class.new(attribute_name, attribute_type, options) }
11
+
12
+ describe '#to_validation_attributes' do
13
+ context 'all pertinent attributes are present' do
14
+ specify do
15
+ expect(subject.to_validation_attributes).to eq([attribute_name, { type: attribute_type }.merge(options)])
16
+ end
17
+ end
18
+
19
+ context 'attribute_type is nil' do
20
+ let(:attribute_type) { nil }
21
+
22
+ specify do
23
+ expect(subject.to_validation_attributes).to eq([attribute_name, { type: attribute_type }.merge(options)])
24
+ end
25
+ end
26
+
27
+ context 'attribute_name is nil' do
28
+ let(:attribute_name) { nil }
29
+
30
+ specify do
31
+ expect(subject.to_validation_attributes).to eq([attribute_name, { type: attribute_type }.merge(options)])
32
+ end
33
+ end
34
+
35
+ context 'options is nil' do
36
+ let(:options) { nil }
37
+
38
+ specify do
39
+ expect(subject.to_validation_attributes).to eq([attribute_name, { type: attribute_type }])
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#type' do
45
+ context 'type is given' do
46
+ specify do
47
+ expect(subject.send(:type)).to eq({ type: attribute_type })
48
+ end
49
+ end
50
+
51
+ context 'type is nil' do
52
+ let(:attribute_type) { nil }
53
+
54
+ specify do
55
+ expect(subject.send(:type)).to eq({ type: nil })
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#merged_options' do
61
+ context 'options are present' do
62
+ specify do
63
+ expect(subject.send(:merged_options)).to eq({ type: attribute_type }.merge(options))
64
+ end
65
+ end
66
+
67
+ context 'options is nil' do
68
+ let(:options) { nil }
69
+
70
+ specify do
71
+ expect(subject.send(:merged_options)).to eq({ type: attribute_type })
72
+ end
73
+ end
74
+
75
+ context 'options is a blank hash' do
76
+ let(:options) { {} }
77
+
78
+ specify do
79
+ expect(subject.send(:merged_options)).to eq({ type: attribute_type })
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record'
2
+ require 'pry'
3
+ require_relative '../lib/validates_type'
4
+ require_relative './active_model_helper'
5
+ require_relative './active_record_helper'
6
+
7
+
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+ require_relative './version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'validates_type'
7
+ s.version = ValidatesType::VERSION
8
+ s.summary = %q{Type validation for ActiveModel/ActiveRecord attributes}
9
+ s.description = %q{}
10
+ s.authors = ['Jake Yesbeck']
11
+ s.email = 'yesbeckjs@gmail.com'
12
+ s.homepage = 'http://rubygems.org/gems/validates_type'
13
+ s.license = 'MIT'
14
+
15
+ s.require_paths = ['lib']
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = s.files.grep(/^spec\//)
18
+
19
+ s.add_dependency 'ruby-boolean', '>= 1.0.0'
20
+ s.add_dependency 'activemodel', '>= 3.0.0'
21
+
22
+ s.add_development_dependency 'bundler', '~> 1.3'
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'rspec'
25
+ s.add_development_dependency 'pry'
26
+ s.add_development_dependency 'activerecord', '>= 3.0.0'
27
+ s.add_development_dependency 'sqlite3'
28
+ end
data/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module ValidatesType
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_type
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jake Yesbeck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-boolean
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ''
126
+ email: yesbeckjs@gmail.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".gitignore"
132
+ - ".travis.yml"
133
+ - Gemfile
134
+ - Gemfile.lock
135
+ - LICENSE.txt
136
+ - README.md
137
+ - errors/unsupported_type.rb
138
+ - lib/arguments.rb
139
+ - lib/validates_type.rb
140
+ - spec/active_model/base_spec.rb
141
+ - spec/active_model/modifiers_spec.rb
142
+ - spec/active_model/types_spec.rb
143
+ - spec/active_model_helper.rb
144
+ - spec/active_record_helper.rb
145
+ - spec/activerecord/base_spec.rb
146
+ - spec/activerecord/modifiers_spec.rb
147
+ - spec/activerecord/types_spec.rb
148
+ - spec/arguments_spec.rb
149
+ - spec/spec_helper.rb
150
+ - validates_type.gemspec
151
+ - version.rb
152
+ homepage: http://rubygems.org/gems/validates_type
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.4.6
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Type validation for ActiveModel/ActiveRecord attributes
176
+ test_files:
177
+ - spec/active_model/base_spec.rb
178
+ - spec/active_model/modifiers_spec.rb
179
+ - spec/active_model/types_spec.rb
180
+ - spec/active_model_helper.rb
181
+ - spec/active_record_helper.rb
182
+ - spec/activerecord/base_spec.rb
183
+ - spec/activerecord/modifiers_spec.rb
184
+ - spec/activerecord/types_spec.rb
185
+ - spec/arguments_spec.rb
186
+ - spec/spec_helper.rb