Skip to content

Commit a37cac7

Browse files
committed
Invert the behaviour of NoWhitespace
Since we have the ability to use `not` as a prefix, having rules that validate negative behavior makes them a bit inflexible, verbose, and harder to understand. This commit will refactor the `NoWhitespace` rule by inverting its behaviour and renaming it to `Spaced`. Although this is a breaking change, users will still be able to have a similar behavior with the prefix `not` + `Spaced`.
1 parent 7046560 commit a37cac7

34 files changed

+159
-152
lines changed

docs/06-concrete-api.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ or magic methods. We'll use a traditional dependency injection approach.
77
```php
88
use Respect\Validation\Validator as v;
99

10-
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
10+
$usernameValidator = v::alnum()->notSpaced()->length(1, 15);
1111
$usernameValidator->isValid('alganet'); // true
1212
```
1313

1414
If you `var_dump($usernameValidator)`, you'll see a composite of objects with
15-
`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\NoWhitespace` and
15+
`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\Spaced` wrapped in `Respect\Validation\Rules\Not` and
1616
`Respect\Validation\Rules\Length`. There is a specific object for each rule, and
1717
the chain only builds the structure. You can build it by yourself:
1818

1919
```php
2020
use Respect\Validation\Rules;
21+
use Respect\Validation\Validator;
2122

22-
$usernameValidator = new Rules\AllOf(
23+
$usernameValidator = Validator::create(
2324
new Rules\Alnum(),
24-
new Rules\NoWhitespace(),
25-
new Rules\Length(1, 15)
25+
new Rules\Not(
26+
new Rules\Spaced(),
27+
),
28+
new Rules\Length(1, 15),
2629
);
2730
$usernameValidator->isValid('alganet'); // true
2831
```
@@ -33,12 +36,18 @@ container or test it in the way you want. Nesting is still possible:
3336
```php
3437
use Respect\Validation\Rules;
3538

36-
$usernameValidator = new Rules\AllOf(
37-
new Rules\Alnum(),
38-
new Rules\NoWhitespace(),
39-
new Rules\Length(1, 15)
39+
$usernameValidator = Validator::create(
40+
new Rules\Key(
41+
'name',
42+
new Rules\AllOf(
43+
new Rules\Alnum(),
44+
new Rules\Not(
45+
new Rules\Spaced(),
46+
),
47+
new Rules\Length(1, 15),
48+
)
49+
)
4050
);
41-
$userValidator = new Rules\Key('name', $usernameValidator);
4251
$userValidator->isValid(['name' => 'alganet']); // true
4352
```
4453

@@ -63,7 +72,7 @@ something complex and returns for you.
6372

6473
> I really don't like static calls, can I avoid it?
6574
66-
Yes. Just use `$validator = new Validator();` each time you want a new validator,
75+
Yes. Just use `$validator = Validator::create();` each time you want a new validator,
6776
and continue from there.
6877

6978
> Do you have a static method for each rule?

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@
236236
- [Json](rules/Json.md)
237237
- [Lowercase](rules/Lowercase.md)
238238
- [NotEmoji](rules/NotEmoji.md)
239-
- [NoWhitespace](rules/NoWhitespace.md)
240239
- [Phone](rules/Phone.md)
241240
- [PhpLabel](rules/PhpLabel.md)
242241
- [PostalCode](rules/PostalCode.md)
@@ -246,6 +245,7 @@
246245
- [Slug](rules/Slug.md)
247246
- [Sorted](rules/Sorted.md)
248247
- [Space](rules/Space.md)
248+
- [Spaced](rules/Spaced.md)
249249
- [StartsWith](rules/StartsWith.md)
250250
- [StringType](rules/StringType.md)
251251
- [StringVal](rules/StringVal.md)
@@ -406,7 +406,6 @@
406406
- [Not](rules/Not.md)
407407
- [NotEmoji](rules/NotEmoji.md)
408408
- [NotEmpty](rules/NotEmpty.md)
409-
- [NoWhitespace](rules/NoWhitespace.md)
410409
- [NullOr](rules/NullOr.md)
411410
- [NullType](rules/NullType.md)
412411
- [Number](rules/Number.md)
@@ -439,6 +438,7 @@
439438
- [Slug](rules/Slug.md)
440439
- [Sorted](rules/Sorted.md)
441440
- [Space](rules/Space.md)
441+
- [Spaced](rules/Spaced.md)
442442
- [StartsWith](rules/StartsWith.md)
443443
- [StringType](rules/StringType.md)
444444
- [StringVal](rules/StringVal.md)

docs/rules/Alnum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ See also:
7272
- [Digit](Digit.md)
7373
- [Lowercase](Lowercase.md)
7474
- [NotEmoji](NotEmoji.md)
75-
- [NoWhitespace](NoWhitespace.md)
7675
- [Regex](Regex.md)
76+
- [Spaced](Spaced.md)
7777
- [StringType](StringType.md)
7878
- [StringVal](StringVal.md)
7979
- [Uppercase](Uppercase.md)

docs/rules/Alpha.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ See also:
6666
- [Digit](Digit.md)
6767
- [Lowercase](Lowercase.md)
6868
- [NotEmoji](NotEmoji.md)
69-
- [NoWhitespace](NoWhitespace.md)
7069
- [Regex](Regex.md)
70+
- [Spaced](Spaced.md)
7171
- [Uppercase](Uppercase.md)
7272
- [Vowel](Vowel.md)

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-
- [NoWhitespace](NoWhitespace.md)
6059
- [NullType](NullType.md)
6160
- [Number](Number.md)
61+
- [Spaced](Spaced.md)
6262
- [Undef](Undef.md)
6363
- [UndefOr](UndefOr.md)

docs/rules/CreditCard.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The current supported brands are:
3030
- RuPay (`'RuPay'` or `CreditCard::RUPAY`)
3131

3232
It ignores any non-numeric characters, use [Digit](Digit.md),
33-
[NoWhitespace](NoWhitespace.md), or [Regex](Regex.md) when appropriate.
33+
[Spaced](Spaced.md), or [Regex](Regex.md) when appropriate.
3434

3535
```php
3636
v::digit()->creditCard()->isValid('5376747397208720'); // true
@@ -79,5 +79,5 @@ See also:
7979
- [Digit](Digit.md)
8080
- [Iban](Iban.md)
8181
- [Luhn](Luhn.md)
82-
- [NoWhitespace](NoWhitespace.md)
8382
- [Regex](Regex.md)
83+
- [Spaced](Spaced.md)

docs/rules/NoWhitespace.md

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

docs/rules/NotEmpty.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- `NotEmpty()`
44

55
Validates wether the given input is not empty. This function also takes whitespace
6-
into account, use `noWhitespace()` if no spaces or linebreaks and other
6+
into account, use `notSpaced()` if no spaces or linebreaks and other
77
whitespace anywhere in the input is desired.
88

99
```php
@@ -68,8 +68,8 @@ See also:
6868
- [Each](Each.md)
6969
- [Max](Max.md)
7070
- [Min](Min.md)
71-
- [NoWhitespace](NoWhitespace.md)
7271
- [NullType](NullType.md)
7372
- [Number](Number.md)
73+
- [Spaced](Spaced.md)
7474
- [Undef](Undef.md)
7575
- [UndefOr](UndefOr.md)

docs/rules/Spaced.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Spaced
2+
3+
- `Spaced()`
4+
5+
Validates if a string contains at least one whitespace (spaces, tabs, or line breaks);
6+
7+
```php
8+
v::spaced()->isValid('foo bar'); // true
9+
v::spaced()->isValid("foo\nbar"); // true
10+
```
11+
12+
This is most useful when inverting the validator as `notSpaced()`, and chaining with other validators such as [Alnum](Alnum.md) or [Alpha](Alpha.md) to ensure that a string contains no whitespace characters:
13+
14+
```php
15+
v::notSpaced()->alnum()->isValid('username'); // true
16+
v::notSpaced()->alnum()->isValid('user name'); // false
17+
```
18+
19+
## Templates
20+
21+
### `Spaced::TEMPLATE_STANDARD`
22+
23+
| Mode | Template |
24+
| ---------- | ------------------------------------------------ |
25+
| `default` | {{subject}} must contain at least one whitespace |
26+
| `inverted` | {{subject}} must not contain whitespaces |
27+
28+
## Template placeholders
29+
30+
| Placeholder | Description |
31+
| ----------- | ---------------------------------------------------------------- |
32+
| `subject` | The validated input or the custom validator name (if specified). |
33+
34+
## Categorization
35+
36+
- Strings
37+
38+
## Changelog
39+
40+
| Version | Description |
41+
| ------: | -------------------------------------------- |
42+
| 3.0.0 | Renamed to `Spaced` and changed the behavior |
43+
| 0.3.9 | Created as `NoWhitespace` |
44+
45+
---
46+
47+
See also:
48+
49+
- [Alnum](Alnum.md)
50+
- [Alpha](Alpha.md)
51+
- [Blank](Blank.md)
52+
- [CreditCard](CreditCard.md)
53+
- [NotEmpty](NotEmpty.md)
54+
- [Undef](Undef.md)
55+
- [UndefOr](UndefOr.md)

docs/rules/Undef.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ See also:
5959

6060
- [Blank](Blank.md)
6161
- [NotEmpty](NotEmpty.md)
62-
- [NoWhitespace](NoWhitespace.md)
6362
- [NullType](NullType.md)
6463
- [Number](Number.md)
64+
- [Spaced](Spaced.md)
6565
- [UndefOr](UndefOr.md)

0 commit comments

Comments
 (0)