-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
265 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StellarWP\Validation\Commands; | ||
|
||
/** | ||
* Returning this command from ValidationRule::__invoke() tells the Validator to skip all subsequent rules. | ||
* | ||
* @unreleased | ||
*/ | ||
class SkipValidationRules | ||
{ | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StellarWP\Validation\Rules; | ||
|
||
use Closure; | ||
use StellarWP\Validation\Commands\SkipValidationRules; | ||
use StellarWP\Validation\Contracts\ValidatesOnFrontEnd; | ||
use StellarWP\Validation\Contracts\ValidationRule; | ||
|
||
/** | ||
* This rule skips further validation if the field is null. It is similar to Optional, but the only allowed value is | ||
* null. | ||
* | ||
* @unreleased | ||
*/ | ||
class Nullable implements ValidationRule, ValidatesOnFrontEnd | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'nullable'; | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function fromString(string $options = null): ValidationRule | ||
{ | ||
return new self(); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @return SkipValidationRules|void | ||
*/ | ||
public function __invoke($value, Closure $fail, string $key, array $values) | ||
{ | ||
if ($value === null) { | ||
return new SkipValidationRules(); | ||
} | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function serializeOption() | ||
{ | ||
return null; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StellarWP\Validation\Rules; | ||
|
||
use Closure; | ||
use StellarWP\Validation\Commands\SkipValidationRules; | ||
use StellarWP\Validation\Contracts\ValidatesOnFrontEnd; | ||
use StellarWP\Validation\Contracts\ValidationRule; | ||
|
||
/** | ||
* This rule marks a field as optional and skips further validation if the rule is either null or an empty string. | ||
* | ||
* @unreleased | ||
*/ | ||
class Optional implements ValidationRule, ValidatesOnFrontEnd | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'optional'; | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public static function fromString(string $options = null): ValidationRule | ||
{ | ||
return new self(); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* | ||
* @return SkipValidationRules|void | ||
*/ | ||
public function __invoke($value, Closure $fail, string $key, array $values) | ||
{ | ||
if ($value === null || $value === '') { | ||
return new SkipValidationRules(); | ||
} | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function serializeOption() | ||
{ | ||
return null; | ||
} | ||
} |
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
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StellarWP\Validation\Tests\Unit\Rules; | ||
|
||
use StellarWP\Validation\Commands\SkipValidationRules; | ||
use StellarWP\Validation\Rules\Nullable; | ||
use StellarWP\Validation\Tests\TestCase; | ||
|
||
class NullableTest extends TestCase | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function testNullableValidation() | ||
{ | ||
$rule = new Nullable(); | ||
|
||
// Passes when value is null and skips remaining tests | ||
self::assertValidationRulePassed($rule, null); | ||
self::assertValidationRuleDoesReturnCommandInstance($rule, SkipValidationRules::class, null); | ||
|
||
// Passes on any other value but does not skip remaining tests | ||
self::assertValidationRulePassed($rule, 'bar'); | ||
self::assertValidationRuleDoesNotReturnCommandInstance($rule, SkipValidationRules::class, 'bar'); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StellarWP\Validation\Tests\Unit\Rules; | ||
|
||
use StellarWP\Validation\Commands\SkipValidationRules; | ||
use StellarWP\Validation\Rules\Optional; | ||
use StellarWP\Validation\Tests\TestCase; | ||
|
||
class OptionalTest extends TestCase | ||
{ | ||
/** | ||
* @unreleased | ||
*/ | ||
public function testNullableValidation() | ||
{ | ||
$rule = new Optional(); | ||
|
||
// Passes when value is null and skips remaining tests | ||
self::assertValidationRulePassed($rule, null); | ||
self::assertValidationRuleDoesReturnCommandInstance($rule, SkipValidationRules::class, null); | ||
|
||
// Passes when value is empty string and skips remaining tests | ||
self::assertValidationRulePassed($rule, ''); | ||
self::assertValidationRuleDoesReturnCommandInstance($rule, SkipValidationRules::class, ''); | ||
|
||
// Passes on any other value but does not skip remaining tests | ||
self::assertValidationRulePassed($rule, 'bar'); | ||
self::assertValidationRuleDoesNotReturnCommandInstance($rule, SkipValidationRules::class, 'bar'); | ||
} | ||
} |
Oops, something went wrong.