diff --git a/docs/06-concrete-api.md b/docs/06-concrete-api.md index d5ce6129a..cf4f0d9c8 100644 --- a/docs/06-concrete-api.md +++ b/docs/06-concrete-api.md @@ -7,22 +7,25 @@ or magic methods. We'll use a traditional dependency injection approach. ```php use Respect\Validation\Validator as v; -$usernameValidator = v::alnum()->noWhitespace()->length(1, 15); +$usernameValidator = v::alnum()->notSpaced()->length(1, 15); $usernameValidator->isValid('alganet'); // true ``` If you `var_dump($usernameValidator)`, you'll see a composite of objects with -`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\NoWhitespace` and +`Respect\Validation\Rules\Alnum`, `Respect\Validation\Rules\Spaced` wrapped in `Respect\Validation\Rules\Not` and `Respect\Validation\Rules\Length`. There is a specific object for each rule, and the chain only builds the structure. You can build it by yourself: ```php use Respect\Validation\Rules; +use Respect\Validation\Validator; -$usernameValidator = new Rules\AllOf( +$usernameValidator = Validator::create( new Rules\Alnum(), - new Rules\NoWhitespace(), - new Rules\Length(1, 15) + new Rules\Not( + new Rules\Spaced(), + ), + new Rules\Length(1, 15), ); $usernameValidator->isValid('alganet'); // true ``` @@ -33,12 +36,18 @@ container or test it in the way you want. Nesting is still possible: ```php use Respect\Validation\Rules; -$usernameValidator = new Rules\AllOf( - new Rules\Alnum(), - new Rules\NoWhitespace(), - new Rules\Length(1, 15) +$usernameValidator = Validator::create( + new Rules\Key( + 'name', + new Rules\AllOf( + new Rules\Alnum(), + new Rules\Not( + new Rules\Spaced(), + ), + new Rules\Length(1, 15), + ) + ) ); -$userValidator = new Rules\Key('name', $usernameValidator); $userValidator->isValid(['name' => 'alganet']); // true ``` @@ -63,7 +72,7 @@ something complex and returns for you. > I really don't like static calls, can I avoid it? -Yes. Just use `$validator = new Validator();` each time you want a new validator, +Yes. Just use `$validator = Validator::create();` each time you want a new validator, and continue from there. > Do you have a static method for each rule? diff --git a/docs/09-list-of-rules-by-category.md b/docs/09-list-of-rules-by-category.md index c42be628e..92c5176e6 100644 --- a/docs/09-list-of-rules-by-category.md +++ b/docs/09-list-of-rules-by-category.md @@ -236,7 +236,6 @@ - [Json](rules/Json.md) - [Lowercase](rules/Lowercase.md) - [NotEmoji](rules/NotEmoji.md) -- [NoWhitespace](rules/NoWhitespace.md) - [Phone](rules/Phone.md) - [PhpLabel](rules/PhpLabel.md) - [PostalCode](rules/PostalCode.md) @@ -246,6 +245,7 @@ - [Slug](rules/Slug.md) - [Sorted](rules/Sorted.md) - [Space](rules/Space.md) +- [Spaced](rules/Spaced.md) - [StartsWith](rules/StartsWith.md) - [StringType](rules/StringType.md) - [StringVal](rules/StringVal.md) @@ -406,7 +406,6 @@ - [Not](rules/Not.md) - [NotEmoji](rules/NotEmoji.md) - [NotEmpty](rules/NotEmpty.md) -- [NoWhitespace](rules/NoWhitespace.md) - [NullOr](rules/NullOr.md) - [NullType](rules/NullType.md) - [Number](rules/Number.md) @@ -439,6 +438,7 @@ - [Slug](rules/Slug.md) - [Sorted](rules/Sorted.md) - [Space](rules/Space.md) +- [Spaced](rules/Spaced.md) - [StartsWith](rules/StartsWith.md) - [StringType](rules/StringType.md) - [StringVal](rules/StringVal.md) diff --git a/docs/rules/Alnum.md b/docs/rules/Alnum.md index b988db4d4..a27894d9a 100644 --- a/docs/rules/Alnum.md +++ b/docs/rules/Alnum.md @@ -72,8 +72,8 @@ See also: - [Digit](Digit.md) - [Lowercase](Lowercase.md) - [NotEmoji](NotEmoji.md) -- [NoWhitespace](NoWhitespace.md) - [Regex](Regex.md) +- [Spaced](Spaced.md) - [StringType](StringType.md) - [StringVal](StringVal.md) - [Uppercase](Uppercase.md) diff --git a/docs/rules/Alpha.md b/docs/rules/Alpha.md index 7ff2fd664..7211711d6 100644 --- a/docs/rules/Alpha.md +++ b/docs/rules/Alpha.md @@ -66,7 +66,7 @@ See also: - [Digit](Digit.md) - [Lowercase](Lowercase.md) - [NotEmoji](NotEmoji.md) -- [NoWhitespace](NoWhitespace.md) - [Regex](Regex.md) +- [Spaced](Spaced.md) - [Uppercase](Uppercase.md) - [Vowel](Vowel.md) diff --git a/docs/rules/Blank.md b/docs/rules/Blank.md index 113a6916c..252a5c399 100644 --- a/docs/rules/Blank.md +++ b/docs/rules/Blank.md @@ -56,8 +56,8 @@ It's similar to [NotEmpty](NotEmpty.md), but way stricter. See also: - [NotEmpty](NotEmpty.md) -- [NoWhitespace](NoWhitespace.md) - [NullType](NullType.md) - [Number](Number.md) +- [Spaced](Spaced.md) - [Undef](Undef.md) - [UndefOr](UndefOr.md) diff --git a/docs/rules/CreditCard.md b/docs/rules/CreditCard.md index f45984be5..4aa39ab93 100644 --- a/docs/rules/CreditCard.md +++ b/docs/rules/CreditCard.md @@ -30,7 +30,7 @@ The current supported brands are: - RuPay (`'RuPay'` or `CreditCard::RUPAY`) It ignores any non-numeric characters, use [Digit](Digit.md), -[NoWhitespace](NoWhitespace.md), or [Regex](Regex.md) when appropriate. +[Spaced](Spaced.md), or [Regex](Regex.md) when appropriate. ```php v::digit()->creditCard()->isValid('5376747397208720'); // true @@ -79,5 +79,5 @@ See also: - [Digit](Digit.md) - [Iban](Iban.md) - [Luhn](Luhn.md) -- [NoWhitespace](NoWhitespace.md) - [Regex](Regex.md) +- [Spaced](Spaced.md) diff --git a/docs/rules/NoWhitespace.md b/docs/rules/NoWhitespace.md deleted file mode 100644 index a110c8c0d..000000000 --- a/docs/rules/NoWhitespace.md +++ /dev/null @@ -1,49 +0,0 @@ -# NoWhitespace - -- `NoWhitespace()` - -Validates if a string contains no whitespace (spaces, tabs and line breaks); - -```php -v::noWhitespace()->isValid('foo bar'); //false -v::noWhitespace()->isValid("foo\nbar"); // false -``` - -This is most useful when chaining with other validators such as `Alnum()` - -## Templates - -### `NoWhitespace::TEMPLATE_STANDARD` - -| Mode | Template | -| ---------- | ------------------------------------------------ | -| `default` | {{subject}} must not contain whitespaces | -| `inverted` | {{subject}} must contain at least one whitespace | - -## Template placeholders - -| Placeholder | Description | -| ----------- | ---------------------------------------------------------------- | -| `subject` | The validated input or the custom validator name (if specified). | - -## Categorization - -- Strings - -## Changelog - -| Version | Description | -| ------: | ----------- | -| 0.3.9 | Created | - ---- - -See also: - -- [Alnum](Alnum.md) -- [Alpha](Alpha.md) -- [Blank](Blank.md) -- [CreditCard](CreditCard.md) -- [NotEmpty](NotEmpty.md) -- [Undef](Undef.md) -- [UndefOr](UndefOr.md) diff --git a/docs/rules/NotEmpty.md b/docs/rules/NotEmpty.md index a6b6054f6..762ab28ce 100644 --- a/docs/rules/NotEmpty.md +++ b/docs/rules/NotEmpty.md @@ -3,7 +3,7 @@ - `NotEmpty()` Validates wether the given input is not empty. This function also takes whitespace -into account, use `noWhitespace()` if no spaces or linebreaks and other +into account, use `notSpaced()` if no spaces or linebreaks and other whitespace anywhere in the input is desired. ```php @@ -68,8 +68,8 @@ See also: - [Each](Each.md) - [Max](Max.md) - [Min](Min.md) -- [NoWhitespace](NoWhitespace.md) - [NullType](NullType.md) - [Number](Number.md) +- [Spaced](Spaced.md) - [Undef](Undef.md) - [UndefOr](UndefOr.md) diff --git a/docs/rules/Spaced.md b/docs/rules/Spaced.md new file mode 100644 index 000000000..82e71a020 --- /dev/null +++ b/docs/rules/Spaced.md @@ -0,0 +1,55 @@ +# Spaced + +- `Spaced()` + +Validates if a string contains at least one whitespace (spaces, tabs, or line breaks); + +```php +v::spaced()->isValid('foo bar'); // true +v::spaced()->isValid("foo\nbar"); // true +``` + +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: + +```php +v::notSpaced()->alnum()->isValid('username'); // true +v::notSpaced()->alnum()->isValid('user name'); // false +``` + +## Templates + +### `Spaced::TEMPLATE_STANDARD` + +| Mode | Template | +| ---------- | ------------------------------------------------ | +| `default` | {{subject}} must contain at least one whitespace | +| `inverted` | {{subject}} must not contain whitespaces | + +## Template placeholders + +| Placeholder | Description | +| ----------- | ---------------------------------------------------------------- | +| `subject` | The validated input or the custom validator name (if specified). | + +## Categorization + +- Strings + +## Changelog + +| Version | Description | +| ------: | -------------------------------------------- | +| 3.0.0 | Renamed to `Spaced` and changed the behavior | +| 0.3.9 | Created as `NoWhitespace` | + +--- + +See also: + +- [Alnum](Alnum.md) +- [Alpha](Alpha.md) +- [Blank](Blank.md) +- [CreditCard](CreditCard.md) +- [NotEmpty](NotEmpty.md) +- [Undef](Undef.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/Undef.md b/docs/rules/Undef.md index c0fe59e71..ec3d40be1 100644 --- a/docs/rules/Undef.md +++ b/docs/rules/Undef.md @@ -59,7 +59,7 @@ See also: - [Blank](Blank.md) - [NotEmpty](NotEmpty.md) -- [NoWhitespace](NoWhitespace.md) - [NullType](NullType.md) - [Number](Number.md) +- [Spaced](Spaced.md) - [UndefOr](UndefOr.md) diff --git a/docs/rules/UndefOr.md b/docs/rules/UndefOr.md index f75e95cb1..662c5fd50 100644 --- a/docs/rules/UndefOr.md +++ b/docs/rules/UndefOr.md @@ -67,7 +67,7 @@ See also: - [Blank](Blank.md) - [NotEmpty](NotEmpty.md) -- [NoWhitespace](NoWhitespace.md) - [NullOr](NullOr.md) - [NullType](NullType.md) +- [Spaced](Spaced.md) - [Undef](Undef.md) diff --git a/library/Mixins/Builder.php b/library/Mixins/Builder.php index 15ddd156b..a82b54477 100644 --- a/library/Mixins/Builder.php +++ b/library/Mixins/Builder.php @@ -238,8 +238,6 @@ public static function nip(): Chain; public static function no(bool $useLocale = false): Chain; - public static function noWhitespace(): Chain; - public static function noneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function not(Rule $rule): Chain; @@ -313,6 +311,8 @@ public static function sorted(string $direction): Chain; public static function space(string ...$additionalChars): Chain; + public static function spaced(): Chain; + public static function startsWith(mixed $startValue, bool $identical = false): Chain; public static function stringType(): Chain; diff --git a/library/Mixins/Chain.php b/library/Mixins/Chain.php index 1b61daa2a..3d46de119 100644 --- a/library/Mixins/Chain.php +++ b/library/Mixins/Chain.php @@ -241,8 +241,6 @@ public function nip(): Chain; public function no(bool $useLocale = false): Chain; - public function noWhitespace(): Chain; - public function noneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function not(Rule $rule): Chain; @@ -316,6 +314,8 @@ public function sorted(string $direction): Chain; public function space(string ...$additionalChars): Chain; + public function spaced(): Chain; + public function startsWith(mixed $startValue, bool $identical = false): Chain; public function stringType(): Chain; diff --git a/library/Mixins/KeyBuilder.php b/library/Mixins/KeyBuilder.php index 6f4af1b8d..8bbf58482 100644 --- a/library/Mixins/KeyBuilder.php +++ b/library/Mixins/KeyBuilder.php @@ -217,8 +217,6 @@ public static function keyNip(int|string $key): Chain; public static function keyNo(int|string $key, bool $useLocale = false): Chain; - public static function keyNoWhitespace(int|string $key): Chain; - public static function keyNoneOf(int|string $key, Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function keyNot(int|string $key, Rule $rule): Chain; @@ -284,6 +282,8 @@ public static function keySorted(int|string $key, string $direction): Chain; public static function keySpace(int|string $key, string ...$additionalChars): Chain; + public static function keySpaced(int|string $key): Chain; + public static function keyStartsWith(int|string $key, mixed $startValue, bool $identical = false): Chain; public static function keyStringType(int|string $key): Chain; diff --git a/library/Mixins/KeyChain.php b/library/Mixins/KeyChain.php index 55689aa08..0dfeb63d3 100644 --- a/library/Mixins/KeyChain.php +++ b/library/Mixins/KeyChain.php @@ -217,8 +217,6 @@ public function keyNip(int|string $key): Chain; public function keyNo(int|string $key, bool $useLocale = false): Chain; - public function keyNoWhitespace(int|string $key): Chain; - public function keyNoneOf(int|string $key, Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function keyNot(int|string $key, Rule $rule): Chain; @@ -284,6 +282,8 @@ public function keySorted(int|string $key, string $direction): Chain; public function keySpace(int|string $key, string ...$additionalChars): Chain; + public function keySpaced(int|string $key): Chain; + public function keyStartsWith(int|string $key, mixed $startValue, bool $identical = false): Chain; public function keyStringType(int|string $key): Chain; diff --git a/library/Mixins/NotBuilder.php b/library/Mixins/NotBuilder.php index b50a58d9a..2ef28c5f4 100644 --- a/library/Mixins/NotBuilder.php +++ b/library/Mixins/NotBuilder.php @@ -225,8 +225,6 @@ public static function notNip(): Chain; public static function notNo(bool $useLocale = false): Chain; - public static function notNoWhitespace(): Chain; - public static function notNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function notNullType(): Chain; @@ -292,6 +290,8 @@ public static function notSorted(string $direction): Chain; public static function notSpace(string ...$additionalChars): Chain; + public static function notSpaced(): Chain; + public static function notStartsWith(mixed $startValue, bool $identical = false): Chain; public static function notStringType(): Chain; diff --git a/library/Mixins/NotChain.php b/library/Mixins/NotChain.php index 50bcfee23..c949fec60 100644 --- a/library/Mixins/NotChain.php +++ b/library/Mixins/NotChain.php @@ -225,8 +225,6 @@ public function notNip(): Chain; public function notNo(bool $useLocale = false): Chain; - public function notNoWhitespace(): Chain; - public function notNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function notNullType(): Chain; @@ -292,6 +290,8 @@ public function notSorted(string $direction): Chain; public function notSpace(string ...$additionalChars): Chain; + public function notSpaced(): Chain; + public function notStartsWith(mixed $startValue, bool $identical = false): Chain; public function notStringType(): Chain; diff --git a/library/Mixins/NullOrBuilder.php b/library/Mixins/NullOrBuilder.php index 6f46f8aac..5fbc94e36 100644 --- a/library/Mixins/NullOrBuilder.php +++ b/library/Mixins/NullOrBuilder.php @@ -225,8 +225,6 @@ public static function nullOrNip(): Chain; public static function nullOrNo(bool $useLocale = false): Chain; - public static function nullOrNoWhitespace(): Chain; - public static function nullOrNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function nullOrNot(Rule $rule): Chain; @@ -298,6 +296,8 @@ public static function nullOrSorted(string $direction): Chain; public static function nullOrSpace(string ...$additionalChars): Chain; + public static function nullOrSpaced(): Chain; + public static function nullOrStartsWith(mixed $startValue, bool $identical = false): Chain; public static function nullOrStringType(): Chain; diff --git a/library/Mixins/NullOrChain.php b/library/Mixins/NullOrChain.php index b201169d1..6af09ad42 100644 --- a/library/Mixins/NullOrChain.php +++ b/library/Mixins/NullOrChain.php @@ -225,8 +225,6 @@ public function nullOrNip(): Chain; public function nullOrNo(bool $useLocale = false): Chain; - public function nullOrNoWhitespace(): Chain; - public function nullOrNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function nullOrNot(Rule $rule): Chain; @@ -298,6 +296,8 @@ public function nullOrSorted(string $direction): Chain; public function nullOrSpace(string ...$additionalChars): Chain; + public function nullOrSpaced(): Chain; + public function nullOrStartsWith(mixed $startValue, bool $identical = false): Chain; public function nullOrStringType(): Chain; diff --git a/library/Mixins/PropertyBuilder.php b/library/Mixins/PropertyBuilder.php index f63bd1a7c..d9e84b33a 100644 --- a/library/Mixins/PropertyBuilder.php +++ b/library/Mixins/PropertyBuilder.php @@ -221,8 +221,6 @@ public static function propertyNip(string $propertyName): Chain; public static function propertyNo(string $propertyName, bool $useLocale = false): Chain; - public static function propertyNoWhitespace(string $propertyName): Chain; - public static function propertyNoneOf(string $propertyName, Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function propertyNot(string $propertyName, Rule $rule): Chain; @@ -292,6 +290,8 @@ public static function propertySorted(string $propertyName, string $direction): public static function propertySpace(string $propertyName, string ...$additionalChars): Chain; + public static function propertySpaced(string $propertyName): Chain; + public static function propertyStartsWith(string $propertyName, mixed $startValue, bool $identical = false): Chain; public static function propertyStringType(string $propertyName): Chain; diff --git a/library/Mixins/PropertyChain.php b/library/Mixins/PropertyChain.php index 40f95eaf3..573bb4000 100644 --- a/library/Mixins/PropertyChain.php +++ b/library/Mixins/PropertyChain.php @@ -217,8 +217,6 @@ public function propertyNip(string $propertyName): Chain; public function propertyNo(string $propertyName, bool $useLocale = false): Chain; - public function propertyNoWhitespace(string $propertyName): Chain; - public function propertyNoneOf(string $propertyName, Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function propertyNot(string $propertyName, Rule $rule): Chain; @@ -284,6 +282,8 @@ public function propertySorted(string $propertyName, string $direction): Chain; public function propertySpace(string $propertyName, string ...$additionalChars): Chain; + public function propertySpaced(string $propertyName): Chain; + public function propertyStartsWith(string $propertyName, mixed $startValue, bool $identical = false): Chain; public function propertyStringType(string $propertyName): Chain; diff --git a/library/Mixins/UndefOrBuilder.php b/library/Mixins/UndefOrBuilder.php index b5a4fb5e8..9be6f64e9 100644 --- a/library/Mixins/UndefOrBuilder.php +++ b/library/Mixins/UndefOrBuilder.php @@ -223,8 +223,6 @@ public static function undefOrNip(): Chain; public static function undefOrNo(bool $useLocale = false): Chain; - public static function undefOrNoWhitespace(): Chain; - public static function undefOrNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public static function undefOrNot(Rule $rule): Chain; @@ -296,6 +294,8 @@ public static function undefOrSorted(string $direction): Chain; public static function undefOrSpace(string ...$additionalChars): Chain; + public static function undefOrSpaced(): Chain; + public static function undefOrStartsWith(mixed $startValue, bool $identical = false): Chain; public static function undefOrStringType(): Chain; diff --git a/library/Mixins/UndefOrChain.php b/library/Mixins/UndefOrChain.php index 34b406c2a..f93a300a8 100644 --- a/library/Mixins/UndefOrChain.php +++ b/library/Mixins/UndefOrChain.php @@ -223,8 +223,6 @@ public function undefOrNip(): Chain; public function undefOrNo(bool $useLocale = false): Chain; - public function undefOrNoWhitespace(): Chain; - public function undefOrNoneOf(Rule $rule1, Rule $rule2, Rule ...$rules): Chain; public function undefOrNot(Rule $rule): Chain; @@ -296,6 +294,8 @@ public function undefOrSorted(string $direction): Chain; public function undefOrSpace(string ...$additionalChars): Chain; + public function undefOrSpaced(): Chain; + public function undefOrStartsWith(mixed $startValue, bool $identical = false): Chain; public function undefOrStringType(): Chain; diff --git a/library/Rules/Domain.php b/library/Rules/Domain.php index 6265e1ee2..b8c7a6808 100644 --- a/library/Rules/Domain.php +++ b/library/Rules/Domain.php @@ -61,7 +61,7 @@ private function createGenericRule(): Circuit { return new Circuit( new StringType(), - new NoWhitespace(), + new Not(new Spaced()), new Contains('.'), new Length(new GreaterThanOrEqual(3)), ); diff --git a/library/Rules/NoWhitespace.php b/library/Rules/Spaced.php similarity index 75% rename from library/Rules/NoWhitespace.php rename to library/Rules/Spaced.php index e172bc71f..d82d92ffc 100644 --- a/library/Rules/NoWhitespace.php +++ b/library/Rules/Spaced.php @@ -18,21 +18,17 @@ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] #[Template( - '{{subject}} must not contain whitespaces', '{{subject}} must contain at least one whitespace', + '{{subject}} must not contain whitespaces', )] -final class NoWhitespace extends Simple +final class Spaced extends Simple { public function isValid(mixed $input): bool { - if ($input === null) { - return true; - } - - if (is_scalar($input) === false) { + if (!is_scalar($input)) { return false; } - return !preg_match('#\s#', (string) $input); + return preg_match('/\s/', (string) $input) > 0; } } diff --git a/tests/feature/DoNotRelyOnNestedValidationExceptionInterfaceForCheckTest.php b/tests/feature/DoNotRelyOnNestedValidationExceptionInterfaceForCheckTest.php index 99cd23cf6..bb7089eb3 100644 --- a/tests/feature/DoNotRelyOnNestedValidationExceptionInterfaceForCheckTest.php +++ b/tests/feature/DoNotRelyOnNestedValidationExceptionInterfaceForCheckTest.php @@ -10,6 +10,6 @@ use Respect\Validation\Validator; test('Scenario #1', catchMessage( - fn() => Validator::alnum('__')->lengthBetween(1, 15)->noWhitespace()->assert('really messed up screen#name'), + fn() => Validator::alnum('__')->lengthBetween(1, 15)->notSpaced()->assert('really messed up screen#name'), fn(string $message) => expect($message)->toBe('"really messed up screen#name" must contain only letters (a-z), digits (0-9), and "__"'), )); diff --git a/tests/feature/Issues/Issue1333Test.php b/tests/feature/Issues/Issue1333Test.php index 85ba4ff6c..d85eaa826 100644 --- a/tests/feature/Issues/Issue1333Test.php +++ b/tests/feature/Issues/Issue1333Test.php @@ -8,7 +8,7 @@ declare(strict_types=1); test('https://github.com/Respect/Validation/issues/1333', catchAll( - fn() => v::noWhitespace()->email()->setName('User Email')->assert('not email'), + fn() => v::notSpaced()->email()->setName('User Email')->assert('not email'), fn(string $message, string $fullMessage, array $messages) => expect() ->and($message)->toBe('User Email must not contain whitespaces') ->and($fullMessage)->toBe(<<<'FULL_MESSAGE' @@ -18,7 +18,7 @@ FULL_MESSAGE) ->and($messages)->toBe([ '__root__' => 'User Email must pass all the rules', - 'noWhitespace' => '"not email" must not contain whitespaces', + 'notSpaced' => '"not email" must not contain whitespaces', 'email' => '"not email" must be a valid email address', ]), )); diff --git a/tests/feature/Readme/CustomMessagesTest.php b/tests/feature/Readme/CustomMessagesTest.php index f899b5d94..3612cb4f4 100644 --- a/tests/feature/Readme/CustomMessagesTest.php +++ b/tests/feature/Readme/CustomMessagesTest.php @@ -9,17 +9,17 @@ test('Scenario #1', catchMessages( fn() => v::alnum() - ->noWhitespace() + ->notSpaced() ->length(v::between(1, 15)) ->assert('really messed up screen#name', [ 'alnum' => '{{subject}} must contain only letters and digits', - 'noWhitespace' => '{{subject}} cannot contain spaces', + 'notSpaced' => '{{subject}} cannot contain spaces', 'length' => '{{subject}} must not have more than 15 chars', ]), fn(array $messages) => expect($messages)->toBe([ '__root__' => '"really messed up screen#name" must pass all the rules', 'alnum' => '"really messed up screen#name" must contain only letters and digits', - 'noWhitespace' => '"really messed up screen#name" cannot contain spaces', + 'notSpaced' => '"really messed up screen#name" cannot contain spaces', 'lengthBetween' => 'The length of "really messed up screen#name" must be between 1 and 15', ]), )); diff --git a/tests/feature/Readme/ExampleTest.php b/tests/feature/Readme/ExampleTest.php index 6d8c4db46..cf2ea273b 100644 --- a/tests/feature/Readme/ExampleTest.php +++ b/tests/feature/Readme/ExampleTest.php @@ -8,7 +8,7 @@ declare(strict_types=1); test('Scenario #1', catchFullMessage( - fn() => v::alnum()->noWhitespace()->lengthBetween(1, 15)->assert('really messed up screen#name'), + fn() => v::alnum()->notSpaced()->lengthBetween(1, 15)->assert('really messed up screen#name'), fn(string $fullMessage) => expect($fullMessage)->toBe(<<<'FULL_MESSAGE' - "really messed up screen#name" must pass all the rules - "really messed up screen#name" must contain only letters (a-z) and digits (0-9) diff --git a/tests/feature/Readme/GettingMessagesAsAnArrayTest.php b/tests/feature/Readme/GettingMessagesAsAnArrayTest.php index 2ec49fd3a..cb3b70458 100644 --- a/tests/feature/Readme/GettingMessagesAsAnArrayTest.php +++ b/tests/feature/Readme/GettingMessagesAsAnArrayTest.php @@ -8,11 +8,11 @@ declare(strict_types=1); test('Scenario #1', catchMessages( - fn() => v::alnum()->noWhitespace()->lengthBetween(1, 15)->assert('really messed up screen#name'), + fn() => v::alnum()->notSpaced()->lengthBetween(1, 15)->assert('really messed up screen#name'), fn(array $messages) => expect($messages)->toBe([ '__root__' => '"really messed up screen#name" must pass all the rules', 'alnum' => '"really messed up screen#name" must contain only letters (a-z) and digits (0-9)', - 'noWhitespace' => '"really messed up screen#name" must not contain whitespaces', + 'notSpaced' => '"really messed up screen#name" must not contain whitespaces', 'lengthBetween' => 'The length of "really messed up screen#name" must be between 1 and 15', ]), )); diff --git a/tests/feature/Rules/CallTest.php b/tests/feature/Rules/CallTest.php index a51187190..ae24117da 100644 --- a/tests/feature/Rules/CallTest.php +++ b/tests/feature/Rules/CallTest.php @@ -8,7 +8,7 @@ declare(strict_types=1); test('Scenario #1', catchMessage( - fn() => v::call('trim', v::noWhitespace())->assert(' two words '), + fn() => v::call('trim', v::notSpaced())->assert(' two words '), fn(string $message) => expect($message)->toBe('"two words" must not contain whitespaces'), )); diff --git a/tests/feature/Rules/NoWhitespaceTest.php b/tests/feature/Rules/NoWhitespaceTest.php deleted file mode 100644 index b72d668b4..000000000 --- a/tests/feature/Rules/NoWhitespaceTest.php +++ /dev/null @@ -1,28 +0,0 @@ - - * SPDX-License-Identifier: MIT - */ - -declare(strict_types=1); - -test('Scenario #1', catchMessage( - fn() => v::noWhitespace()->assert('w poiur'), - fn(string $message) => expect($message)->toBe('"w poiur" must not contain whitespaces'), -)); - -test('Scenario #2', catchMessage( - fn() => v::not(v::noWhitespace())->assert('wpoiur'), - fn(string $message) => expect($message)->toBe('"wpoiur" must contain at least one whitespace'), -)); - -test('Scenario #3', catchFullMessage( - fn() => v::noWhitespace()->assert('w poiur'), - fn(string $fullMessage) => expect($fullMessage)->toBe('- "w poiur" must not contain whitespaces'), -)); - -test('Scenario #4', catchFullMessage( - fn() => v::not(v::noWhitespace())->assert('wpoiur'), - fn(string $fullMessage) => expect($fullMessage)->toBe('- "wpoiur" must contain at least one whitespace'), -)); diff --git a/tests/feature/Rules/SpacedTest.php b/tests/feature/Rules/SpacedTest.php new file mode 100644 index 000000000..6f1cb2be1 --- /dev/null +++ b/tests/feature/Rules/SpacedTest.php @@ -0,0 +1,24 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +test('default template', catchAll( + fn() => v::spaced()->assert('word'), + fn(string $message, string $fullMessage, array $messages) => expect() + ->and($message)->toBe('"word" must contain at least one whitespace') + ->and($fullMessage)->toBe('- "word" must contain at least one whitespace') + ->and($messages)->toBe(['spaced' => '"word" must contain at least one whitespace']), +)); + +test('inverted template', catchAll( + fn() => v::not(v::spaced())->assert('two words'), + fn(string $message, string $fullMessage, array $messages) => expect() + ->and($message)->toBe('"two words" must not contain whitespaces') + ->and($fullMessage)->toBe('- "two words" must not contain whitespaces') + ->and($messages)->toBe(['notSpaced' => '"two words" must not contain whitespaces']), +)); diff --git a/tests/unit/Rules/NoWhitespaceTest.php b/tests/unit/Rules/SpacedTest.php similarity index 76% rename from tests/unit/Rules/NoWhitespaceTest.php rename to tests/unit/Rules/SpacedTest.php index c9fdc426f..bda94d768 100644 --- a/tests/unit/Rules/NoWhitespaceTest.php +++ b/tests/unit/Rules/SpacedTest.php @@ -15,13 +15,13 @@ use stdClass; #[Group('rule')] -#[CoversClass(NoWhitespace::class)] -final class NoWhitespaceTest extends RuleTestCase +#[CoversClass(Spaced::class)] +final class SpacedTest extends RuleTestCase { - /** @return iterable */ - public static function providerForValidInput(): iterable + /** @return iterable */ + public static function providerForInvalidInput(): iterable { - $rule = new NoWhitespace(); + $rule = new Spaced(); return [ [$rule, ''], @@ -29,17 +29,17 @@ public static function providerForValidInput(): iterable [$rule, 0], [$rule, 'wpoiur'], [$rule, 'Foo'], + [$rule, []], + [$rule, new stdClass()], ]; } - /** @return iterable */ - public static function providerForInvalidInput(): iterable + /** @return iterable */ + public static function providerForValidInput(): iterable { - $rule = new NoWhitespace(); + $rule = new Spaced(); return [ - [$rule, []], - [$rule, new stdClass()], [$rule, ' '], [$rule, 'w poiur'], [$rule, ' '],