Skip to content

Commit 7046560

Browse files
committed
Invert the behaviour of NotUndef
Since we have the ability to use `not` as a prefix, having rules that start with not becomes a bit inflexible, verbose, and harder to understand. This commit will refactor the `NotUndef` rule by inverting its behaviour and renaming it to `Undef`.
1 parent 338c186 commit 7046560

26 files changed

+134
-156
lines changed

bin/create-mixin

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use Respect\Validation\Mixins\NotBuilder;
2626
use Respect\Validation\Mixins\NullOrBuilder;
2727
use Respect\Validation\Mixins\PropertyBuilder;
2828
use Respect\Validation\Mixins\UndefOrBuilder;
29-
use Respect\Validation\Rules\NotUndef;
29+
use Respect\Validation\Rules\Undef;
3030
use Respect\Validation\Rules\NullOr;
3131
use Respect\Validation\Rules\UndefOr;
3232
use Respect\Validation\Rule;
@@ -172,10 +172,10 @@ function overwriteFile(string $content, string $basename): void
172172
['Length', 'length', $numberRelatedRules, []],
173173
['Max', 'max', $numberRelatedRules, []],
174174
['Min', 'min', $numberRelatedRules, []],
175-
['Not', 'not', [], ['Not', 'NotEmpty', 'NotEmoji', 'NotUndef', 'NullOr', 'UndefOr', 'Attributes', 'Templated', 'Named']],
176-
['NullOr', 'nullOr', [], ['NullOr', 'Blank', 'NotUndef', 'UndefOr', 'Templated', 'Named']],
175+
['Not', 'not', [], ['Not', 'NotEmpty', 'NotEmoji', 'NullOr', 'UndefOr', 'Attributes', 'Templated', 'Named']],
176+
['NullOr', 'nullOr', [], ['NullOr', 'Blank', 'Undef', 'UndefOr', 'Templated', 'Named']],
177177
['Property', 'property', [], $structureRelatedRules],
178-
['UndefOr', 'undefOr', [], ['NullOr', 'Blank', 'NotUndef', 'UndefOr', 'Attributes', 'Templated', 'Named']],
178+
['UndefOr', 'undefOr', [], ['NullOr', 'Blank', 'Undef', 'UndefOr', 'Attributes', 'Templated', 'Named']],
179179
['', null, [], []],
180180
];
181181

docs/09-list-of-rules-by-category.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163
- [FilterVar](rules/FilterVar.md)
164164
- [Named](rules/Named.md)
165165
- [NotEmpty](rules/NotEmpty.md)
166-
- [NotUndef](rules/NotUndef.md)
167166
- [Templated](rules/Templated.md)
167+
- [Undef](rules/Undef.md)
168168

169169
## Nesting
170170

@@ -406,7 +406,6 @@
406406
- [Not](rules/Not.md)
407407
- [NotEmoji](rules/NotEmoji.md)
408408
- [NotEmpty](rules/NotEmpty.md)
409-
- [NotUndef](rules/NotUndef.md)
410409
- [NoWhitespace](rules/NoWhitespace.md)
411410
- [NullOr](rules/NullOr.md)
412411
- [NullType](rules/NullType.md)
@@ -451,6 +450,7 @@
451450
- [Tld](rules/Tld.md)
452451
- [TrueVal](rules/TrueVal.md)
453452
- [Type](rules/Type.md)
453+
- [Undef](rules/Undef.md)
454454
- [UndefOr](rules/UndefOr.md)
455455
- [Unique](rules/Unique.md)
456456
- [Uploaded](rules/Uploaded.md)

docs/rules/Attributes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Example of object:
1010
use Respect\Validation\Rules as Rule;
1111

1212
#[Rule\AnyOf(
13-
new Rule\Property('email', new Rule\NotUndef()),
14-
new Rule\Property('phone', new Rule\NotUndef()),
13+
new Rule\Property('email', new Rule\Not(new Rule\Undef())),
14+
new Rule\Property('phone', new Rule\Not(new Rule\Undef())),
1515
)]
1616
final class Person
1717
{

docs/rules/Blank.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ It's similar to [NotEmpty](NotEmpty.md), but way stricter.
5656
See also:
5757

5858
- [NotEmpty](NotEmpty.md)
59-
- [NotUndef](NotUndef.md)
6059
- [NoWhitespace](NoWhitespace.md)
6160
- [NullType](NullType.md)
6261
- [Number](Number.md)
62+
- [Undef](Undef.md)
6363
- [UndefOr](UndefOr.md)

docs/rules/NoWhitespace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ See also:
4545
- [Blank](Blank.md)
4646
- [CreditCard](CreditCard.md)
4747
- [NotEmpty](NotEmpty.md)
48-
- [NotUndef](NotUndef.md)
48+
- [Undef](Undef.md)
4949
- [UndefOr](UndefOr.md)

docs/rules/NotEmpty.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ See also:
6868
- [Each](Each.md)
6969
- [Max](Max.md)
7070
- [Min](Min.md)
71-
- [NotUndef](NotUndef.md)
7271
- [NoWhitespace](NoWhitespace.md)
7372
- [NullType](NullType.md)
7473
- [Number](Number.md)
74+
- [Undef](Undef.md)
7575
- [UndefOr](UndefOr.md)

docs/rules/NotUndef.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

docs/rules/NullType.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ See also:
4646
- [FloatType](FloatType.md)
4747
- [IntType](IntType.md)
4848
- [NotEmpty](NotEmpty.md)
49-
- [NotUndef](NotUndef.md)
5049
- [NullOr](NullOr.md)
5150
- [Number](Number.md)
5251
- [ObjectType](ObjectType.md)
5352
- [ResourceType](ResourceType.md)
5453
- [StringType](StringType.md)
5554
- [StringVal](StringVal.md)
5655
- [Type](Type.md)
56+
- [Undef](Undef.md)
5757
- [UndefOr](UndefOr.md)

docs/rules/Number.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ See also:
4848
- [FloatType](FloatType.md)
4949
- [IntType](IntType.md)
5050
- [NotEmpty](NotEmpty.md)
51-
- [NotUndef](NotUndef.md)
5251
- [NullType](NullType.md)
5352
- [NumericVal](NumericVal.md)
5453
- [ObjectType](ObjectType.md)
5554
- [ResourceType](ResourceType.md)
5655
- [StringType](StringType.md)
5756
- [Type](Type.md)
57+
- [Undef](Undef.md)

docs/rules/Undef.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Undef
2+
3+
- `Undef()`
4+
5+
Validates if the given input is undefined. By _undefined_ we consider `null` or an empty string (`''`).
6+
7+
```php
8+
v::undef()->isValid(''); // true
9+
v::undef()->isValid(null); // true
10+
```
11+
12+
Other values similar to _undefined_ values are considered _defined_:
13+
14+
```php
15+
v::undef()->isValid([]); // false
16+
v::undef()->isValid(' '); // false
17+
v::undef()->isValid(0); // false
18+
v::undef()->isValid('0'); // false
19+
v::undef()->isValid('0.0'); // false
20+
v::undef()->isValid(false); // false
21+
v::undef()->isValid(['']); // false
22+
v::undef()->isValid([' ']); // false
23+
v::undef()->isValid([0]); // false
24+
v::undef()->isValid(['0']); // false
25+
v::undef()->isValid([false]); // false
26+
v::undef()->isValid([[''], [0]]); // false
27+
v::undef()->isValid(new stdClass()); // false
28+
```
29+
30+
## Templates
31+
32+
### `Undef::TEMPLATE_STANDARD`
33+
34+
| Mode | Template |
35+
| ---------- | ----------------------------- |
36+
| `default` | {{subject}} must be undefined |
37+
| `inverted` | {{subject}} must be defined |
38+
39+
## Template placeholders
40+
41+
| Placeholder | Description |
42+
| ----------- | ---------------------------------------------------------------- |
43+
| `subject` | The validated input or the custom validator name (if specified). |
44+
45+
## Categorization
46+
47+
- Miscellaneous
48+
49+
## Changelog
50+
51+
| Version | Description |
52+
| ------: | ------------------------------------------- |
53+
| 3.0.0 | Renamed to `Undef` and changed the behavior |
54+
| 1.0.0 | Created as `NotOptional` |
55+
56+
---
57+
58+
See also:
59+
60+
- [Blank](Blank.md)
61+
- [NotEmpty](NotEmpty.md)
62+
- [NoWhitespace](NoWhitespace.md)
63+
- [NullType](NullType.md)
64+
- [Number](Number.md)
65+
- [UndefOr](UndefOr.md)

0 commit comments

Comments
 (0)