diff --git a/bin/create-mixin b/bin/create-mixin index c28c233d..cea77560 100755 --- a/bin/create-mixin +++ b/bin/create-mixin @@ -25,6 +25,7 @@ use Respect\Validation\Mixins\StaticNot; use Respect\Validation\Mixins\StaticNullOr; use Respect\Validation\Mixins\StaticProperty; use Respect\Validation\Mixins\StaticUndefOr; +use Respect\Validation\Rules\NotUndef; use Respect\Validation\Rules\NullOr; use Respect\Validation\Rules\UndefOr; use Respect\Validation\Validatable; @@ -175,10 +176,10 @@ function overwriteFile(string $content, string $basename): void ['Length', 'length', $numberRelatedRules, []], ['Max', 'max', $numberRelatedRules, []], ['Min', 'min', $numberRelatedRules, []], - ['Not', 'not', [], ['Not', 'NotEmpty', 'NotBlank', 'NotEmoji', 'NotOptional', 'NullOr', 'UndefOr', 'Optional']], - ['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'UndefOr']], + ['Not', 'not', [], ['Not', 'NotEmpty', 'NotBlank', 'NotEmoji', 'NotUndef', 'NotOptional', 'NullOr', 'UndefOr', 'Optional']], + ['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'NotOptional', 'NotUndef', 'UndefOr']], ['Property', 'property', [], $structureRelatedRules], - ['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'Optional', 'UndefOr']], + ['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'NotOptional', 'NotUndef', 'Optional', 'UndefOr']], ['Validator', null, [], []], ]; @@ -200,6 +201,9 @@ function overwriteFile(string $content, string $basename): void if ($className === NullOr::class) { $names['Nullable'] = $reflection; } + if ($className === NotUndef::class) { + $names['NotOptional'] = $reflection; + } } ksort($names); diff --git a/docs/02-feature-guide.md b/docs/02-feature-guide.md index 646fbcee..5c10bc0e 100644 --- a/docs/02-feature-guide.md +++ b/docs/02-feature-guide.md @@ -102,7 +102,7 @@ v::optional(v::alpha())->validate(null); // true By _optional_ we consider `null` or an empty string (`''`). -See more on [Optional](rules/Optional.md). +See more on [Optional](rules/UndefOr.md). ## Negating rules diff --git a/docs/08-list-of-rules-by-category.md b/docs/08-list-of-rules-by-category.md index d78766dd..79e298b3 100644 --- a/docs/08-list-of-rules-by-category.md +++ b/docs/08-list-of-rules-by-category.md @@ -157,7 +157,7 @@ - [FilterVar](rules/FilterVar.md) - [NotBlank](rules/NotBlank.md) - [NotEmpty](rules/NotEmpty.md) -- [NotOptional](rules/NotOptional.md) +- [NotUndef](rules/NotUndef.md) ## Nesting @@ -173,9 +173,9 @@ - [Not](rules/Not.md) - [Nullable](rules/Nullable.md) - [OneOf](rules/OneOf.md) -- [Optional](rules/Optional.md) - [Property](rules/Property.md) - [PropertyOptional](rules/PropertyOptional.md) +- [UndefOr](rules/UndefOr.md) - [When](rules/When.md) ## Numbers @@ -394,7 +394,7 @@ - [NotBlank](rules/NotBlank.md) - [NotEmoji](rules/NotEmoji.md) - [NotEmpty](rules/NotEmpty.md) -- [NotOptional](rules/NotOptional.md) +- [NotUndef](rules/NotUndef.md) - [NullType](rules/NullType.md) - [Nullable](rules/Nullable.md) - [Number](rules/Number.md) @@ -402,7 +402,6 @@ - [ObjectType](rules/ObjectType.md) - [Odd](rules/Odd.md) - [OneOf](rules/OneOf.md) -- [Optional](rules/Optional.md) - [PerfectSquare](rules/PerfectSquare.md) - [Pesel](rules/Pesel.md) - [Phone](rules/Phone.md) @@ -438,6 +437,7 @@ - [Tld](rules/Tld.md) - [TrueVal](rules/TrueVal.md) - [Type](rules/Type.md) +- [UndefOr](rules/UndefOr.md) - [Unique](rules/Unique.md) - [Uploaded](rules/Uploaded.md) - [Uppercase](rules/Uppercase.md) diff --git a/docs/rules/NoWhitespace.md b/docs/rules/NoWhitespace.md index 3de02e09..58fe9c02 100644 --- a/docs/rules/NoWhitespace.md +++ b/docs/rules/NoWhitespace.md @@ -29,5 +29,5 @@ See also: - [CreditCard](CreditCard.md) - [NotBlank](NotBlank.md) - [NotEmpty](NotEmpty.md) -- [NotOptional](NotOptional.md) -- [Optional](Optional.md) +- [NotUndef](NotUndef.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/NotBlank.md b/docs/rules/NotBlank.md index 6141e688..d5de9678 100644 --- a/docs/rules/NotBlank.md +++ b/docs/rules/NotBlank.md @@ -41,7 +41,7 @@ See also: - [NoWhitespace](NoWhitespace.md) - [NotEmpty](NotEmpty.md) -- [NotOptional](NotOptional.md) +- [NotUndef](NotUndef.md) - [NullType](NullType.md) - [Number](Number.md) -- [Optional](Optional.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/NotEmpty.md b/docs/rules/NotEmpty.md index a7149d60..585e32a8 100644 --- a/docs/rules/NotEmpty.md +++ b/docs/rules/NotEmpty.md @@ -53,7 +53,7 @@ See also: - [Min](Min.md) - [NoWhitespace](NoWhitespace.md) - [NotBlank](NotBlank.md) -- [NotOptional](NotOptional.md) +- [NotUndef](NotUndef.md) - [NullType](NullType.md) - [Number](Number.md) -- [Optional](Optional.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/NotOptional.md b/docs/rules/NotOptional.md deleted file mode 100644 index 34c47084..00000000 --- a/docs/rules/NotOptional.md +++ /dev/null @@ -1,50 +0,0 @@ -# NotOptional - -- `NotOptional()` - -Validates if the given input is not optional. By _optional_ we consider `null` -or an empty string (`''`). - -```php -v::notOptional()->validate(''); // false -v::notOptional()->validate(null); // false -``` - -Other values: - -```php -v::notOptional()->validate([]); // true -v::notOptional()->validate(' '); // true -v::notOptional()->validate(0); // true -v::notOptional()->validate('0'); // true -v::notOptional()->validate(0); // true -v::notOptional()->validate('0.0'); // true -v::notOptional()->validate(false); // true -v::notOptional()->validate(['']); // true -v::notOptional()->validate([' ']); // true -v::notOptional()->validate([0]); // true -v::notOptional()->validate(['0']); // true -v::notOptional()->validate([false]); // true -v::notOptional()->validate([[''), [0]]); // true -v::notOptional()->validate(new stdClass()); // true -``` - -## Categorization - -- Miscellaneous - -## Changelog - -Version | Description ---------|------------- - 1.0.0 | Created - -*** -See also: - -- [NoWhitespace](NoWhitespace.md) -- [NotBlank](NotBlank.md) -- [NotEmpty](NotEmpty.md) -- [NullType](NullType.md) -- [Number](Number.md) -- [Optional](Optional.md) diff --git a/docs/rules/NotUndef.md b/docs/rules/NotUndef.md new file mode 100644 index 00000000..e4be9506 --- /dev/null +++ b/docs/rules/NotUndef.md @@ -0,0 +1,51 @@ +# NotUndef + +- `NotUndef()` + +Validates if the given input is not optional. By _optional_ we consider `null` +or an empty string (`''`). + +```php +v::notUndef()->validate(''); // false +v::notUndef()->validate(null); // false +``` + +Other values: + +```php +v::notUndef()->validate([]); // true +v::notUndef()->validate(' '); // true +v::notUndef()->validate(0); // true +v::notUndef()->validate('0'); // true +v::notUndef()->validate(0); // true +v::notUndef()->validate('0.0'); // true +v::notUndef()->validate(false); // true +v::notUndef()->validate(['']); // true +v::notUndef()->validate([' ']); // true +v::notUndef()->validate([0]); // true +v::notUndef()->validate(['0']); // true +v::notUndef()->validate([false]); // true +v::notUndef()->validate([[''), [0]]); // true +v::notUndef()->validate(new stdClass()); // true +``` + +## Categorization + +- Miscellaneous + +## Changelog + +| Version | Description | +|---------:|------------------------------------------| +| 3.0.0 | Renamed from "NotOptional" to "NotUndef" | +| 1.0.0 | Created | + +*** +See also: + +- [NoWhitespace](NoWhitespace.md) +- [NotBlank](NotBlank.md) +- [NotEmpty](NotEmpty.md) +- [NullType](NullType.md) +- [Number](Number.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/NullType.md b/docs/rules/NullType.md index 9caca224..c03441d8 100644 --- a/docs/rules/NullType.md +++ b/docs/rules/NullType.md @@ -30,12 +30,12 @@ See also: - [IntType](IntType.md) - [NotBlank](NotBlank.md) - [NotEmpty](NotEmpty.md) -- [NotOptional](NotOptional.md) +- [NotUndef](NotUndef.md) - [Nullable](Nullable.md) - [Number](Number.md) - [ObjectType](ObjectType.md) -- [Optional](Optional.md) - [ResourceType](ResourceType.md) - [StringType](StringType.md) - [StringVal](StringVal.md) - [Type](Type.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/Nullable.md b/docs/rules/Nullable.md index d81823e1..4f171129 100644 --- a/docs/rules/Nullable.md +++ b/docs/rules/Nullable.md @@ -24,4 +24,4 @@ Version | Description See also: - [NullType](NullType.md) -- [Optional](Optional.md) +- [UndefOr](UndefOr.md) diff --git a/docs/rules/Number.md b/docs/rules/Number.md index f5f97c34..13c734ee 100644 --- a/docs/rules/Number.md +++ b/docs/rules/Number.md @@ -32,7 +32,7 @@ See also: - [IntType](IntType.md) - [NotBlank](NotBlank.md) - [NotEmpty](NotEmpty.md) -- [NotOptional](NotOptional.md) +- [NotUndef](NotUndef.md) - [NullType](NullType.md) - [NumericVal](NumericVal.md) - [ObjectType](ObjectType.md) diff --git a/docs/rules/UndefOr.md b/docs/rules/UndefOr.md index d095aca9..4ba70bd0 100644 --- a/docs/rules/UndefOr.md +++ b/docs/rules/UndefOr.md @@ -40,6 +40,6 @@ See also: - [NoWhitespace](NoWhitespace.md) - [NotBlank](NotBlank.md) - [NotEmpty](NotEmpty.md) -- [NotOptional](NotOptional.md) +- [NotUndef](NotUndef.md) - [NullType](NullType.md) - [Nullable](Nullable.md) diff --git a/library/Mixins/ChainedKey.php b/library/Mixins/ChainedKey.php index b582fa0f..fd775aa4 100644 --- a/library/Mixins/ChainedKey.php +++ b/library/Mixins/ChainedKey.php @@ -255,6 +255,8 @@ interface ChainedKey public function keyNotOptional(int|string $key): ChainedValidator; + public function keyNotUndef(int|string $key): ChainedValidator; + public function keyNullType(int|string $key): ChainedValidator; public function keyNumber(int|string $key): ChainedValidator; diff --git a/library/Mixins/ChainedNullOr.php b/library/Mixins/ChainedNullOr.php index ecbf7c89..37cd0cc7 100644 --- a/library/Mixins/ChainedNullOr.php +++ b/library/Mixins/ChainedNullOr.php @@ -242,8 +242,6 @@ interface ChainedNullOr public function nullOrNotEmpty(): ChainedValidator; - public function nullOrNotOptional(): ChainedValidator; - public function nullOrNullType(): ChainedValidator; public function nullOrNumber(): ChainedValidator; diff --git a/library/Mixins/ChainedProperty.php b/library/Mixins/ChainedProperty.php index b9633429..834853ee 100644 --- a/library/Mixins/ChainedProperty.php +++ b/library/Mixins/ChainedProperty.php @@ -267,6 +267,8 @@ interface ChainedProperty public function propertyNotOptional(string $propertyName): ChainedValidator; + public function propertyNotUndef(string $propertyName): ChainedValidator; + public function propertyNullType(string $propertyName): ChainedValidator; public function propertyNumber(string $propertyName): ChainedValidator; diff --git a/library/Mixins/ChainedUndefOr.php b/library/Mixins/ChainedUndefOr.php index 9d10750f..53aaa2b8 100644 --- a/library/Mixins/ChainedUndefOr.php +++ b/library/Mixins/ChainedUndefOr.php @@ -246,8 +246,6 @@ interface ChainedUndefOr public function undefOrNotEmpty(): ChainedValidator; - public function undefOrNotOptional(): ChainedValidator; - public function undefOrNullType(): ChainedValidator; public function undefOrNumber(): ChainedValidator; diff --git a/library/Mixins/ChainedValidator.php b/library/Mixins/ChainedValidator.php index 6c4fee8f..772b8924 100644 --- a/library/Mixins/ChainedValidator.php +++ b/library/Mixins/ChainedValidator.php @@ -253,6 +253,8 @@ interface ChainedValidator extends public function notOptional(): ChainedValidator; + public function notUndef(): ChainedValidator; + public function nullOr(Validatable $rule): ChainedValidator; public function nullType(): ChainedValidator; diff --git a/library/Mixins/StaticKey.php b/library/Mixins/StaticKey.php index 4d52c00d..e5de4280 100644 --- a/library/Mixins/StaticKey.php +++ b/library/Mixins/StaticKey.php @@ -259,6 +259,8 @@ interface StaticKey public static function keyNotOptional(int|string $key): ChainedValidator; + public static function keyNotUndef(int|string $key): ChainedValidator; + public static function keyNullType(int|string $key): ChainedValidator; public static function keyNumber(int|string $key): ChainedValidator; diff --git a/library/Mixins/StaticNullOr.php b/library/Mixins/StaticNullOr.php index 883448c9..8954fc49 100644 --- a/library/Mixins/StaticNullOr.php +++ b/library/Mixins/StaticNullOr.php @@ -258,8 +258,6 @@ interface StaticNullOr public static function nullOrNotEmpty(): ChainedValidator; - public static function nullOrNotOptional(): ChainedValidator; - public static function nullOrNullType(): ChainedValidator; public static function nullOrNumber(): ChainedValidator; diff --git a/library/Mixins/StaticProperty.php b/library/Mixins/StaticProperty.php index 4fed1ca6..764f7aa3 100644 --- a/library/Mixins/StaticProperty.php +++ b/library/Mixins/StaticProperty.php @@ -291,6 +291,8 @@ interface StaticProperty public static function propertyNotOptional(string $propertyName): ChainedValidator; + public static function propertyNotUndef(string $propertyName): ChainedValidator; + public static function propertyNullType(string $propertyName): ChainedValidator; public static function propertyNumber(string $propertyName): ChainedValidator; diff --git a/library/Mixins/StaticUndefOr.php b/library/Mixins/StaticUndefOr.php index ae3aa763..2e9a0709 100644 --- a/library/Mixins/StaticUndefOr.php +++ b/library/Mixins/StaticUndefOr.php @@ -258,8 +258,6 @@ interface StaticUndefOr public static function undefOrNotEmpty(): ChainedValidator; - public static function undefOrNotOptional(): ChainedValidator; - public static function undefOrNullType(): ChainedValidator; public static function undefOrNumber(): ChainedValidator; diff --git a/library/Mixins/StaticValidator.php b/library/Mixins/StaticValidator.php index be152d57..fd24dd6a 100644 --- a/library/Mixins/StaticValidator.php +++ b/library/Mixins/StaticValidator.php @@ -256,6 +256,8 @@ interface StaticValidator extends public static function notOptional(): ChainedValidator; + public static function notUndef(): ChainedValidator; + public static function nullOr(Validatable $rule): ChainedValidator; public static function nullType(): ChainedValidator; diff --git a/library/Rules/NotOptional.php b/library/Rules/NotUndef.php similarity index 95% rename from library/Rules/NotOptional.php rename to library/Rules/NotUndef.php index 1675be9f..544b6866 100644 --- a/library/Rules/NotOptional.php +++ b/library/Rules/NotUndef.php @@ -24,7 +24,7 @@ use Respect\Validation\Rules\Core\Standard; '{{name}} must be optional', self::TEMPLATE_NAMED, )] -final class NotOptional extends Standard +final class NotUndef extends Standard { use CanValidateUndefined; diff --git a/library/Transformers/Aliases.php b/library/Transformers/Aliases.php index 12284ec4..ee36265e 100644 --- a/library/Transformers/Aliases.php +++ b/library/Transformers/Aliases.php @@ -15,6 +15,7 @@ use function in_array; final class Aliases implements Transformer { private const ALIASES = [ + 'notOptional' => 'notUndef', 'nullable' => 'nullOr', 'optional' => 'undefOr', ]; diff --git a/library/Transformers/Prefix.php b/library/Transformers/Prefix.php index 389b83e1..a2dcd342 100644 --- a/library/Transformers/Prefix.php +++ b/library/Transformers/Prefix.php @@ -30,7 +30,7 @@ final class Prefix implements Transformer 'notBlank', 'notEmoji', 'notEmpty', - 'notOptional', + 'notUndef', 'nullOr', 'property', 'propertyExists', diff --git a/tests/integration/rules/notOptional.phpt b/tests/integration/rules/notOptional.phpt deleted file mode 100644 index 4d15e443..00000000 --- a/tests/integration/rules/notOptional.phpt +++ /dev/null @@ -1,27 +0,0 @@ ---FILE-- - v::notOptional()->check(null)); -exceptionMessage(static fn() => v::not(v::notOptional())->check(0)); -exceptionMessage(static fn() => v::notOptional()->setName('Field')->check(null)); -exceptionMessage(static fn() => v::not(v::notOptional()->setName('Field'))->check([])); -exceptionFullMessage(static fn() => v::notOptional()->assert('')); -exceptionFullMessage(static fn() => v::not(v::notOptional())->assert([])); -exceptionFullMessage(static fn() => v::notOptional()->setName('Field')->assert('')); -exceptionFullMessage(static fn() => v::not(v::notOptional()->setName('Field'))->assert([])); -?> ---EXPECT-- -The value must not be optional -The value must be optional -Field must not be optional -Field must be optional -- The value must not be optional -- The value must be optional -- Field must not be optional -- Field must be optional diff --git a/tests/integration/rules/notUndef.phpt b/tests/integration/rules/notUndef.phpt new file mode 100644 index 00000000..e7277357 --- /dev/null +++ b/tests/integration/rules/notUndef.phpt @@ -0,0 +1,27 @@ +--FILE-- + v::notUndef()->check(null)); +exceptionMessage(static fn() => v::not(v::notUndef())->check(0)); +exceptionMessage(static fn() => v::notUndef()->setName('Field')->check(null)); +exceptionMessage(static fn() => v::not(v::notUndef()->setName('Field'))->check([])); +exceptionFullMessage(static fn() => v::notUndef()->assert('')); +exceptionFullMessage(static fn() => v::not(v::notUndef())->assert([])); +exceptionFullMessage(static fn() => v::notUndef()->setName('Field')->assert('')); +exceptionFullMessage(static fn() => v::not(v::notUndef()->setName('Field'))->assert([])); +?> +--EXPECT-- +The value must not be optional +The value must be optional +Field must not be optional +Field must be optional +- The value must not be optional +- The value must be optional +- Field must not be optional +- Field must be optional diff --git a/tests/unit/Rules/NotOptionalTest.php b/tests/unit/Rules/NotUndefTest.php similarity index 78% rename from tests/unit/Rules/NotOptionalTest.php rename to tests/unit/Rules/NotUndefTest.php index a9a2e3cb..9534c164 100644 --- a/tests/unit/Rules/NotOptionalTest.php +++ b/tests/unit/Rules/NotUndefTest.php @@ -15,13 +15,13 @@ use Respect\Validation\Test\RuleTestCase; use stdClass; #[Group('rule')] -#[CoversClass(NotOptional::class)] -final class NotOptionalTest extends RuleTestCase +#[CoversClass(NotUndef::class)] +final class NotUndefTest extends RuleTestCase { - /** @return iterable */ + /** @return iterable */ public static function providerForValidInput(): iterable { - $rule = new NotOptional(); + $rule = new NotUndef(); return [ [$rule, []], @@ -41,10 +41,10 @@ final class NotOptionalTest extends RuleTestCase ]; } - /** @return iterable */ + /** @return iterable */ public static function providerForInvalidInput(): iterable { - $rule = new NotOptional(); + $rule = new NotUndef(); return [ [$rule, null], diff --git a/tests/unit/Rules/UndefOrTest.php b/tests/unit/Rules/UndefOrTest.php index 0be86470..71aab680 100644 --- a/tests/unit/Rules/UndefOrTest.php +++ b/tests/unit/Rules/UndefOrTest.php @@ -44,7 +44,7 @@ final class UndefOrTest extends RuleTestCase } #[Test] - public function itShouldUseWrappedRuleToEvaluateWhenNotOptional(): void + public function itShouldUseWrappedRuleToEvaluateWhenNotUndef(): void { $input = new stdClass(); diff --git a/tests/unit/Transformers/AliasesTest.php b/tests/unit/Transformers/AliasesTest.php index 86f51f98..0867517f 100644 --- a/tests/unit/Transformers/AliasesTest.php +++ b/tests/unit/Transformers/AliasesTest.php @@ -18,6 +18,19 @@ use Respect\Validation\Test\Transformers\StubTransformer; #[CoversClass(Aliases::class)] final class AliasesTest extends TestCase { + #[Test] + public function itShouldConvertNotOptionalIntoNotUndef(): void + { + $transformer = new Aliases(new StubTransformer()); + + $ruleSpec = new RuleSpec('notOptional', [Stub::daze()]); + + $actual = $transformer->transform($ruleSpec); + $expected = new RuleSpec('notUndef', $ruleSpec->arguments); + + self::assertEquals($expected, $actual); + } + #[Test] public function itShouldConvertOptionalIntoUndefOr(): void { diff --git a/tests/unit/Transformers/PrefixTest.php b/tests/unit/Transformers/PrefixTest.php index 9e5e103e..f80c5452 100644 --- a/tests/unit/Transformers/PrefixTest.php +++ b/tests/unit/Transformers/PrefixTest.php @@ -96,7 +96,7 @@ final class PrefixTest extends TestCase 'notBlank' => ['notBlank'], 'notEmoji' => ['notEmoji'], 'notEmpty' => ['notEmpty'], - 'notOptional' => ['notOptional'], + 'notUndef' => ['notUndef'], 'property' => ['property'], 'propertyExists' => ['propertyExists'], 'propertyOptional' => ['propertyOptional'],