schema_validations 2.2.0 → 2.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f722b7a85eff3f3def8a6355153476f9801da846
4
- data.tar.gz: 1e6eb1107162cd7eb637473d59ebd1d4de2ec33e
3
+ metadata.gz: 4d32813f578be832c807834e301af6646a5de2e8
4
+ data.tar.gz: 8f6d05bc4a8ee9230d2db146823ffa87ac2b16a1
5
5
  SHA512:
6
- metadata.gz: b5dfa087e36e5a81ac6f3e38fa6912db50e43785bb0d6c46959d1c48dc1a208cf6108d5eabab8c255a159ca65ed5de5994f6519f039dcca155e3548ad7963534
7
- data.tar.gz: 102c7dda2cc81cb0c0dbbc0e5518ee6c14368b365ce34c0181c69405b7521964b764a978b2e3ca598446894d3ee54a01bd8cbfaff3e420a6974c2351db12733a
6
+ metadata.gz: e41b110b12a186b3cacce1280ae74279e83b8b8be5296cdcedd43fe27fa258609ce79226b6b8c9cf0b034ac78633c319d696a76c6cc67d119119defb396edabd
7
+ data.tar.gz: ae10f5ffc2434e9e0f4470225fed144f37bc217864f487c8b391df20836d43f42029136a75213b4117623199ec967a97f8f97f87bb804038885becdb2f090e22
data/README.md CHANGED
@@ -187,6 +187,11 @@ Earlier versions of SchemaValidations supported:
187
187
 
188
188
  ## Release Notes
189
189
 
190
+ ### 2.2.1
191
+
192
+ * Bug fix: don't create presence validation for `null: false` with a
193
+ default defined (#18, #49)
194
+
190
195
  ### 2.2.0
191
196
 
192
197
  * Works with AR 5.0. Thanks to [@plicjo](https://github.coms/plicjo).
@@ -1,3 +1,3 @@
1
1
  eval File.read File.expand_path('../../Gemfile.base', __FILE__)
2
2
 
3
- gem "activerecord", "~> 5.0.0"
3
+ gem "activerecord", "~> 5.0.1"
@@ -2,6 +2,7 @@ require 'valuable'
2
2
 
3
3
  require 'schema_plus_columns'
4
4
  require 'schema_validations/version'
5
+ require 'schema_validations/validators/not_nil_validator'
5
6
  require 'schema_validations/active_record/validations'
6
7
  require 'schema_validations/active_record/type'
7
8
 
@@ -123,7 +123,12 @@ module SchemaValidations
123
123
  if datatype == :boolean
124
124
  validate_logged :validates_inclusion_of, name, :in => [true, false], :message => :blank
125
125
  else
126
- validate_logged :validates_presence_of, name
126
+ if !column.default.nil? && column.default.blank?
127
+ validate_logged :validates_with, SchemaValidations::Validators::NotNilValidator, attributes: [name]
128
+ else
129
+ # Validate presence
130
+ validate_logged :validates_presence_of, name
131
+ end
127
132
  end
128
133
  end
129
134
 
@@ -0,0 +1,11 @@
1
+ module SchemaValidations
2
+ module Validators
3
+ # Validates that the field is not nil?
4
+ # (Unlike the standard PresenceValidator which uses #blank?)
5
+ class NotNilValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attr_name, value)
7
+ record.errors.add(attr_name, :blank, options) if value.nil?
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module SchemaValidations
2
- VERSION = "2.2.0"
2
+ VERSION = "2.2.1"
3
3
  end
@@ -5,34 +5,34 @@ describe "Validations" do
5
5
  before(:each) do
6
6
  ActiveRecord::Schema.define do
7
7
 
8
- create_table :articles, :force => true do |t|
9
- t.string :title, :limit => 50
10
- t.text :content, :null => false
8
+ create_table :articles, force: true do |t|
9
+ t.string :title, limit: 50
10
+ t.text :content, null: false
11
11
  t.integer :state
12
12
  t.integer :votes
13
- t.float :average_mark, :null => false
14
- t.boolean :active, :null => false
15
- t.decimal :max10, :precision => 2, :scale => 1
16
- t.decimal :arbitrary, :precision => nil, :scale => nil
17
- t.decimal :max100, :precision => 2, :scale => nil
13
+ t.float :average_mark, null: false
14
+ t.boolean :active, null: false
15
+ t.decimal :max10, precision: 2, scale: 1
16
+ t.decimal :arbitrary, precision: nil, scale: nil
17
+ t.decimal :max100, precision: 2, scale: nil
18
18
  end
19
- add_index :articles, :title, :unique => true
20
- add_index :articles, [:state, :active], :unique => true
19
+ add_index :articles, :title, unique: true
20
+ add_index :articles, [:state, :active], unique: true
21
21
 
22
- create_table :reviews, :force => true do |t|
23
- t.integer :article_id, :null => false
24
- t.string :author, :null => false
25
- t.string :content, :limit => 200
22
+ create_table :reviews, force: true do |t|
23
+ t.integer :article_id, null: false
24
+ t.string :author, null: false
25
+ t.string :content, limit: 200
26
26
  t.string :type
27
- t.timestamps :null => false
27
+ t.timestamps null: false
28
28
  end
29
- add_index :reviews, :article_id, :unique => true
29
+ add_index :reviews, :article_id, unique: true
30
30
 
31
- create_table :article_reviews, :force => true do |t|
31
+ create_table :article_reviews, force: true do |t|
32
32
  t.integer :article_id
33
33
  t.integer :review_id
34
34
  end
35
- add_index :article_reviews, [:article_id, :review_id], :unique => true
35
+ add_index :article_reviews, [:article_id, :review_id], unique: true
36
36
  end
37
37
  end
38
38
 
@@ -43,8 +43,8 @@ describe "Validations" do
43
43
 
44
44
  class Review < ActiveRecord::Base
45
45
  belongs_to :article
46
- belongs_to :news_article, :class_name => 'Article', :foreign_key => :article_id
47
- schema_validations :except => :content
46
+ belongs_to :news_article, class_name: 'Article', foreign_key: :article_id
47
+ schema_validations except: :content
48
48
  end
49
49
 
50
50
  class ArticleReview < ActiveRecord::Base
@@ -81,15 +81,15 @@ describe "Validations" do
81
81
  end
82
82
 
83
83
  it "should check title length" do
84
- expect(Article.new(:title => 'a' * 100).error_on(:title).size).to eq(1)
84
+ expect(Article.new(title: 'a' * 100).error_on(:title).size).to eq(1)
85
85
  end
86
86
 
87
87
  it "should validate state numericality" do
88
- expect(Article.new(:state => 'unknown').error_on(:state).size).to eq(1)
88
+ expect(Article.new(state: 'unknown').error_on(:state).size).to eq(1)
89
89
  end
90
90
 
91
91
  it "should validate if state is integer" do
92
- expect(Article.new(:state => 1.23).error_on(:state).size).to eq(1)
92
+ expect(Article.new(state: 1.23).error_on(:state).size).to eq(1)
93
93
  end
94
94
 
95
95
  it "should validate the range of votes" do
@@ -115,28 +115,28 @@ describe "Validations" do
115
115
  expect(Article.new(max100: -100).error_on(:max100).size).to eq(1)
116
116
  end
117
117
 
118
- it "should not validate the range of arbitrary decimal", :mysql => :skip do # mysql provides a default precision
118
+ it "should not validate the range of arbitrary decimal", mysql: :skip do # mysql provides a default precision
119
119
  expect(Article.new(arbitrary: Float::MAX).error_on(:arbitrary).size).to eq(0)
120
120
  end
121
121
 
122
122
  it "should validate average_mark numericality" do
123
- expect(Article.new(:average_mark => "high").error_on(:average_mark).size).to eq(1)
123
+ expect(Article.new(average_mark: "high").error_on(:average_mark).size).to eq(1)
124
124
  end
125
125
 
126
126
  it "should validate boolean fields" do
127
- expect(Article.new(:active => nil).error_on(:active).size).to eq(1)
127
+ expect(Article.new(active: nil).error_on(:active).size).to eq(1)
128
128
  end
129
129
 
130
130
  it "should validate title uniqueness" do
131
131
  article1 = Article.create(valid_article_attributes)
132
- article2 = Article.new(:title => valid_article_attributes[:title])
132
+ article2 = Article.new(title: valid_article_attributes[:title])
133
133
  expect(article2.error_on(:title).size).to eq(1)
134
134
  article1.destroy
135
135
  end
136
136
 
137
137
  it "should validate state uniqueness in scope of 'active' value" do
138
138
  article1 = Article.create(valid_article_attributes)
139
- article2 = Article.new(valid_article_attributes.merge(:title => 'SchemaPlus 2.0 released'))
139
+ article2 = Article.new(valid_article_attributes.merge(title: 'SchemaPlus 2.0 released'))
140
140
  expect(article2).not_to be_valid
141
141
  article2.toggle(:active)
142
142
  expect(article2).to be_valid
@@ -151,9 +151,9 @@ describe "Validations" do
151
151
  it "should validate uniqueness of belongs_to association" do
152
152
  article = Article.create(valid_article_attributes)
153
153
  expect(article).to be_valid
154
- review1 = Review.create(:article => article, :author => 'michal')
154
+ review1 = Review.create(article: article, author: 'michal')
155
155
  expect(review1).to be_valid
156
- review2 = Review.new(:article => article, :author => 'michal')
156
+ review2 = Review.new(article: article, author: 'michal')
157
157
  expect(review2.error_on(:article_id).size).to be >= 1
158
158
  end
159
159
 
@@ -162,18 +162,81 @@ describe "Validations" do
162
162
  end
163
163
 
164
164
  it "should not validate uniqueness when scope is absent" do
165
- article_review_1 = ArticleReview.create(:article_id => 1, :review_id => nil)
165
+ article_review_1 = ArticleReview.create(article_id: 1, review_id: nil)
166
166
  expect(article_review_1).to be_valid
167
167
 
168
- article_review_2 = ArticleReview.create(:article_id => 1, :review_id => nil)
168
+ article_review_2 = ArticleReview.create(article_id: 1, review_id: nil)
169
169
  expect(article_review_2).to be_valid
170
170
 
171
- article_review_3 = ArticleReview.create(:article_id => nil, :review_id => 1)
171
+ article_review_3 = ArticleReview.create(article_id: nil, review_id: 1)
172
172
  expect(article_review_3).to be_valid
173
173
 
174
- article_review_4 = ArticleReview.create(:article_id => nil, :review_id => 1)
174
+ article_review_4 = ArticleReview.create(article_id: nil, review_id: 1)
175
175
  expect(article_review_4).to be_valid
176
176
  end
177
+
178
+ context 'when NOT NULL validations' do
179
+ before(:each) do
180
+ ActiveRecord::Schema.define do
181
+ create_table :anti_nulls, force: true do |t|
182
+ t.string :no_default, null: false
183
+ t.string :blank_default, default: '', null: false
184
+ t.string :non_blank_default, default: 'not blank', null: false
185
+ end
186
+ end
187
+ with_auto_validations do
188
+ class AntiNull < ActiveRecord::Base
189
+ def self.all_blank
190
+ @all_blank ||= AntiNull.new(
191
+ no_default: '',
192
+ blank_default: '',
193
+ non_blank_default: ''
194
+ )
195
+ end
196
+
197
+ def self.all_non_blank
198
+ @all_non_blank ||= AntiNull.new(
199
+ no_default: 'foo',
200
+ blank_default: 'bar',
201
+ non_blank_default: 'baz'
202
+ )
203
+ end
204
+
205
+ def self.all_nil
206
+ @all_nil ||= AntiNull.new(
207
+ no_default: nil,
208
+ blank_default: nil,
209
+ non_blank_default: nil
210
+ )
211
+ end
212
+
213
+ def self.non_null_with(**fields)
214
+ opts = { no_default: 'foo' }.merge!(fields)
215
+ AntiNull.new **opts
216
+ end
217
+ end
218
+ end
219
+
220
+
221
+ end
222
+
223
+ it 'should fail validation on empty fields only if the default value is not blank' do
224
+ expect(AntiNull.all_nil.error_on(:no_default).size).to eq(1)
225
+ expect(AntiNull.all_nil.error_on(:blank_default).size).to eq(1)
226
+ expect(AntiNull.all_nil.error_on(:non_blank_default).size).to eq(1)
227
+ end
228
+
229
+ it 'should fail validation on empty fields only if the default value is not blank' do
230
+ expect(AntiNull.all_blank.error_on(:no_default).size).to eq(1)
231
+ expect(AntiNull.all_blank.error_on(:non_blank_default).size).to eq(1)
232
+ expect(AntiNull.all_blank.error_on(:blank_default)).to be_empty
233
+ end
234
+
235
+ it 'should not fail if fields are neither nil nor empty' do
236
+ expect(AntiNull.all_non_blank).to be_valid
237
+ end
238
+
239
+ end
177
240
  end
178
241
 
179
242
  context "auto-created but changed" do
@@ -182,62 +245,62 @@ describe "Validations" do
182
245
  class Article < ActiveRecord::Base ; end
183
246
  class Review < ActiveRecord::Base
184
247
  belongs_to :article
185
- belongs_to :news_article, :class_name => 'Article', :foreign_key => :article_id
248
+ belongs_to :news_article, class_name: 'Article', foreign_key: :article_id
186
249
  end
187
250
  end
188
251
  @too_big_content = 'a' * 1000
189
252
  end
190
253
 
191
254
  it "would normally have an error" do
192
- @review = Review.new(:content => @too_big_content)
255
+ @review = Review.new(content: @too_big_content)
193
256
  expect(@review.error_on(:content).size).to eq(1)
194
257
  expect(@review.error_on(:author).size).to eq(1)
195
258
  end
196
259
 
197
260
  it "shouldn't validate fields passed to :except option" do
198
- Review.schema_validations :except => :content
199
- @review = Review.new(:content => @too_big_content)
261
+ Review.schema_validations except: :content
262
+ @review = Review.new(content: @too_big_content)
200
263
  expect(@review.errors_on(:content).size).to eq(0)
201
264
  expect(@review.error_on(:author).size).to eq(1)
202
265
  end
203
266
 
204
267
  it "shouldn't validate the fields in default whitelist" do
205
- Review.schema_validations :except => :content
268
+ Review.schema_validations except: :content
206
269
  expect(Review.new.error_on(:updated_at).size).to eq(0)
207
270
  expect(Review.new.error_on(:created_at).size).to eq(0)
208
271
  end
209
272
 
210
273
  it "shouldn't validate the fields in whitelist" do
211
- Review.schema_validations :except => :content, whitelist: [:updated_at]
274
+ Review.schema_validations except: :content, whitelist: [:updated_at]
212
275
  expect(Review.new.error_on(:updated_at).size).to eq(0)
213
276
  expect(Review.new.error_on(:created_at).size).to eq(1)
214
277
  end
215
278
 
216
279
  it "shouldn't validate types passed to :except_type option using full validation" do
217
- Review.schema_validations :except_type => :validates_length_of
218
- @review = Review.new(:content => @too_big_content)
280
+ Review.schema_validations except_type: :validates_length_of
281
+ @review = Review.new(content: @too_big_content)
219
282
  expect(@review.errors_on(:content).size).to eq(0)
220
283
  expect(@review.error_on(:author).size).to eq(1)
221
284
  end
222
285
 
223
286
  it "shouldn't validate types passed to :except_type option using shorthand" do
224
- Review.schema_validations :except_type => :length
225
- @review = Review.new(:content => @too_big_content)
287
+ Review.schema_validations except_type: :length
288
+ @review = Review.new(content: @too_big_content)
226
289
  expect(@review.errors_on(:content).size).to eq(0)
227
290
  expect(@review.error_on(:author).size).to eq(1)
228
291
  end
229
292
 
230
293
  it "should only validate type passed to :only_type option" do
231
- Review.schema_validations :only_type => :length
232
- @review = Review.new(:content => @too_big_content)
294
+ Review.schema_validations only_type: :length
295
+ @review = Review.new(content: @too_big_content)
233
296
  expect(@review.error_on(:content).size).to eq(1)
234
297
  expect(@review.errors_on(:author).size).to eq(0)
235
298
  end
236
299
 
237
300
 
238
301
  it "shouldn't create validations if locally disabled" do
239
- Review.schema_validations :auto_create => false
240
- @review = Review.new(:content => @too_big_content)
302
+ Review.schema_validations auto_create: false
303
+ @review = Review.new(content: @too_big_content)
241
304
  expect(@review.errors_on(:content).size).to eq(0)
242
305
  expect(@review.error_on(:author).size).to eq(0)
243
306
  end
@@ -251,23 +314,23 @@ describe "Validations" do
251
314
  before(:each) do
252
315
  class Review < ActiveRecord::Base
253
316
  belongs_to :article
254
- belongs_to :news_article, :class_name => 'Article', :foreign_key => :article_id
317
+ belongs_to :news_article, class_name: 'Article', foreign_key: :article_id
255
318
  end
256
319
  @too_big_content = 'a' * 1000
257
320
  end
258
321
 
259
322
  it "should not create validation" do
260
- expect(Review.new(:content => @too_big_title).errors_on(:content).size).to eq(0)
323
+ expect(Review.new(content: @too_big_title).errors_on(:content).size).to eq(0)
261
324
  end
262
325
 
263
326
  it "should create validation if locally enabled explicitly" do
264
- Review.schema_validations :auto_create => true
265
- expect(Review.new(:content => @too_big_content).error_on(:content).size).to eq(1)
327
+ Review.schema_validations auto_create: true
328
+ expect(Review.new(content: @too_big_content).error_on(:content).size).to eq(1)
266
329
  end
267
330
 
268
331
  it "should create validation if locally enabled implicitly" do
269
332
  Review.schema_validations
270
- expect(Review.new(:content => @too_big_content).error_on(:content).size).to eq(1)
333
+ expect(Review.new(content: @too_big_content).error_on(:content).size).to eq(1)
271
334
  end
272
335
 
273
336
  end
@@ -275,18 +338,18 @@ describe "Validations" do
275
338
  context "manually invoked" do
276
339
  before(:each) do
277
340
  class Article < ActiveRecord::Base ; end
278
- Article.schema_validations :only => [:title, :state]
341
+ Article.schema_validations only: [:title, :state]
279
342
 
280
343
  class Review < ActiveRecord::Base
281
344
  belongs_to :dummy_association
282
- schema_validations :except => :content
345
+ schema_validations except: :content
283
346
  end
284
347
  end
285
348
 
286
349
  it "should validate fields passed to :only option" do
287
350
  too_big_title = 'a' * 100
288
351
  wrong_state = 'unknown'
289
- article = Article.new(:title => too_big_title, :state => wrong_state)
352
+ article = Article.new(title: too_big_title, state: wrong_state)
290
353
  expect(article.error_on(:title).size).to eq(1)
291
354
  expect(article.error_on(:state).size).to eq(1)
292
355
  end
@@ -317,7 +380,7 @@ describe "Validations" do
317
380
  belongs_to :article
318
381
  end
319
382
  @columns = Review.content_columns.dup
320
- Review.schema_validations :only => [:title]
383
+ Review.schema_validations only: [:title]
321
384
  end
322
385
 
323
386
  it "shouldn't validate associations not included in :only option" do
@@ -352,9 +415,9 @@ describe "Validations" do
352
415
  context "when used with enum" do
353
416
  it "does not validate numericality" do
354
417
  class Article < ActiveRecord::Base
355
- enum :state => [:happy, :sad]
418
+ enum state: [:happy, :sad]
356
419
  end
357
- expect(Article.new(valid_article_attributes.merge(:state => :happy))).to be_valid
420
+ expect(Article.new(valid_article_attributes.merge(state: :happy))).to be_valid
358
421
  end
359
422
  end if ActiveRecord::Base.respond_to? :enum
360
423
 
@@ -366,11 +429,11 @@ describe "Validations" do
366
429
  context 'without scope' do
367
430
  before do
368
431
  ActiveRecord::Schema.define do
369
- create_table :books, :force => true do |t|
432
+ create_table :books, force: true do |t|
370
433
  t.string :title
371
434
  end
372
435
 
373
- add_index :books, :title, :unique => true
436
+ add_index :books, :title, unique: true
374
437
  end
375
438
 
376
439
  with_auto_validations do
@@ -390,12 +453,12 @@ describe "Validations" do
390
453
  context 'within a scope' do
391
454
  before do
392
455
  ActiveRecord::Schema.define do
393
- create_table :folders, :force => true do |t|
456
+ create_table :folders, force: true do |t|
394
457
  t.integer :parent_id
395
458
  t.string :name
396
459
  end
397
460
 
398
- add_index :folders, [:parent_id, :name], :unique => true
461
+ add_index :folders, [:parent_id, :name], unique: true
399
462
  end
400
463
 
401
464
  with_auto_validations do
@@ -408,10 +471,10 @@ describe "Validations" do
408
471
  it "should validate the uniqueness in a case insensitive manner" do
409
472
  mixed_case_name = 'Schema Validations'
410
473
  parent_folder = Folder.create
411
- Folder.create(:parent => parent_folder, :name => mixed_case_name)
474
+ Folder.create(parent: parent_folder, name: mixed_case_name)
412
475
 
413
- expect(Folder.new(:parent => parent_folder, :name => mixed_case_name)).not_to be_valid
414
- expect(Folder.new(:parent => parent_folder, :name => mixed_case_name.downcase)).not_to be_valid
476
+ expect(Folder.new(parent: parent_folder, name: mixed_case_name)).not_to be_valid
477
+ expect(Folder.new(parent: parent_folder, name: mixed_case_name.downcase)).not_to be_valid
415
478
  end
416
479
  end
417
480
  end
@@ -419,7 +482,7 @@ describe "Validations" do
419
482
  context 'with optimistic locking' do
420
483
  before do
421
484
  ActiveRecord::Schema.define do
422
- create_table :optimistics, :force => true do |t|
485
+ create_table :optimistics, force: true do |t|
423
486
  t.integer :lock_version
424
487
  end
425
488
  end
@@ -448,11 +511,11 @@ describe "Validations" do
448
511
 
449
512
  def valid_article_attributes
450
513
  {
451
- :title => 'SchemaPlus released!',
452
- :content => "Database matters. Get full use of it but don't write unecessary code. Get SchemaPlus!",
453
- :state => 3,
454
- :average_mark => 9.78,
455
- :active => true
514
+ title: 'SchemaPlus released!',
515
+ content: "Database matters. Get full use of it but don't write unecessary code. Get SchemaPlus!",
516
+ state: 3,
517
+ average_mark: 9.78,
518
+ active: true
456
519
  }
457
520
  end
458
521
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-25 00:00:00.000000000 Z
12
+ date: 2017-02-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: schema_plus_columns
@@ -188,6 +188,7 @@ files:
188
188
  - lib/schema_validations/active_record/type.rb
189
189
  - lib/schema_validations/active_record/validations.rb
190
190
  - lib/schema_validations/railtie.rb
191
+ - lib/schema_validations/validators/not_nil_validator.rb
191
192
  - lib/schema_validations/version.rb
192
193
  - schema_dev.yml
193
194
  - schema_validations.gemspec