Rename "NotOptional" to "NotUndef"

Since I've already renamed the "Optional" rule to "UnderOf," it makes
sense to rename this rule too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-05-06 20:29:44 +02:00
parent 719f12a424
commit 4fd26e39bb
No known key found for this signature in database
GPG key ID: 221E9281655813A6
31 changed files with 137 additions and 114 deletions

View file

@ -25,6 +25,7 @@ use Respect\Validation\Mixins\StaticNot;
use Respect\Validation\Mixins\StaticNullOr; use Respect\Validation\Mixins\StaticNullOr;
use Respect\Validation\Mixins\StaticProperty; use Respect\Validation\Mixins\StaticProperty;
use Respect\Validation\Mixins\StaticUndefOr; use Respect\Validation\Mixins\StaticUndefOr;
use Respect\Validation\Rules\NotUndef;
use Respect\Validation\Rules\NullOr; use Respect\Validation\Rules\NullOr;
use Respect\Validation\Rules\UndefOr; use Respect\Validation\Rules\UndefOr;
use Respect\Validation\Validatable; use Respect\Validation\Validatable;
@ -175,10 +176,10 @@ function overwriteFile(string $content, string $basename): void
['Length', 'length', $numberRelatedRules, []], ['Length', 'length', $numberRelatedRules, []],
['Max', 'max', $numberRelatedRules, []], ['Max', 'max', $numberRelatedRules, []],
['Min', 'min', $numberRelatedRules, []], ['Min', 'min', $numberRelatedRules, []],
['Not', 'not', [], ['Not', 'NotEmpty', 'NotBlank', 'NotEmoji', 'NotOptional', 'NullOr', 'UndefOr', 'Optional']], ['Not', 'not', [], ['Not', 'NotEmpty', 'NotBlank', 'NotEmoji', 'NotUndef', 'NotOptional', 'NullOr', 'UndefOr', 'Optional']],
['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'UndefOr']], ['NullOr', 'nullOr', [], ['Nullable', 'NullOr', 'Optional', 'NotOptional', 'NotUndef', 'UndefOr']],
['Property', 'property', [], $structureRelatedRules], ['Property', 'property', [], $structureRelatedRules],
['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'Optional', 'UndefOr']], ['UndefOr', 'undefOr', [], ['Nullable', 'NullOr', 'NotOptional', 'NotUndef', 'Optional', 'UndefOr']],
['Validator', null, [], []], ['Validator', null, [], []],
]; ];
@ -200,6 +201,9 @@ function overwriteFile(string $content, string $basename): void
if ($className === NullOr::class) { if ($className === NullOr::class) {
$names['Nullable'] = $reflection; $names['Nullable'] = $reflection;
} }
if ($className === NotUndef::class) {
$names['NotOptional'] = $reflection;
}
} }
ksort($names); ksort($names);

View file

@ -102,7 +102,7 @@ v::optional(v::alpha())->validate(null); // true
By _optional_ we consider `null` or an empty string (`''`). 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 ## Negating rules

View file

@ -157,7 +157,7 @@
- [FilterVar](rules/FilterVar.md) - [FilterVar](rules/FilterVar.md)
- [NotBlank](rules/NotBlank.md) - [NotBlank](rules/NotBlank.md)
- [NotEmpty](rules/NotEmpty.md) - [NotEmpty](rules/NotEmpty.md)
- [NotOptional](rules/NotOptional.md) - [NotUndef](rules/NotUndef.md)
## Nesting ## Nesting
@ -173,9 +173,9 @@
- [Not](rules/Not.md) - [Not](rules/Not.md)
- [Nullable](rules/Nullable.md) - [Nullable](rules/Nullable.md)
- [OneOf](rules/OneOf.md) - [OneOf](rules/OneOf.md)
- [Optional](rules/Optional.md)
- [Property](rules/Property.md) - [Property](rules/Property.md)
- [PropertyOptional](rules/PropertyOptional.md) - [PropertyOptional](rules/PropertyOptional.md)
- [UndefOr](rules/UndefOr.md)
- [When](rules/When.md) - [When](rules/When.md)
## Numbers ## Numbers
@ -394,7 +394,7 @@
- [NotBlank](rules/NotBlank.md) - [NotBlank](rules/NotBlank.md)
- [NotEmoji](rules/NotEmoji.md) - [NotEmoji](rules/NotEmoji.md)
- [NotEmpty](rules/NotEmpty.md) - [NotEmpty](rules/NotEmpty.md)
- [NotOptional](rules/NotOptional.md) - [NotUndef](rules/NotUndef.md)
- [NullType](rules/NullType.md) - [NullType](rules/NullType.md)
- [Nullable](rules/Nullable.md) - [Nullable](rules/Nullable.md)
- [Number](rules/Number.md) - [Number](rules/Number.md)
@ -402,7 +402,6 @@
- [ObjectType](rules/ObjectType.md) - [ObjectType](rules/ObjectType.md)
- [Odd](rules/Odd.md) - [Odd](rules/Odd.md)
- [OneOf](rules/OneOf.md) - [OneOf](rules/OneOf.md)
- [Optional](rules/Optional.md)
- [PerfectSquare](rules/PerfectSquare.md) - [PerfectSquare](rules/PerfectSquare.md)
- [Pesel](rules/Pesel.md) - [Pesel](rules/Pesel.md)
- [Phone](rules/Phone.md) - [Phone](rules/Phone.md)
@ -438,6 +437,7 @@
- [Tld](rules/Tld.md) - [Tld](rules/Tld.md)
- [TrueVal](rules/TrueVal.md) - [TrueVal](rules/TrueVal.md)
- [Type](rules/Type.md) - [Type](rules/Type.md)
- [UndefOr](rules/UndefOr.md)
- [Unique](rules/Unique.md) - [Unique](rules/Unique.md)
- [Uploaded](rules/Uploaded.md) - [Uploaded](rules/Uploaded.md)
- [Uppercase](rules/Uppercase.md) - [Uppercase](rules/Uppercase.md)

View file

@ -29,5 +29,5 @@ See also:
- [CreditCard](CreditCard.md) - [CreditCard](CreditCard.md)
- [NotBlank](NotBlank.md) - [NotBlank](NotBlank.md)
- [NotEmpty](NotEmpty.md) - [NotEmpty](NotEmpty.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [Optional](Optional.md) - [UndefOr](UndefOr.md)

View file

@ -41,7 +41,7 @@ See also:
- [NoWhitespace](NoWhitespace.md) - [NoWhitespace](NoWhitespace.md)
- [NotEmpty](NotEmpty.md) - [NotEmpty](NotEmpty.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [NullType](NullType.md) - [NullType](NullType.md)
- [Number](Number.md) - [Number](Number.md)
- [Optional](Optional.md) - [UndefOr](UndefOr.md)

View file

@ -53,7 +53,7 @@ See also:
- [Min](Min.md) - [Min](Min.md)
- [NoWhitespace](NoWhitespace.md) - [NoWhitespace](NoWhitespace.md)
- [NotBlank](NotBlank.md) - [NotBlank](NotBlank.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [NullType](NullType.md) - [NullType](NullType.md)
- [Number](Number.md) - [Number](Number.md)
- [Optional](Optional.md) - [UndefOr](UndefOr.md)

View file

@ -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)

51
docs/rules/NotUndef.md Normal file
View file

@ -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)

View file

@ -30,12 +30,12 @@ See also:
- [IntType](IntType.md) - [IntType](IntType.md)
- [NotBlank](NotBlank.md) - [NotBlank](NotBlank.md)
- [NotEmpty](NotEmpty.md) - [NotEmpty](NotEmpty.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [Nullable](Nullable.md) - [Nullable](Nullable.md)
- [Number](Number.md) - [Number](Number.md)
- [ObjectType](ObjectType.md) - [ObjectType](ObjectType.md)
- [Optional](Optional.md)
- [ResourceType](ResourceType.md) - [ResourceType](ResourceType.md)
- [StringType](StringType.md) - [StringType](StringType.md)
- [StringVal](StringVal.md) - [StringVal](StringVal.md)
- [Type](Type.md) - [Type](Type.md)
- [UndefOr](UndefOr.md)

View file

@ -24,4 +24,4 @@ Version | Description
See also: See also:
- [NullType](NullType.md) - [NullType](NullType.md)
- [Optional](Optional.md) - [UndefOr](UndefOr.md)

View file

@ -32,7 +32,7 @@ See also:
- [IntType](IntType.md) - [IntType](IntType.md)
- [NotBlank](NotBlank.md) - [NotBlank](NotBlank.md)
- [NotEmpty](NotEmpty.md) - [NotEmpty](NotEmpty.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [NullType](NullType.md) - [NullType](NullType.md)
- [NumericVal](NumericVal.md) - [NumericVal](NumericVal.md)
- [ObjectType](ObjectType.md) - [ObjectType](ObjectType.md)

View file

@ -40,6 +40,6 @@ See also:
- [NoWhitespace](NoWhitespace.md) - [NoWhitespace](NoWhitespace.md)
- [NotBlank](NotBlank.md) - [NotBlank](NotBlank.md)
- [NotEmpty](NotEmpty.md) - [NotEmpty](NotEmpty.md)
- [NotOptional](NotOptional.md) - [NotUndef](NotUndef.md)
- [NullType](NullType.md) - [NullType](NullType.md)
- [Nullable](Nullable.md) - [Nullable](Nullable.md)

View file

@ -255,6 +255,8 @@ interface ChainedKey
public function keyNotOptional(int|string $key): ChainedValidator; public function keyNotOptional(int|string $key): ChainedValidator;
public function keyNotUndef(int|string $key): ChainedValidator;
public function keyNullType(int|string $key): ChainedValidator; public function keyNullType(int|string $key): ChainedValidator;
public function keyNumber(int|string $key): ChainedValidator; public function keyNumber(int|string $key): ChainedValidator;

View file

@ -242,8 +242,6 @@ interface ChainedNullOr
public function nullOrNotEmpty(): ChainedValidator; public function nullOrNotEmpty(): ChainedValidator;
public function nullOrNotOptional(): ChainedValidator;
public function nullOrNullType(): ChainedValidator; public function nullOrNullType(): ChainedValidator;
public function nullOrNumber(): ChainedValidator; public function nullOrNumber(): ChainedValidator;

View file

@ -267,6 +267,8 @@ interface ChainedProperty
public function propertyNotOptional(string $propertyName): ChainedValidator; public function propertyNotOptional(string $propertyName): ChainedValidator;
public function propertyNotUndef(string $propertyName): ChainedValidator;
public function propertyNullType(string $propertyName): ChainedValidator; public function propertyNullType(string $propertyName): ChainedValidator;
public function propertyNumber(string $propertyName): ChainedValidator; public function propertyNumber(string $propertyName): ChainedValidator;

View file

@ -246,8 +246,6 @@ interface ChainedUndefOr
public function undefOrNotEmpty(): ChainedValidator; public function undefOrNotEmpty(): ChainedValidator;
public function undefOrNotOptional(): ChainedValidator;
public function undefOrNullType(): ChainedValidator; public function undefOrNullType(): ChainedValidator;
public function undefOrNumber(): ChainedValidator; public function undefOrNumber(): ChainedValidator;

View file

@ -253,6 +253,8 @@ interface ChainedValidator extends
public function notOptional(): ChainedValidator; public function notOptional(): ChainedValidator;
public function notUndef(): ChainedValidator;
public function nullOr(Validatable $rule): ChainedValidator; public function nullOr(Validatable $rule): ChainedValidator;
public function nullType(): ChainedValidator; public function nullType(): ChainedValidator;

View file

@ -259,6 +259,8 @@ interface StaticKey
public static function keyNotOptional(int|string $key): ChainedValidator; 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 keyNullType(int|string $key): ChainedValidator;
public static function keyNumber(int|string $key): ChainedValidator; public static function keyNumber(int|string $key): ChainedValidator;

View file

@ -258,8 +258,6 @@ interface StaticNullOr
public static function nullOrNotEmpty(): ChainedValidator; public static function nullOrNotEmpty(): ChainedValidator;
public static function nullOrNotOptional(): ChainedValidator;
public static function nullOrNullType(): ChainedValidator; public static function nullOrNullType(): ChainedValidator;
public static function nullOrNumber(): ChainedValidator; public static function nullOrNumber(): ChainedValidator;

View file

@ -291,6 +291,8 @@ interface StaticProperty
public static function propertyNotOptional(string $propertyName): ChainedValidator; public static function propertyNotOptional(string $propertyName): ChainedValidator;
public static function propertyNotUndef(string $propertyName): ChainedValidator;
public static function propertyNullType(string $propertyName): ChainedValidator; public static function propertyNullType(string $propertyName): ChainedValidator;
public static function propertyNumber(string $propertyName): ChainedValidator; public static function propertyNumber(string $propertyName): ChainedValidator;

View file

@ -258,8 +258,6 @@ interface StaticUndefOr
public static function undefOrNotEmpty(): ChainedValidator; public static function undefOrNotEmpty(): ChainedValidator;
public static function undefOrNotOptional(): ChainedValidator;
public static function undefOrNullType(): ChainedValidator; public static function undefOrNullType(): ChainedValidator;
public static function undefOrNumber(): ChainedValidator; public static function undefOrNumber(): ChainedValidator;

View file

@ -256,6 +256,8 @@ interface StaticValidator extends
public static function notOptional(): ChainedValidator; public static function notOptional(): ChainedValidator;
public static function notUndef(): ChainedValidator;
public static function nullOr(Validatable $rule): ChainedValidator; public static function nullOr(Validatable $rule): ChainedValidator;
public static function nullType(): ChainedValidator; public static function nullType(): ChainedValidator;

View file

@ -24,7 +24,7 @@ use Respect\Validation\Rules\Core\Standard;
'{{name}} must be optional', '{{name}} must be optional',
self::TEMPLATE_NAMED, self::TEMPLATE_NAMED,
)] )]
final class NotOptional extends Standard final class NotUndef extends Standard
{ {
use CanValidateUndefined; use CanValidateUndefined;

View file

@ -15,6 +15,7 @@ use function in_array;
final class Aliases implements Transformer final class Aliases implements Transformer
{ {
private const ALIASES = [ private const ALIASES = [
'notOptional' => 'notUndef',
'nullable' => 'nullOr', 'nullable' => 'nullOr',
'optional' => 'undefOr', 'optional' => 'undefOr',
]; ];

View file

@ -30,7 +30,7 @@ final class Prefix implements Transformer
'notBlank', 'notBlank',
'notEmoji', 'notEmoji',
'notEmpty', 'notEmpty',
'notOptional', 'notUndef',
'nullOr', 'nullOr',
'property', 'property',
'propertyExists', 'propertyExists',

View file

@ -1,27 +0,0 @@
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
exceptionMessage(static fn() => 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

View file

@ -0,0 +1,27 @@
--FILE--
<?php
declare(strict_types=1);
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
exceptionMessage(static fn() => 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

View file

@ -15,13 +15,13 @@ use Respect\Validation\Test\RuleTestCase;
use stdClass; use stdClass;
#[Group('rule')] #[Group('rule')]
#[CoversClass(NotOptional::class)] #[CoversClass(NotUndef::class)]
final class NotOptionalTest extends RuleTestCase final class NotUndefTest extends RuleTestCase
{ {
/** @return iterable<array{NotOptional, mixed}> */ /** @return iterable<array{NotUndef, mixed}> */
public static function providerForValidInput(): iterable public static function providerForValidInput(): iterable
{ {
$rule = new NotOptional(); $rule = new NotUndef();
return [ return [
[$rule, []], [$rule, []],
@ -41,10 +41,10 @@ final class NotOptionalTest extends RuleTestCase
]; ];
} }
/** @return iterable<array{NotOptional, mixed}> */ /** @return iterable<array{NotUndef, mixed}> */
public static function providerForInvalidInput(): iterable public static function providerForInvalidInput(): iterable
{ {
$rule = new NotOptional(); $rule = new NotUndef();
return [ return [
[$rule, null], [$rule, null],

View file

@ -44,7 +44,7 @@ final class UndefOrTest extends RuleTestCase
} }
#[Test] #[Test]
public function itShouldUseWrappedRuleToEvaluateWhenNotOptional(): void public function itShouldUseWrappedRuleToEvaluateWhenNotUndef(): void
{ {
$input = new stdClass(); $input = new stdClass();

View file

@ -18,6 +18,19 @@ use Respect\Validation\Test\Transformers\StubTransformer;
#[CoversClass(Aliases::class)] #[CoversClass(Aliases::class)]
final class AliasesTest extends TestCase 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] #[Test]
public function itShouldConvertOptionalIntoUndefOr(): void public function itShouldConvertOptionalIntoUndefOr(): void
{ {

View file

@ -96,7 +96,7 @@ final class PrefixTest extends TestCase
'notBlank' => ['notBlank'], 'notBlank' => ['notBlank'],
'notEmoji' => ['notEmoji'], 'notEmoji' => ['notEmoji'],
'notEmpty' => ['notEmpty'], 'notEmpty' => ['notEmpty'],
'notOptional' => ['notOptional'], 'notUndef' => ['notUndef'],
'property' => ['property'], 'property' => ['property'],
'propertyExists' => ['propertyExists'], 'propertyExists' => ['propertyExists'],
'propertyOptional' => ['propertyOptional'], 'propertyOptional' => ['propertyOptional'],