Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #608] skip strict validations #610

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#608](https://github.com/rubocop/rubocop-rails/issues/608): Fix autocorrection of strict validation for `Rails/RedundantPresenceValidationOnBelongsTo` cop. ([@pirj][])
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ 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, presence: true, strict: true
#
# @example source that DOES NOT match - custom strict validation
# validates :user_id, presence: true, 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 @@ -261,6 +261,20 @@ class Profile
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