diff --git a/docs/03-rules.md b/docs/03-rules.md index a435d51..7d5bd3b 100644 --- a/docs/03-rules.md +++ b/docs/03-rules.md @@ -14,6 +14,7 @@ - [Count](03-rules_count.md) - [IsNull](03-rules_is-null.md) - [NotBlank](03-rules_not-blank.md) +- [NotNull](03-rules_not-null.md) - [Type](03-rules_type.md) ## String Rules diff --git a/docs/03-rules_is-null.md b/docs/03-rules_is-null.md index 00afcf9..0325250 100644 --- a/docs/03-rules_is-null.md +++ b/docs/03-rules_is-null.md @@ -2,6 +2,8 @@ Validates that a value is `null`. +Check the [NotNull](03-rules_not-null.md) rule for the opposite validation. + ```php IsNull( ?string $message = null diff --git a/docs/03-rules_not-null.md b/docs/03-rules_not-null.md new file mode 100644 index 0000000..b249142 --- /dev/null +++ b/docs/03-rules_not-null.md @@ -0,0 +1,36 @@ +# NotNull + +Validates that a value is not `null`. + +Check the [IsNull](03-rules_is-null.md) rule for the opposite validation. + +```php +NotNull( + ?string $message = null +); +``` + +## Basic Usage + +```php +// anything else will be true +Validator::notNull()->validate(null); // false +``` + +## Options + +### `message` + +type: `?string` default: `The {{ name }} value should not be null.` + +Message that will be shown if the value is not null. + +The following parameters are available: + +| Parameter | Description | +|---------------|---------------------------| +| `{{ name }}` | Name of the invalid value | + +## Changelog + +- `1.3.0` Created \ No newline at end of file diff --git a/src/ChainedValidatorInterface.php b/src/ChainedValidatorInterface.php index 8aec8f0..035cf9b 100644 --- a/src/ChainedValidatorInterface.php +++ b/src/ChainedValidatorInterface.php @@ -121,6 +121,10 @@ public function notBlank( ?string $message = null ): ChainedValidatorInterface&Validator; + public function notNull( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public function optional( Validator $validator ): ChainedValidatorInterface&Validator; diff --git a/src/Exception/NotNullException.php b/src/Exception/NotNullException.php new file mode 100644 index 0000000..8dded16 --- /dev/null +++ b/src/Exception/NotNullException.php @@ -0,0 +1,5 @@ +message = $message ?? $this->message; + } + + public function assert(mixed $value, ?string $name = null): void + { + if (Validator::isNull()->validate($value) === true) { + throw new NotNullException( + message: $this->message, + parameters: [ + 'name' => $name + ] + ); + } + } +} \ No newline at end of file diff --git a/src/StaticValidatorInterface.php b/src/StaticValidatorInterface.php index 7b759c8..f72dc2b 100644 --- a/src/StaticValidatorInterface.php +++ b/src/StaticValidatorInterface.php @@ -120,6 +120,10 @@ public static function notBlank( ?string $message = null ): ChainedValidatorInterface&Validator; + public static function notNull( + ?string $message = null + ): ChainedValidatorInterface&Validator; + public static function optional( Validator $validator ): ChainedValidatorInterface&Validator; diff --git a/tests/NotNullTest.php b/tests/NotNullTest.php new file mode 100644 index 0000000..c73d9b7 --- /dev/null +++ b/tests/NotNullTest.php @@ -0,0 +1,43 @@ + [new NotNull(), null, $exception, $message]; + } + + public static function provideRuleSuccessConditionData(): \Generator + { + yield 'int' => [new NotNull(), 1]; + yield 'string' => [new NotNull(), 'string']; + yield 'bool' => [new NotNull(), true]; + yield 'array' => [new NotNull(), []]; + } + + public static function provideRuleMessageOptionData(): \Generator + { + yield 'message' => [ + new NotNull( + message: '{{ name }}' + ), + null, + 'test' + ]; + } +} \ No newline at end of file