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

Commit 63cf347

Browse files
authored
Merge pull request #59 from programmatordev/1.x
1.x
2 parents ed0d636 + 1c2acad commit 63cf347

37 files changed

+676
-67
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ Simple usage looks like:
3939
use ProgrammatorDev\Validator\Rule;
4040
use ProgrammatorDev\Validator\Validator;
4141

42-
// do this...
43-
$validator = Validator::notBlank()->greaterThanOrEqual(18);
42+
// do this:
43+
$validator = Validator::type('int')->greaterThanOrEqual(18);
4444

45-
// ...and validate with these:
45+
// and validate with these:
4646
$validator->validate(16); // returns bool: false
4747
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
4848
```

docs/01-get-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ Simple usage looks like:
3030
use ProgrammatorDev\Validator\Rule;
3131
use ProgrammatorDev\Validator\Validator;
3232

33-
// do this...
34-
$validator = Validator::notBlank()->greaterThanOrEqual(18);
33+
// do this:
34+
$validator = Validator::type('int')->greaterThanOrEqual(18);
3535

36-
// ...and validate with these:
36+
// and validate with these:
3737
$validator->validate(16); // returns bool: false
3838
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
3939
```

docs/02-usage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getWeather(float $latitude, float $longitude, string $unitSystem
2222
{
2323
Validator::range(-90, 90)->assert($latitude, 'latitude');
2424
Validator::range(-180, 180)->assert($longitude, 'longitude');
25-
Validator::notBlank()->choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
25+
Validator::choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
2626

2727
// ...
2828
}
@@ -51,7 +51,7 @@ function getWeather(float $latitude, float $longitude, string $unitSystem): floa
5151
{
5252
Validator::range(-90, 90)->assert($latitude, 'latitude');
5353
Validator::range(-180, 180)->assert($longitude, 'longitude');
54-
Validator::notBlank()->choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
54+
Validator::choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
5555

5656
// ...
5757
}
@@ -98,7 +98,7 @@ use ProgrammatorDev\Validator\Validator;
9898
try {
9999
Validator::range(-90, 90)->assert($latitude, 'latitude');
100100
Validator::range(-180, 180)->assert($longitude, 'longitude');
101-
Validator::notBlank()->choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
101+
Validator::choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
102102
}
103103
catch (Exception\RangeException $exception) {
104104
// do something when Range fails
@@ -120,7 +120,7 @@ use ProgrammatorDev\Validator\Validator;
120120
try {
121121
Validator::range(-90, 90)->assert($latitude, 'latitude');
122122
Validator::range(-180, 180)->assert($longitude, 'longitude');
123-
Validator::notBlank()->choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
123+
Validator::choice(['metric', 'imperial'])->assert($unitSystem, 'unit system');
124124
}
125125
catch (ValidationException $exception) {
126126
// do something when a rule fails

docs/03-rules.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
- [Email](03-rules_email.md)
1919
- [Length](03-rules_length.md)
20+
- [PasswordStrength](03-rules_password-strength.md)
21+
- [Regex](03-rules_regex.md)
2022
- [URL](03-rules_url.md)
2123

2224
## Comparison Rules
@@ -29,6 +31,7 @@
2931

3032
## Date Rules
3133

34+
- [DateTime](03-rules_date-time.md)
3235
- [Timezone](03-rules_timezone.md)
3336

3437
## Choice Rules

docs/03-rules_date-time.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# DateTime
2+
3+
Validates that a given value is a valid datetime in a specific format.
4+
5+
```php
6+
DateTime(
7+
string $format = 'Y-m-d H:i:s',
8+
?string $message = null
9+
);
10+
```
11+
12+
## Basic Usage
13+
14+
```php
15+
// default "Y-m-d H:i:s"
16+
Validator::dateTime()->validate('2024-01-01 00:00:00'); // true
17+
Validator::dateTime()->validate('2024-01-01'); // false
18+
19+
// validate date
20+
Validator::dateTime(format: 'Y-m-d')->validate('2024-01-01'); // true
21+
Validator::dateTime(format: 'Y-m-d')->validate('2024-01-35'); // false
22+
23+
// validate time
24+
Validator::dateTime(format: 'H:i:s')->validate('21:00:00'); // true
25+
Validator::dateTime(format: 'H:i:s')->validate('35:00:00'); // false
26+
```
27+
28+
> [!NOTE]
29+
> An `UnexpectedValueException` will be thrown when the input value is not a `string` or an object implementing `\Stringable`.
30+
31+
## Options
32+
33+
### `format`
34+
35+
type: `string` default: `Y-m-d H:i:s`
36+
37+
Format of the datetime to be validated.
38+
Check all formatting options [here](https://www.php.net/manual/en/datetimeimmutable.createfromformat.php).
39+
40+
### `message`
41+
42+
type: `?string` default: `The {{ name }} value is not a valid datetime.`
43+
44+
Message that will be shown when the input value is not a valid datetime.
45+
46+
The following parameters are available:
47+
48+
| Parameter | Description |
49+
|----------------|---------------------------|
50+
| `{{ value }}` | The current invalid value |
51+
| `{{ name }}` | Name of the invalid value |
52+
| `{{ format }}` | The datetime format |
53+
54+
## Changelog
55+
56+
- `0.8.0` Created

docs/03-rules_length.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ Allows to define a `callable` that will be applied to the value before checking
9999
For example, use `trim`, or pass your own function, to not measure whitespaces at the end of a string:
100100

101101
```php
102-
// existing php callables
102+
Validator::length(max: 3)->validate('abc '); // false
103+
103104
Validator::length(max: 3, normalizer: 'trim')->validate('abc '); // true
104-
// function
105-
Validator::length(max: 3, normalizer: fn($value) => trim($value))->validate('abc '); // false
105+
Validator::length(max: 3, normalizer: fn($value) => trim($value))->validate('abc '); // true
106106
```
107107

108108
### `minMessage`

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 $message = 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

docs/03-rules_regex.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Regex
2+
3+
Validates that a given regular expression pattern is valid.
4+
5+
```php
6+
Regex(
7+
string $pattern,
8+
bool $match = true,
9+
?callable $normalizer = null,
10+
?string $message = null
11+
);
12+
```
13+
14+
## Basic Usage
15+
16+
```php
17+
Validator::regex('/[a-z]/')->validate('abc'); // true
18+
Validator::regex('/[a-z]/')->validate('123'); // false
19+
20+
// if match is false, assert that the pattern does not match
21+
// in this case, assert that the value does not contain any lowercase letters
22+
Validator::regex('/[a-z]/', match: false)->validate('abc'); // false
23+
Validator::regex('/[a-z]/', match: false)->validate('123'); // true
24+
```
25+
26+
> [!NOTE]
27+
> An `UnexpectedValueException` will be thrown if the `pattern` is not a valid regular expression.
28+
29+
> [!NOTE]
30+
> An `UnexpectedValueException` will be thrown when the input value is not a `string` or an object implementing `\Stringable`.
31+
32+
## Options
33+
34+
### `pattern`
35+
36+
type: `string` `required`
37+
38+
Regular expression pattern to be matched against.
39+
40+
### `match`
41+
42+
type: `bool` default: `true`
43+
44+
- `true` the validation will pass if the given input value matches the regular expression pattern.
45+
- `false` the validation will pass if the given input value *does not* match the regular expression pattern.
46+
47+
### `normalizer`
48+
49+
type: `callable` default: `null`
50+
51+
Allows to define a `callable` that will be applied to the value before checking if it is valid.
52+
53+
For example, use `trim`, or pass your own function, to not evaluate whitespaces at the end of a string:
54+
55+
```php
56+
// allow all chars except whitespaces
57+
Validator::length(pattern: '/^\S*$/')->validate('abc '); // false
58+
59+
Validator::length(pattern: '/^\S*$/', normalizer: 'trim')->validate('abc '); // true
60+
Validator::length(pattern: '/^\S*$/', normalizer: fn($value) => trim($value))->validate('abc '); // true
61+
```
62+
63+
### `message`
64+
65+
type: `?string` default: `The {{ name }} value is not valid.`
66+
67+
Message that will be shown when the input value does not match the regular expression pattern.
68+
69+
The following parameters are available:
70+
71+
| Parameter | Description |
72+
|-----------------|---------------------------|
73+
| `{{ value }}` | The current invalid value |
74+
| `{{ name }}` | Name of the invalid value |
75+
| `{{ pattern }}` | The given pattern |
76+
77+
## Changelog
78+
79+
- `0.8.0` Created

src/ChainedValidatorInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public function country(
3131
?string $message = null
3232
): ChainedValidatorInterface&Validator;
3333

34+
public function dateTime(
35+
string $format = 'Y-m-d H:i:s',
36+
?string $message = null
37+
): ChainedValidatorInterface&Validator;
38+
3439
public function eachKey(
3540
Validator $validator,
3641
?string $message = null
@@ -84,12 +89,24 @@ public function notBlank(
8489
?string $message = null
8590
): ChainedValidatorInterface&Validator;
8691

92+
public function passwordStrength(
93+
string $minStrength = 'medium',
94+
?string $message = null
95+
): ChainedValidatorInterface&Validator;
96+
8797
public function range(
8898
mixed $min,
8999
mixed $max,
90100
?string $message = null
91101
): ChainedValidatorInterface&Validator;
92102

103+
public function regex(
104+
string $pattern,
105+
bool $match = true,
106+
?callable $normalizer = null,
107+
?string $message = null
108+
): ChainedValidatorInterface&Validator;
109+
93110
public function rule(
94111
RuleInterface $constraint
95112
): ChainedValidatorInterface&Validator;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class DateTimeException extends ValidationException {}

0 commit comments

Comments
 (0)