has_secure_token
has_secure_token copied to clipboard
Validations?
I've yet to check what upstream is doing, but would a before_validation hook be more appropriate than a before_create hook? Currently things like validates_presence_of :token
don't work.
Here's what I'm thinking: https://github.com/zeckalpha/rails/commit/1198e376235e02f4f85c8330ad30bd528b4c26de
https://github.com/rails/rails/pull/21290
I think this is a valid issue, as has_secure_password
has a way to validate using:
require 'rails_helper'
RSpec.describe User, type: :model do
describe "secure password" do
it { is_expected.to have_secure_password }
end
end
I think will be great to have this matchers for has_secure_token
as well using has_secure_token?
.
I think this should work.
validates_presence_of :token, on: :create
@Sen-Zhang, I'll try that on my branch from rails/rails#21290
That didn't seem to work for me.
+1 to this change. I ran into an issue where I was doing validates_uniqueness_of :token and the validation was running against empty string ('') since the token value was not generated yet. I want to run this validation here instead of creating a unique index on this column because my database already has duplicate values in token rows.