has_secure_token icon indicating copy to clipboard operation
has_secure_token copied to clipboard

Validations?

Open kmarekspartz opened this issue 9 years ago • 7 comments

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.

kmarekspartz avatar Aug 12 '15 07:08 kmarekspartz

Here's what I'm thinking: https://github.com/zeckalpha/rails/commit/1198e376235e02f4f85c8330ad30bd528b4c26de

kmarekspartz avatar Aug 18 '15 16:08 kmarekspartz

https://github.com/rails/rails/pull/21290

kmarekspartz avatar Aug 18 '15 16:08 kmarekspartz

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?.

fgbreel avatar Aug 28 '15 17:08 fgbreel

I think this should work.

validates_presence_of :token, on: :create

Sen-Zhang avatar Sep 14 '15 20:09 Sen-Zhang

@Sen-Zhang, I'll try that on my branch from rails/rails#21290

kmarekspartz avatar Sep 15 '15 14:09 kmarekspartz

That didn't seem to work for me.

kmarekspartz avatar Sep 15 '15 15:09 kmarekspartz

+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.

yusufali2205 avatar Jun 01 '17 19:06 yusufali2205