mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
Do not accept whitespace by default in "Alpha" rule
The intent of the "Alpha" rule is to validate alphabetic values. However, it also considers any whitespace character (by default). That causes some confusion, and unless you check its code or tests, you would never expect that behavior. Because of that confusion, I decided to make "Alpha" to not consider whitespace characters as valid, and since in the constructor of this rule it's possible to add extra characters to the validation it makes sense to let the user decide whether they want whitespaces, tabs, new lines, etc. or not. This rule, as the same as "Alnum" previously, extends "AbstractCtypeRule" pretty much to only make it easier to consider any whitespaces as valid, therefore I saw no reason to keep extending it. Now "Alpha" extends the "AbstractFilterRule" which is the parent of "AbstractCtypeRule". I also took the opportunity to apply our contribution guidelines to "Alpha" since we want to apply that to all the rules. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
3601adf6db
commit
27bd5d204d
|
@ -3,12 +3,28 @@
|
|||
- `Alpha()`
|
||||
- `Alpha(string $additionalChars)`
|
||||
|
||||
This is similar to `Alnum()`, but it doesn't allow numbers.
|
||||
Validates whether the input contains only alphabetic characters. This is similar
|
||||
to [Alnum](Alnum.md), but it does not allow numbers.
|
||||
|
||||
```php
|
||||
v::alpha()->validate('some name'); // false
|
||||
v::alpha(' ')->validate('some name'); // true
|
||||
v::alpha()->validate('Cedric-Fabian'); // false
|
||||
v::alpha('-')->validate('Cedric-Fabian'); // true
|
||||
```
|
||||
|
||||
You can restrict case using the [Lowercase](Lowercase.md) and
|
||||
[Uppercase](Uppercase.md) rules.
|
||||
|
||||
```php
|
||||
v::alpha()->uppercase()->validate('example'); // false
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Removed support to whitespaces by default
|
||||
0.3.9 | Created
|
||||
|
||||
***
|
||||
|
|
|
@ -13,16 +13,23 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AlphaException extends FilteredValidationException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class AlphaException extends FilteredValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain only letters (a-z)',
|
||||
self::EXTRA => '{{name}} must contain only letters (a-z) and "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must contain only letters (a-z) and {{additionalChars}}',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not contain letters (a-z)',
|
||||
self::EXTRA => '{{name}} must not contain letters (a-z) or "{{additionalChars}}"',
|
||||
self::EXTRA => '{{name}} must not contain letters (a-z) or {{additionalChars}}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,14 +13,21 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Alpha extends AbstractCtypeRule
|
||||
{
|
||||
protected function filter($input)
|
||||
{
|
||||
return $this->filterWhiteSpaceOption($input);
|
||||
}
|
||||
use function ctype_alpha;
|
||||
|
||||
protected function ctypeFunction($input)
|
||||
/**
|
||||
* Validates whether the input contains only alphabetic characters.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
final class Alpha extends AbstractFilterRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function validateClean($input)
|
||||
{
|
||||
return ctype_alpha($input);
|
||||
}
|
||||
|
|
66
tests/integration/rules/alpha.phpt
Normal file
66
tests/integration/rules/alpha.phpt
Normal file
|
@ -0,0 +1,66 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AlphaException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alpha()->check('aaa%a');
|
||||
} catch (AlphaException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alpha(' ')->check('bbb%b');
|
||||
} catch (AlphaException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alpha())->check('ccccc');
|
||||
} catch (AlphaException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alpha('% '))->check('ddd%d');
|
||||
} catch (AlphaException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alpha()->assert('eee^e');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alpha())->assert('fffff');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alpha('* &%')->assert('ggg^g');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alpha('^'))->assert('hhh^h');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECT--
|
||||
"aaa%a" must contain only letters (a-z)
|
||||
"bbb%b" must contain only letters (a-z) and " "
|
||||
"ccccc" must not contain letters (a-z)
|
||||
"ddd%d" must not contain letters (a-z) or "% "
|
||||
- "eee^e" must contain only letters (a-z)
|
||||
- "fffff" must not contain letters (a-z)
|
||||
- "ggg^g" must contain only letters (a-z) and "* &%"
|
||||
- "hhh^h" must not contain letters (a-z) or "^"
|
|
@ -1,11 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::alpha()->assert('abc');
|
||||
v::alpha()->check('abc');
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\AlphaException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alpha()->check(1);
|
||||
} catch (AlphaException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alpha()->assert(2);
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
1 must contain only letters (a-z)
|
||||
- 2 must contain only letters (a-z)
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\AlphaException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alpha()->setName('Field')->check(null);
|
||||
} catch (AlphaException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alpha()->setName('Field')->assert('');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
Field must contain only letters (a-z)
|
||||
- Field must contain only letters (a-z)
|
|
@ -1,23 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Exceptions\AlphaException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::alpha())->check('a');
|
||||
} catch (AlphaException $e) {
|
||||
echo $e->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alpha())->assert('b');
|
||||
} catch (AllOfException $e) {
|
||||
echo $e->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"a" must not contain letters (a-z)
|
||||
- "b" must not contain letters (a-z)
|
|
@ -13,41 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\AlphaException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Alpha
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Pascal Borreli <pascal@borreli.com>
|
||||
*/
|
||||
class AlphaTest extends TestCase
|
||||
final class AlphaTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidAlpha
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function validAlphanumericCharsShouldReturnTrue($validAlpha, $additional): void
|
||||
{
|
||||
$validator = new Alpha($additional);
|
||||
self::assertTrue($validator->validate($validAlpha));
|
||||
$validator->check($validAlpha);
|
||||
$validator->assert($validAlpha);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlpha
|
||||
* @expectedException \Respect\Validation\Exceptions\AlphaException
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidAlphanumericCharsShouldThrowAlphaException($invalidAlpha, $additional): void
|
||||
{
|
||||
$validator = new Alpha($additional);
|
||||
self::assertFalse($validator->validate($invalidAlpha));
|
||||
$validator->assert($invalidAlpha);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
||||
|
@ -56,71 +37,51 @@ class AlphaTest extends TestCase
|
|||
*/
|
||||
public function invalidConstructorParamsShouldThrowComponentException($additional): void
|
||||
{
|
||||
$validator = new Alpha($additional);
|
||||
new Alpha($additional);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerAdditionalChars
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function additionalCharsShouldBeRespected($additional, $query): void
|
||||
{
|
||||
$validator = new Alpha($additional);
|
||||
self::assertTrue($validator->validate($query));
|
||||
}
|
||||
|
||||
public function providerAdditionalChars()
|
||||
public function providerForInvalidParams(): array
|
||||
{
|
||||
return [
|
||||
['!@#$%^&*(){}', '!@#$%^&*(){} abc'],
|
||||
['[]?+=/\\-_|"\',<>.', "[]?+=/\\-_|\"',<>. \t \n abc"],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return [
|
||||
[new \stdClass()],
|
||||
[new stdClass()],
|
||||
[[]],
|
||||
[0x2],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForValidAlpha()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
['alganet', ''],
|
||||
['alganet', 'alganet'],
|
||||
['0alg-anet0', '0-9'],
|
||||
['a', ''],
|
||||
["\t", ''],
|
||||
["\n", ''],
|
||||
['foobar', ''],
|
||||
['rubinho_', '_'],
|
||||
['google.com', '.'],
|
||||
['alganet alganet', ''],
|
||||
["\nabc", ''],
|
||||
["\tdef", ''],
|
||||
["\nabc \t", ''],
|
||||
'alphabetic' => [new Alpha(), 'alganet'],
|
||||
'alphabetic with one exception' => [new Alpha('.'), 'google.com'],
|
||||
'alphabetic with multiple exceptions' => [new Alpha('0-9'), '0alg-anet9'],
|
||||
'non-alphabetic with only exceptions' => [new Alpha('!@#$%^&*(){}'), '!@#$%^&*(){}'],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidAlpha()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['', ''],
|
||||
['@#$', ''],
|
||||
['_', ''],
|
||||
['dgç', ''],
|
||||
['122al', ''],
|
||||
['122', ''],
|
||||
[11123, ''],
|
||||
[1e21, ''],
|
||||
[0, ''],
|
||||
[null, ''],
|
||||
[new \stdClass(), ''],
|
||||
[[], ''],
|
||||
'empty string' => [new Alpha(), ''],
|
||||
'symbols' => [new Alpha(), '@#$'],
|
||||
'underscore' => [new Alpha(), '_'],
|
||||
'non ASCII chars' => [new Alpha(), 'dgç'],
|
||||
'alphanumeric' => [new Alpha(), '122al'],
|
||||
'digits as string' => [new Alpha(), '122'],
|
||||
'integers' => [new Alpha(), 11123],
|
||||
'zero' => [new Alpha(), 0],
|
||||
'null' => [new Alpha(), null],
|
||||
'object' => [new Alpha(), new stdClass()],
|
||||
'array' => [new Alpha(), []],
|
||||
'newline' => [new Alpha(), "\nabc"],
|
||||
'tab' => [new Alpha(), "\tdef"],
|
||||
'alphabetic with spaces' => [new Alpha(), 'alganet alganet'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue