mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Do not accept whitespace by default in "Alnum" rule
The "Alnum" rule is supposed to validate alphanumeric values, but instead, it also validates any whitespace character as valid. The rule also accepts a list of characters on its constructor, so it the users intentionally want some specific characters to also be allowed it is better than they also defined these characters on the rule's constructor. While refactoring the rule I could notice that "AbstractCtypeRule" is just an overhead that does not add much to it, so instead of extending it "Alnum" now extends "AbstractFilterRule" directly (which is the parent of "AbstractCtypeRule"). And since we want all rules to follow our contribution guidelines, this commit also make sure the "Alnum" rule is in accordance with that. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
fb9d3a6918
commit
779c0c1503
|
@ -3,42 +3,33 @@
|
|||
- `Alnum()`
|
||||
- `Alnum(string $additionalChars)`
|
||||
|
||||
Validates alphanumeric characters from a-Z and 0-9.
|
||||
Validates whether the input is alphanumeric or not.
|
||||
|
||||
Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
|
||||
characters.
|
||||
|
||||
```php
|
||||
v::alnum()->validate('foo 123'); // true
|
||||
v::alnum()->validate('number 100%'); // false
|
||||
v::alnum('%')->validate('number 100%'); // true
|
||||
v::alnum()->validate('foo 123'); // false
|
||||
v::alnum(' ')->validate('foo 123'); // true
|
||||
v::alnum()->validate('100%'); // false
|
||||
v::alnum('%')->validate('100%'); // true
|
||||
```
|
||||
|
||||
Because this rule allows whitespaces by default, you can separate additional
|
||||
characters with a whitespace:
|
||||
You can restrict case using the [Lowercase](Lowercase.md) and
|
||||
[Uppercase](Uppercase.md) rules.
|
||||
|
||||
```php
|
||||
v::alnum('- ! :')->validate('foo :- 123 !'); // true
|
||||
v::alnum()->uppercase()->validate('example'); // false
|
||||
```
|
||||
|
||||
This validator allows whitespace, if you want to
|
||||
remove them add `->noWhitespace()` to the chain:
|
||||
|
||||
```php
|
||||
v::alnum()->noWhitespace()->validate('foo 123'); // false
|
||||
```
|
||||
|
||||
You can restrict case using the `->lowercase()` and
|
||||
`->uppercase()` validators:
|
||||
|
||||
```php
|
||||
v::alnum()->uppercase()->validate('aaa'); // false
|
||||
```
|
||||
|
||||
Message template for this validator includes `{{additionalChars}}` as
|
||||
the string of extra chars passed as the parameter.
|
||||
Message template for this validator includes `{{additionalChars}}` as the string
|
||||
of extra chars passed as the parameter.
|
||||
|
||||
## Changelog
|
||||
|
||||
Version | Description
|
||||
--------|-------------
|
||||
2.0.0 | Removed support to whitespaces by default
|
||||
0.3.9 | Created
|
||||
|
||||
***
|
||||
|
|
|
@ -13,8 +13,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AlnumException extends AlphaException
|
||||
/**
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class AlnumException extends AlphaException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must contain only letters (a-z) and digits (0-9)',
|
||||
|
|
|
@ -13,14 +13,24 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Alnum extends AbstractCtypeRule
|
||||
{
|
||||
protected function filter($input)
|
||||
{
|
||||
return $this->filterWhiteSpaceOption($input);
|
||||
}
|
||||
use function ctype_alnum;
|
||||
|
||||
protected function ctypeFunction($input)
|
||||
/**
|
||||
* Validates whether the input is alphanumeric or not.
|
||||
*
|
||||
* Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
|
||||
* characters.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
*/
|
||||
final class Alnum extends AbstractFilterRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function validateClean($input)
|
||||
{
|
||||
return ctype_alnum($input);
|
||||
}
|
||||
|
|
66
tests/integration/rules/alnum.phpt
Normal file
66
tests/integration/rules/alnum.phpt
Normal file
|
@ -0,0 +1,66 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AlnumException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alnum()->check('abc%1');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alnum(' ')->check('abc%2');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alnum())->check('abcd3');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alnum('% '))->check('abc%4');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alnum()->assert('abc^1');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alnum())->assert('abcd2');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::alnum('* &%')->assert('abc^3');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::alnum('^'))->assert('abc^4');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"abc%1" must contain only letters (a-z) and digits (0-9)
|
||||
"abc%2" must contain only letters (a-z), digits (0-9) and " "
|
||||
"abcd3" must not contain letters (a-z) or digits (0-9)
|
||||
"abc%4" must not contain letters (a-z), digits (0-9) or "% "
|
||||
- "abc^1" must contain only letters (a-z) and digits (0-9)
|
||||
- "abcd2" must not contain letters (a-z) or digits (0-9)
|
||||
- "abc^3" must contain only letters (a-z), digits (0-9) and "* &%"
|
||||
- "abc^4" must not contain letters (a-z), digits (0-9) or "^"
|
|
@ -1,11 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
v::alnum()->assert('Bla 123');
|
||||
v::alnum()->check('Bla 123');
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,17 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AlnumException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alnum()->check('Bla %123');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"Bla %123" must contain only letters (a-z) and digits (0-9)
|
|
@ -1,18 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::alnum()->noWhitespace()->assert('Bla %1#%&23');
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- All of the required rules must pass for "Bla %1#%&23"
|
||||
- "Bla %1#%&23" must contain only letters (a-z) and digits (0-9)
|
||||
- "Bla %1#%&23" must not contain whitespace
|
|
@ -1,17 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AlnumException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::alnum())->check('adsfASDF123');
|
||||
} catch (AlnumException $exception) {
|
||||
echo $exception->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
"adsfASDF123" must not contain letters (a-z) or digits (0-9)
|
|
@ -1,15 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\AllOfException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::not(v::alnum())->assert('asd124SF');
|
||||
} catch (AllOfException $exception) {
|
||||
echo $exception->getFullMessage();
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
- "asd124SF" must not contain letters (a-z) or digits (0-9)
|
|
@ -1,12 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
if (!v::alnum('-')->validate('bla - bla')) {
|
||||
echo 'ok';
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,12 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
if (v::alnum()->notEmpty()->validate('')) {
|
||||
echo 'error';
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -1,12 +0,0 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
if (!v::alnum()->uppercase()->validate('ASDF')) {
|
||||
echo 'ok';
|
||||
}
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
|
@ -13,39 +13,23 @@ 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\AlnumException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\Alnum
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
* @author Islam Elshobokshy <islam20088@hotmail.com>
|
||||
* @author Nick Lombard <github@jigsoft.co.za>
|
||||
* @author Pascal Borreli <pascal@borreli.com>
|
||||
*/
|
||||
class AlnumTest extends TestCase
|
||||
final class AlnumTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerForValidAlnum
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function validAlnumCharsShouldReturnTrue($validAlnum, $additional): void
|
||||
{
|
||||
$validator = new Alnum($additional);
|
||||
self::assertTrue($validator->validate($validAlnum));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlnum
|
||||
* @expectedException \Respect\Validation\Exceptions\AlnumException
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidAlnumCharsShouldThrowAlnumExceptionAndReturnFalse($invalidAlnum, $additional): void
|
||||
{
|
||||
$validator = new Alnum($additional);
|
||||
self::assertFalse($validator->validate($invalidAlnum));
|
||||
$validator->assert($invalidAlnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidParams
|
||||
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
||||
|
@ -54,71 +38,63 @@ class AlnumTest extends TestCase
|
|||
*/
|
||||
public function invalidConstructorParamsShouldThrowComponentExceptionUponInstantiation($additional): void
|
||||
{
|
||||
$validator = new Alnum($additional);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerAdditionalChars
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function additionalCharsShouldBeRespected($additional, $query): void
|
||||
{
|
||||
$validator = new Alnum($additional);
|
||||
self::assertTrue($validator->validate($query));
|
||||
}
|
||||
|
||||
public function providerAdditionalChars()
|
||||
{
|
||||
return [
|
||||
['!@#$%^&*(){}', '!@#$%^&*(){} abc 123'],
|
||||
['[]?+=/\\-_|"\',<>.', "[]?+=/\\-_|\"',<>. \t \n abc 123"],
|
||||
];
|
||||
new Alnum($additional);
|
||||
}
|
||||
|
||||
public function providerForInvalidParams()
|
||||
{
|
||||
return [
|
||||
[new \stdClass()],
|
||||
[new stdClass()],
|
||||
[[]],
|
||||
[0x2],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForValidAlnum()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
return [
|
||||
['alganet', ''],
|
||||
['foo :- 123 !', '- ! :'],
|
||||
['number 100%', '%'],
|
||||
['0alg-anet0', '0-9'],
|
||||
['1', ''],
|
||||
["\t", ''],
|
||||
["\n", ''],
|
||||
['a', ''],
|
||||
['foobar', ''],
|
||||
['rubinho_', '_'],
|
||||
['google.com', '.'],
|
||||
['alganet alganet', ''],
|
||||
["\nabc", ''],
|
||||
["\tdef", ''],
|
||||
["\nabc \t", ''],
|
||||
[0, ''],
|
||||
[new Alnum(), 'alganet'],
|
||||
[new Alnum('- ! :'), 'foo :- 123 !'],
|
||||
[new Alnum('0-9'), '0alg-anet0'],
|
||||
[new Alnum(), '1'],
|
||||
[new Alnum(), 'a'],
|
||||
[new Alnum(), 'foobar'],
|
||||
[new Alnum('_'), 'rubinho_'],
|
||||
[new Alnum('.'), 'google.com'],
|
||||
[new Alnum(' '), 'alganet alganet'],
|
||||
[new Alnum(), 0],
|
||||
[new Alnum('!@#$%^&*(){}'), '!@#$%^&*(){}abc123'],
|
||||
[new Alnum('[]?+=/\\-_|"\',<>.'), '[]?+=/\\-_|"\',<>.abc123'],
|
||||
[new Alnum("[]?+=/\\-_|\"',<>. \t\n"), "abc[]?+=/\\-_|\"',<>. \t\n123"],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForInvalidAlnum()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
return [
|
||||
['', ''],
|
||||
['number 100%', ''],
|
||||
['@#$', ''],
|
||||
['_', ''],
|
||||
['dgç', ''],
|
||||
[1e21, ''], //evaluates to "1.0E+21"
|
||||
[null, ''],
|
||||
[new \stdClass(), ''],
|
||||
[[], ''],
|
||||
[new Alnum(), ''],
|
||||
[new Alnum(), 'number 100%'],
|
||||
[new Alnum('%'), 'number 100%'],
|
||||
[new Alnum(), '@#$'],
|
||||
[new Alnum(), '_'],
|
||||
[new Alnum(), 'dgç'],
|
||||
[new Alnum(), 1e21],
|
||||
[new Alnum(), null],
|
||||
[new Alnum(), new stdClass()],
|
||||
[new Alnum(), []],
|
||||
[new Alnum('%'), 'number 100%'],
|
||||
[new Alnum(), "\t"],
|
||||
[new Alnum(), "\n"],
|
||||
[new Alnum(), "\nabc"],
|
||||
[new Alnum(), "\tdef"],
|
||||
[new Alnum(), "\nabc \t"],
|
||||
[new Alnum(), 'alganet alganet'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue