Skip to content

Commit

Permalink
[Fix #608] skip strict validations
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Dec 31, 2021
1 parent 210c368 commit bb5168d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#610](https://github.com/rubocop/rubocop-rails/pull/610): Fix autocorrection of strict validation for `Rails/RedundantPresenceValidationOnBelongsTo` cop. ([@pirj][])
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ class RedundantPresenceValidationOnBelongsTo < Base
#
# @example source that matches - by a foreign key
# validates :user_id, presence: true
#
# @example source that DOES NOT match - strict validation
# validates :user_id, strict: true
#
# @example source that DOES NOT match - custom strict validation
# validates :user_id, strict: MissingUserError
def_node_matcher :presence_validation?, <<~PATTERN
$(
send nil? :validates
(sym $_)
...
$(hash <$(pair (sym :presence) {true hash}) ...>)
$[
(hash <$(pair (sym :presence) {true hash}) ...>) # presence: true
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
]
)
PATTERN

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@
validates :author, presence: true
RUBY
end

it 'does not register an offense with strict validation' do
expect_no_offenses(<<~RUBY)
belongs_to :user
validates :user, presence: true, strict: true
RUBY
end

it 'does not register an offense with strict validation with an explicit exception class' do
expect_no_offenses(<<~RUBY)
belongs_to :user
validates :user, presence: true, strict: MissingUserError
RUBY
end
end

context 'Rails < 5.0', :rails42 do
Expand Down

0 comments on commit bb5168d

Please sign in to comment.