mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
Create "NotOptional" rule
This commit is contained in:
parent
6f9a5771c2
commit
15b0a937b2
|
@ -18,6 +18,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
|
|||
- Create "KeyValue" rule (#441)
|
||||
- Create "Mimetype" rule (#361)
|
||||
- Create "NotBlank" rule (#443)
|
||||
- Create "NotOptional" rule (#448)
|
||||
- Create "Optional" rule (#423)
|
||||
- Create "ResourceType" rule (#397)
|
||||
- Create "ScalarVal" rule (#397)
|
||||
|
|
39
docs/NotOptional.md
Normal file
39
docs/NotOptional.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
# NotOptional
|
||||
|
||||
- `v::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(array()); // 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(array('')); // true
|
||||
v::notOptional()->validate(array(' ')); // true
|
||||
v::notOptional()->validate(array(0)); // true
|
||||
v::notOptional()->validate(array('0')); // true
|
||||
v::notOptional()->validate(array(false)); // true
|
||||
v::notOptional()->validate(array(array(''), array(0))); // true
|
||||
v::notOptional()->validate(new stdClass()); // true
|
||||
```
|
||||
|
||||
***
|
||||
See also:
|
||||
|
||||
* [NoWhitespace](NoWhitespace.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NullType](NullType.md)
|
||||
* [Optional](Optional.md)
|
|
@ -166,6 +166,7 @@
|
|||
* [MacAddress](MacAddress.md)
|
||||
* [NfeAccessKey](NfeAccessKey.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotOptional](NotOptional.md)
|
||||
* [Phone](Phone.md)
|
||||
* [Sf](Sf.md)
|
||||
* [Url](Url.md)
|
||||
|
@ -252,6 +253,7 @@
|
|||
* [Not](Not.md)
|
||||
* [NotBlank](NotBlank.md)
|
||||
* [NotEmpty](NotEmpty.md)
|
||||
* [NotOptional](NotOptional.md)
|
||||
* [NullType](NullType.md)
|
||||
* [Numeric](Numeric.md)
|
||||
* [ObjectType](ObjectType.md)
|
||||
|
|
34
library/Exceptions/NotOptionalException.php
Normal file
34
library/Exceptions/NotOptionalException.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotOptionalException extends ValidationException
|
||||
{
|
||||
const STANDARD = 0;
|
||||
const NAMED = 1;
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => 'The value must not be optional',
|
||||
self::NAMED => '{{name}} must not be optional',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => 'The value must be optional',
|
||||
self::NAMED => '{{name}} must be optional',
|
||||
),
|
||||
);
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
return $this->getName() == '' ? static::STANDARD : static::NAMED;
|
||||
}
|
||||
}
|
20
library/Rules/NotOptional.php
Normal file
20
library/Rules/NotOptional.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class NotOptional extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
{
|
||||
return (false === in_array($input, array(null, ''), true));
|
||||
}
|
||||
}
|
|
@ -89,6 +89,7 @@ use Respect\Validation\Rules\Key;
|
|||
* @method static Validator not(Validatable $rule)
|
||||
* @method static Validator notBlank()
|
||||
* @method static Validator notEmpty()
|
||||
* @method static Validator notOptional()
|
||||
* @method static Validator noWhitespace()
|
||||
* @method static Validator nullType()
|
||||
* @method static Validator numeric()
|
||||
|
|
12
tests/integration/notOptional_1.phpt
Normal file
12
tests/integration/notOptional_1.phpt
Normal file
|
@ -0,0 +1,12 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::notOptional()->check(0);
|
||||
v::notOptional()->assert(false);
|
||||
?>
|
||||
===DONE===
|
||||
--EXPECTF--
|
||||
===DONE===
|
23
tests/integration/notOptional_2.phpt
Normal file
23
tests/integration/notOptional_2.phpt
Normal file
|
@ -0,0 +1,23 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\NotOptionalException;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::notOptional()->check(null);
|
||||
} catch (NotOptionalException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notOptional()->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
null must not be optional
|
||||
\-"" must not be optional
|
23
tests/integration/notOptional_3.phpt
Normal file
23
tests/integration/notOptional_3.phpt
Normal file
|
@ -0,0 +1,23 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\NotOptionalException;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::notOptional()->setName('Field')->check(null);
|
||||
} catch (NotOptionalException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::notOptional()->setName('Field')->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
Field must not be optional
|
||||
\-Field must not be optional
|
23
tests/integration/notOptional_4.phpt
Normal file
23
tests/integration/notOptional_4.phpt
Normal file
|
@ -0,0 +1,23 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\NotOptionalException;
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
|
||||
try {
|
||||
v::not(v::notOptional())->check(0);
|
||||
} catch (NotOptionalException $e) {
|
||||
echo $e->getMainMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::notOptional())->assert(array());
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
The value must be optional
|
||||
\-{ } must be optional
|
69
tests/unit/Rules/NotOptionalTest.php
Normal file
69
tests/unit/Rules/NotOptionalTest.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Respect/Validation.
|
||||
*
|
||||
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
*
|
||||
* For the full copyright and license information, please view the "LICENSE.md"
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers Respect\Validation\Rules\NotOptional
|
||||
*/
|
||||
class NotOptionalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForNotOptional
|
||||
*/
|
||||
public function testShouldValidateWhenNotOptional($input)
|
||||
{
|
||||
$rule = new NotOptional();
|
||||
|
||||
$this->assertTrue($rule->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForOptional
|
||||
*/
|
||||
public function testShouldNotValidateWhenOptional($input)
|
||||
{
|
||||
$rule = new NotOptional();
|
||||
|
||||
$this->assertFalse($rule->validate($input));
|
||||
}
|
||||
|
||||
public function providerForNotOptional()
|
||||
{
|
||||
return array(
|
||||
array(array()),
|
||||
array(' '),
|
||||
array(0),
|
||||
array('0'),
|
||||
array(0),
|
||||
array('0.0'),
|
||||
array(false),
|
||||
array(array('')),
|
||||
array(array(' ')),
|
||||
array(array(0)),
|
||||
array(array('0')),
|
||||
array(array(false)),
|
||||
array(array(array(''), array(0))),
|
||||
array(new stdClass()),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForOptional()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue