Skip to content

Commit 0da37d8

Browse files
committed
[Fix #608] skip strict validations
1 parent 377bf82 commit 0da37d8

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#608](https://github.com/rubocop/rubocop-rails/issues/608): Fix autocorrection of strict validation for `Rails/RedundantPresenceValidationOnBelongsTo` cop. ([@pirj][])

lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,20 @@ class RedundantPresenceValidationOnBelongsTo < Base
5151
#
5252
# @example source that matches - by a foreign key
5353
# validates :user_id, presence: true
54+
#
55+
# @example source that DOES NOT match - strict validation
56+
# validates :user_id, presence: true, strict: true
57+
#
58+
# @example source that DOES NOT match - custom strict validation
59+
# validates :user_id, presence: true, strict: MissingUserError
5460
def_node_matcher :presence_validation?, <<~PATTERN
5561
(
5662
send nil? :validates
5763
(sym $_)+
58-
$(hash <$(pair (sym :presence) {true hash}) ...>)
64+
$[
65+
(hash <$(pair (sym :presence) {true hash}) ...>) # presence: true
66+
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
67+
]
5968
)
6069
PATTERN
6170

spec/rubocop/cop/rails/redundant_presence_validation_on_belongs_to_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ class Profile
261261
validates :author, presence: true
262262
RUBY
263263
end
264+
265+
it 'does not register an offense with strict validation' do
266+
expect_no_offenses(<<~RUBY)
267+
belongs_to :user
268+
validates :user, presence: true, strict: true
269+
RUBY
270+
end
271+
272+
it 'does not register an offense with strict validation with an explicit exception class' do
273+
expect_no_offenses(<<~RUBY)
274+
belongs_to :user
275+
validates :user, presence: true, strict: MissingUserError
276+
RUBY
277+
end
264278
end
265279

266280
context 'Rails < 5.0', :rails42 do

0 commit comments

Comments
 (0)