heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
23.89k stars 5.54k forks source link

Rails 7: Double validation error for `validates_uniqueness_of` when attribute is encrypted #5597

Closed john-999 closed 1 year ago

john-999 commented 1 year ago

Environment

Current behavior

In the User model, I have an attribute (User.handle) that is encrypted and has a uniqueness validation:

class User < ApplicationRecord

  encrypts :handle, deterministic: true, downcase: true

  devise :confirmable, :database_authenticatable, :lockable, :registerable,
         :recoverable, :rememberable, :timeoutable, :trackable, :validatable

  validates_uniqueness_of :handle

end

When I attempt to trigger this validation error (by submitting a User with a value for this attribute that already exists in a different User), the uniqueness error message is displayed twice, instead of only once.

When I output and inspect this User's errors in the log file, I see that 1 error message contains the unencrypted value, and a 2nd error message is added for the encrypted value.

It looks like this:

#<ActiveModel::Errors [

#<ActiveModel::Error attribute=handle, type=taken, options={:if=>#<Proc:0x00007effd8949560 /home/user/Websites/my_app/app/models/user.rb:219>, :allow_blank=>true, :value=>"@john_doe"}>,

#<ActiveModel::Error attribute=handle, type=taken, options={:if=>#<Proc:0x00007effd8949560 /home/user/Websites/my_app/app/models/user.rb:219>, :allow_blank=>true, :value=>"{\"p\":\"aBWdk0jNztyFY2LckeYHMBw=\",\"h\":{\"iv\":\"70SlB049/Bxf9YGh\",\"at\":\"Q6YXbJDmb2XrqUnmkytuaA==\"}}"}>

]> 
carlosantoniodasilva commented 1 year ago

Can you replicate this without devise involved?

john-999 commented 1 year ago

Actually, yes. I happens also in a non-Devise-Controller (with all other conditions remaining the same).

Which means that I have to submit this to the Rails repo, I suppose. So I'm closing this issue.

JDrizzy commented 7 months ago

@john-999 did you end up raising this issue in the rails repo? I'm encountering a similar issue and can't see anything related besides from this post

john-999 commented 7 months ago

@JDrizzy See https://github.com/rails/rails/issues/48248 - and there is a merged pull request at the end.