-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from programmatordev/YAPV-66-create-optional-rule
Create Optional rule
- Loading branch information
Showing
9 changed files
with
178 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Optional | ||
|
||
Validates only if value is *not* `null`. | ||
|
||
```php | ||
Optional( | ||
Validator $validator, | ||
); | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```php | ||
Validator::optional( | ||
Validator::type('int')->greaterThanEqualTo(18) | ||
)->validate(null); // true | ||
|
||
Validator::optional( | ||
Validator::type('int')->greaterThanEqualTo(18) | ||
)->validate(20); // true | ||
|
||
Validator::optional( | ||
Validator::type('int')->greaterThanEqualTo(18) | ||
)->validate(16); // false | ||
``` | ||
|
||
## Options | ||
|
||
### `validator` | ||
|
||
type: `Validator` `required` | ||
|
||
Validator that will validate the input value only when it is *not* `null`. | ||
|
||
## Changelog | ||
|
||
- `1.0.0` Created |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Rule; | ||
|
||
use ProgrammatorDev\Validator\Validator; | ||
|
||
class Optional extends AbstractRule implements RuleInterface | ||
{ | ||
public function __construct( | ||
private readonly Validator $validator | ||
) {} | ||
|
||
public function assert(mixed $value, ?string $name = null): void | ||
{ | ||
// validate only if value is not null | ||
if ($value === null) { | ||
return; | ||
} | ||
|
||
$this->validator->assert($value, $name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace ProgrammatorDev\Validator\Test; | ||
|
||
use ProgrammatorDev\Validator\Exception\NotBlankException; | ||
use ProgrammatorDev\Validator\Rule\NotBlank; | ||
use ProgrammatorDev\Validator\Rule\Optional; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleFailureConditionTrait; | ||
use ProgrammatorDev\Validator\Test\Util\TestRuleSuccessConditionTrait; | ||
use ProgrammatorDev\Validator\Validator; | ||
|
||
class OptionalTest extends AbstractTest | ||
{ | ||
use TestRuleFailureConditionTrait; | ||
use TestRuleSuccessConditionTrait; | ||
|
||
public static function provideRuleFailureConditionData(): \Generator | ||
{ | ||
yield 'default' => [ | ||
new Optional( | ||
new Validator(new NotBlank()) | ||
), | ||
'', | ||
NotBlankException::class, | ||
'/The (.*) value should not be blank, (.*) given\./' | ||
]; | ||
} | ||
|
||
public static function provideRuleSuccessConditionData(): \Generator | ||
{ | ||
yield 'default' => [ | ||
new Optional( | ||
new Validator(new NotBlank()) | ||
), | ||
null | ||
]; | ||
} | ||
} |