Skip to content

Commit fec67f9

Browse files
authored
Merge pull request #5734 from heartcombo/password-length-dynamic
Use proc to set password length validator so it's possible to override it dynamically.
2 parents dce20b7 + 560a1cb commit fec67f9

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@
1818
* Removed deprecations warning output for `Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION` (@soartec-lab)
1919
* Add Rails 8 support.
2020
- Routes are lazy-loaded by default in test and development environments now so Devise loads them before `Devise.mappings` call.
21+
* Password length validator is changed from
2122
23+
```
24+
validates_length_of :password, within: password_length, allow_blank: true`
25+
```
26+
27+
to
28+
29+
```
30+
validates_length_of :password, minimum: proc { password_length.min }, maximum: proc { password_length.max }, allow_blank: true
31+
```
32+
33+
so it's possible to override `password_length` at runtime. (@manojmj92)
2234
* bug fixes
2335
* Make `Devise` work without `ActionMailer` when `Zeitwerk` autoloader is used.
2436

lib/devise/models/validatable.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module Models
1414
# * +email_regexp+: the regular expression used to validate e-mails;
1515
# * +password_length+: a range expressing password length. Defaults to 6..128.
1616
#
17+
# Since +password_length+ is applied in a proc within `validates_length_of` it can be overridden
18+
# at runtime.
1719
module Validatable
1820
# All validations used by this module.
1921
VALIDATIONS = [:validates_presence_of, :validates_uniqueness_of, :validates_format_of,
@@ -34,7 +36,7 @@ def self.included(base)
3436

3537
validates_presence_of :password, if: :password_required?
3638
validates_confirmation_of :password, if: :password_required?
37-
validates_length_of :password, within: password_length, allow_blank: true
39+
validates_length_of :password, minimum: proc { password_length.min }, maximum: proc { password_length.max }, allow_blank: true
3840
end
3941
end
4042

test/models_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def assert_include_modules(klass, *modules)
2626
test 'validations options are not applied too late' do
2727
validators = WithValidation.validators_on :password
2828
length = validators.find { |v| v.kind == :length }
29-
assert_equal 2, length.options[:minimum]
30-
assert_equal 6, length.options[:maximum]
29+
assert_equal 2, length.options[:minimum].call
30+
assert_equal 6, length.options[:maximum].call
3131
end
3232

3333
test 'validations are applied just once' do

0 commit comments

Comments
 (0)