Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit a6a2e47

Browse files
committed
docs: added PasswordStrength rule
1 parent d78f748 commit a6a2e47

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
- [Email](03-rules_email.md)
1919
- [Length](03-rules_length.md)
20+
- [PasswordStrength](03-rules_password-strength.md)
2021
- [URL](03-rules_url.md)
2122

2223
## Comparison Rules

docs/03-rules_password-strength.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# PasswordStrength
2+
3+
Validates that the given password has reached the minimum strength required by the constraint.
4+
The strength is calculated by measuring the entropy of the password (in bits) based on its length and the number of unique characters.
5+
6+
```php
7+
PasswordStrength(
8+
string $minStrength = 'medium',
9+
?string $minMessage = null
10+
);
11+
```
12+
13+
## Basic Usage
14+
15+
```php
16+
Validator::passwordStrength()->validate('password'); // false
17+
Validator::passwordStrength()->validate('i8Kq*MBob~2W"=p'); // true
18+
Validator::passwordStrength(minStrength: 'very-strong')->validate('i8Kq*MBob~2W"=p'); // false
19+
```
20+
21+
> [!NOTE]
22+
> An `UnexpectedValueException` will be thrown when a `minStrength` option is invalid.
23+
24+
> [!NOTE]
25+
> An `UnexpectedValueException` will be thrown when the input value is not a `string`.
26+
27+
## Options
28+
29+
### `minStrength`
30+
31+
type: `string` default: `medium`
32+
33+
Sets the minimum strength of the password in entropy bits.
34+
The entropy is calculated using the formula [here](https://www.pleacher.com/mp/mlessons/algebra/entropy.html).
35+
36+
Available options are:
37+
38+
- `weak` entropy between `64` and `79` bits.
39+
- `medium` entropy between `80` and `95` bits.
40+
- `strong` entropy between `96` and `127` bits.
41+
- `very-strong` entropy greater than `128` bits.
42+
43+
All measurements less than `64` bits will fail.
44+
45+
### `message`
46+
47+
type: `?string` default: `The password strength is not strong enough.`
48+
49+
Message that will be shown when the password is not strong enough.
50+
51+
The following parameters are available:
52+
53+
| Parameter | Description |
54+
|---------------------|---------------------------|
55+
| `{{ name }}` | Name of the invalid value |
56+
| `{{ minStrength }}` | Selected minimum strength |
57+
58+
## Changelog
59+
60+
- `0.8.0` Created

0 commit comments

Comments
 (0)