Skip to content

Commit

Permalink
Do not accept whitespace by default in "Alnum" rule
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
henriquemoody committed Sep 3, 2018
1 parent fb9d3a6 commit 779c0c1
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 220 deletions.
35 changes: 13 additions & 22 deletions docs/rules/Alnum.md
Expand Up @@ -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.

```php
v::alnum()->validate('foo 123'); // true
v::alnum()->validate('number 100%'); // false
v::alnum('%')->validate('number 100%'); // true
```

Because this rule allows whitespaces by default, you can separate additional
characters with a whitespace:

```php
v::alnum('- ! :')->validate('foo :- 123 !'); // true
```

This validator allows whitespace, if you want to
remove them add `->noWhitespace()` to the chain:
Alphanumeric is a combination of alphabetic (a-z and A-Z) and numeric (0-9)
characters.

```php
v::alnum()->noWhitespace()->validate('foo 123'); // false
v::alnum()->validate('foo 123'); // false
v::alnum(' ')->validate('foo 123'); // true
v::alnum()->validate('100%'); // false
v::alnum('%')->validate('100%'); // true
```

You can restrict case using the `->lowercase()` and
`->uppercase()` validators:
You can restrict case using the [Lowercase](Lowercase.md) and
[Uppercase](Uppercase.md) rules.

```php
v::alnum()->uppercase()->validate('aaa'); // false
v::alnum()->uppercase()->validate('example'); // 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

***
Expand Down
9 changes: 8 additions & 1 deletion library/Exceptions/AlnumException.php
Expand Up @@ -13,8 +13,15 @@

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)',
Expand Down
24 changes: 17 additions & 7 deletions library/Rules/Alnum.php
Expand Up @@ -13,14 +13,24 @@

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);
}
Expand Down
66 changes: 66 additions & 0 deletions tests/integration/rules/alnum.phpt
@@ -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 "^"
11 changes: 0 additions & 11 deletions tests/integration/rules/alnum_1.phpt

This file was deleted.

17 changes: 0 additions & 17 deletions tests/integration/rules/alnum_2.phpt

This file was deleted.

18 changes: 0 additions & 18 deletions tests/integration/rules/alnum_3.phpt

This file was deleted.

17 changes: 0 additions & 17 deletions tests/integration/rules/alnum_4.phpt

This file was deleted.

15 changes: 0 additions & 15 deletions tests/integration/rules/alnum_5.phpt

This file was deleted.

12 changes: 0 additions & 12 deletions tests/integration/rules/alnum_6_expected_char.phpt

This file was deleted.

12 changes: 0 additions & 12 deletions tests/integration/rules/alnum_7_not_empty.phpt

This file was deleted.

12 changes: 0 additions & 12 deletions tests/integration/rules/alnum_8_uppercase.phpt

This file was deleted.

0 comments on commit 779c0c1

Please sign in to comment.