Skip to content

Commit

Permalink
update: added support for ascii limited alpha rules
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Nov 11, 2024
1 parent 5a23fe0 commit fb5eec6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 21 deletions.
35 changes: 29 additions & 6 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,55 @@ public static function afterOrEqual(string|DateTimeInterface $dateOrField): stri
}

/**
* The field under validation must be entirely alphabetic characters.
* The field under validation must be entirely Unicode alphabetic characters contained in *\p{L}* and *\p{M}*.
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha
*/
public static function alpha(): string
public static function alpha(?bool $limitToAscii = null): string
{
if ($limitToAscii) {
return 'alpha:ascii';
}

return 'alpha';
}

/**
* The field under validation may have alpha-numeric characters, as well as dashes and underscores.
* The field under validation must be entirely Unicode alpha-numeric characters contained in *\p{L}*, *\p{M}*,
* *\p{N}*, as well as ASCII dashes (*-*) and ASCII underscores (*_*).
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha-dash
*/
public static function alphaDash(): string
public static function alphaDash(?bool $limitToAscii = null): string
{
if ($limitToAscii) {
return 'alpha_dash:ascii';
}

return 'alpha_dash';
}

/**
* The field under validation must be entirely alpha-numeric characters.
* The field under validation must be entirely Unicode alpha-numeric characters contained in *\p{L}*, *\p{M}*, and
* *\p{N}*.
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha-num
*/
public static function alphaNum(): string
public static function alphaNum(?bool $limitToAscii = null): string
{
if ($limitToAscii) {
return 'alpha_num:ascii';
}

return 'alpha_num';
}

Expand Down
29 changes: 20 additions & 9 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,44 @@ public function afterOrEqual(string|DateTimeInterface $dateOrField): self
}

/**
* The field under validation must be entirely alphabetic characters.
* The field under validation must be entirely Unicode alphabetic characters contained in *\p{L}* and *\p{M}*.
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha
*/
public function alpha(): self
public function alpha(?bool $limitToAscii = null): self
{
return $this->rule(Rule::alpha());
return $this->rule(Rule::alpha($limitToAscii));
}

/**
* The field under validation may have alpha-numeric characters, as well as dashes and underscores.
* The field under validation must be entirely Unicode alpha-numeric characters contained in *\p{L}*, *\p{M}*,
* *\p{N}*, as well as ASCII dashes (*-*) and ASCII underscores (*_*).
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha-dash
*/
public function alphaDash(): self
public function alphaDash(?bool $limitToAscii = null): self
{
return $this->rule(Rule::alphaDash());
return $this->rule(Rule::alphaDash($limitToAscii));
}

/**
* The field under validation must be entirely alpha-numeric characters.
* The field under validation must be entirely Unicode alpha-numeric characters contained in *\p{L}*, *\p{M}*, and
* *\p{N}*.
*
* To restrict this validation rule to characters in the ASCII range (*a-z* and *A-Z*), use the *limitToAscii*
* argument.
*
* @link https://laravel.com/docs/11.x/validation#rule-alpha-num
*/
public function alphaNum(): self
public function alphaNum(?bool $limitToAscii = null): self
{
return $this->rule(Rule::alphaNum());
return $this->rule(Rule::alphaNum($limitToAscii));
}

/**
Expand Down
42 changes: 36 additions & 6 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,35 +279,65 @@ public static function ruleDataProvider(): array
'fails' => true,
],
'alpha valid' => [
'data' => 'alpha',
'data' => 'álpha',
'rules' => fn() => RuleSet::create()->alpha(),
'fails' => false,
],
'alpha invalid' => [
'data' => 'not-alpha',
'data' => 'not-álpha',
'rules' => fn() => RuleSet::create()->alpha(),
'fails' => true,
],
'alpha limited to ascii valid' => [
'data' => 'alpha',
'rules' => fn() => RuleSet::create()->alpha(limitToAscii: true),
'fails' => false,
],
'alpha limited to ascii invalid' => [
'data' => 'álpha',
'rules' => fn() => RuleSet::create()->alpha(limitToAscii: true),
'fails' => true,
],
'alphaDash valid' => [
'data' => 'still-alpha',
'data' => 'still-álpha',
'rules' => fn() => RuleSet::create()->alphaDash(),
'fails' => false,
],
'alphaDash invalid' => [
'data' => 'not/alpha',
'data' => 'not/álpha',
'rules' => fn() => RuleSet::create()->alphaDash(),
'fails' => true,
],
'alphaDash limited to ascii valid' => [
'data' => 'still-alpha',
'rules' => fn() => RuleSet::create()->alphaDash(limitToAscii: true),
'fails' => false,
],
'alphaDash limited to ascii invalid' => [
'data' => 'not-álpha',
'rules' => fn() => RuleSet::create()->alphaDash(limitToAscii: true),
'fails' => true,
],
'alphaNum valid' => [
'data' => 'alpha1',
'data' => 'álpha1',
'rules' => fn() => RuleSet::create()->alphaNum(),
'fails' => false,
],
'alphaNum invalid' => [
'data' => 'not-alpha1',
'data' => 'not-álpha1',
'rules' => fn() => RuleSet::create()->alphaNum(),
'fails' => true,
],
'alphaNum limited to ascii valid' => [
'data' => 'alpha1',
'rules' => fn() => RuleSet::create()->alphaNum(limitToAscii: true),
'fails' => false,
],
'alphaNum limited to ascii invalid' => [
'data' => 'álpha1',
'rules' => fn() => RuleSet::create()->alphaNum(limitToAscii: true),
'fails' => true,
],
'array valid' => [
'data' => [
'field' => ['value'],
Expand Down

0 comments on commit fb5eec6

Please sign in to comment.