From d974bbd34bf81f80f1e7ec6902f4f87686fd09ec Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Mon, 23 Jan 2023 14:39:19 +0900 Subject: [PATCH 1/5] add ascii validation rules --- .../Concerns/ValidatesAttributes.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 361b7e58de66..21a258bf2e38 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -380,6 +380,50 @@ public function validateAlphaNum($attribute, $value) return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0; } + /** + * Validate that an attribute contains only ASCII alphabetic characters. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function validateAsciiAlpha($attribute, $value) + { + return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value); + } + + /** + * Validate that an attribute contains only ASCII alpha-numeric characters, dashes, and underscores. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function validateAsciiAlphaDash($attribute, $value) + { + if (! is_string($value) && ! is_numeric($value)) { + return false; + } + + return preg_match('/^[a-zA-Z0-9_-]+$/u', $value) > 0; + } + + /** + * Validate that an attribute contains only ASCII alpha-numeric characters. + * + * @param string $attribute + * @param mixed $value + * @return bool + */ + public function validateAsciiAlphaNum($attribute, $value) + { + if (! is_string($value) && ! is_numeric($value)) { + return false; + } + + return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0; + } + /** * Validate that an attribute is an array. * From 9fdd5cc7536d58e196ffbc97ce50bf2be11a04cd Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Mon, 23 Jan 2023 14:44:10 +0900 Subject: [PATCH 2/5] add test --- tests/Validation/ValidationValidatorTest.php | 89 ++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 21b2da108d28..fcf9b7c751a4 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4497,6 +4497,95 @@ public function testValidateAlphaDash() $this->assertTrue($v->passes()); } + public function testValidateAsciiAlpha() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'AsciiAlpha']); + $this->assertTrue($v->passes()); + + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, [ + 'x' => 'aslsdlks +1 +1', + ], ['x' => 'Alpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => '❤'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => '123'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 123], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'abc123'], ['x' => 'AsciiAlpha']); + $this->assertFalse($v->passes()); + } + + public function testValidateAsciiAlphaNum() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AsciiAlphaNum']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AsciiAlphaNum']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と123'], ['x' => 'AsciiAlphaNum']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => '१२३'], ['x' => 'AsciiAlphaNum']); // numbers in Hindi + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AsciiAlphaNum']); // eastern arabic numerals + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AsciiAlphaNum']); + $this->assertFalse($v->passes()); + } + + public function testValidateAsciiAlphaDash() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AsciiAlphaDash']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AsciiAlphaDash']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と-_123'], ['x' => 'AsciiAlphaDash']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AsciiAlphaDash']); + $this->assertFalse($v->passes()); + + $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AsciiAlphaDash']); // eastern arabic numerals + $this->assertFalse($v->passes()); + } + public function testValidateTimezone() { $trans = $this->getIlluminateArrayTranslator(); From 10be7365800b2208b54df1094706db56f36cd815 Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Mon, 23 Jan 2023 23:12:50 +0900 Subject: [PATCH 3/5] refactor rules --- .../Concerns/ValidatesAttributes.php | 64 ++++++------------- 1 file changed, 18 insertions(+), 46 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 21a258bf2e38..d59c71f25fb5 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -338,90 +338,62 @@ protected function getDateTime($value) /** * Validate that an attribute contains only alphabetic characters. + * If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters * * @param string $attribute * @param mixed $value * @return bool */ - public function validateAlpha($attribute, $value) + public function validateAlpha($attribute, $value, $parameters) { + if (isset($parameters[0]) && $parameters[0] === 'ascii') { + return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value); + } + return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value); } /** * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores. + * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters, + * dashes, and underscores. * * @param string $attribute * @param mixed $value * @return bool */ - public function validateAlphaDash($attribute, $value) + public function validateAlphaDash($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } - return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0; - } - - /** - * Validate that an attribute contains only alpha-numeric characters. - * - * @param string $attribute - * @param mixed $value - * @return bool - */ - public function validateAlphaNum($attribute, $value) - { - if (! is_string($value) && ! is_numeric($value)) { - return false; + if (isset($parameters[0]) && $parameters[0] === 'ascii') { + return preg_match('/^[a-zA-Z0-9_-]+$/u', $value) > 0; } - return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0; - } - - /** - * Validate that an attribute contains only ASCII alphabetic characters. - * - * @param string $attribute - * @param mixed $value - * @return bool - */ - public function validateAsciiAlpha($attribute, $value) - { - return is_string($value) && preg_match('/^[a-zA-Z]+$/u', $value); + return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0; } /** - * Validate that an attribute contains only ASCII alpha-numeric characters, dashes, and underscores. + * Validate that an attribute contains only alpha-numeric characters. + * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters. * * @param string $attribute * @param mixed $value * @return bool */ - public function validateAsciiAlphaDash($attribute, $value) + public function validateAlphaNum($attribute, $value, $parameters) { if (! is_string($value) && ! is_numeric($value)) { return false; } - return preg_match('/^[a-zA-Z0-9_-]+$/u', $value) > 0; - } - - /** - * Validate that an attribute contains only ASCII alpha-numeric characters. - * - * @param string $attribute - * @param mixed $value - * @return bool - */ - public function validateAsciiAlphaNum($attribute, $value) - { - if (! is_string($value) && ! is_numeric($value)) { - return false; + if (isset($parameters[0]) && $parameters[0] === 'ascii') { + return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0; } - return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0; + return preg_match('/^[\pL\pM\pN]+$/u', $value) > 0; } /** From a067b4e22f727f453f392a839ab72b93337bf165 Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Mon, 23 Jan 2023 23:13:12 +0900 Subject: [PATCH 4/5] fix tests --- tests/Validation/ValidationValidatorTest.php | 54 ++++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index fcf9b7c751a4..46219fd4b273 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -4497,10 +4497,10 @@ public function testValidateAlphaDash() $this->assertTrue($v->passes()); } - public function testValidateAsciiAlpha() + public function testValidateAlphaWithAsciiOption() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'aslsdlks'], ['x' => 'Alpha:ascii']); $this->assertTrue($v->passes()); $trans = $this->getIlluminateArrayTranslator(); @@ -4508,81 +4508,81 @@ public function testValidateAsciiAlpha() 'x' => 'aslsdlks 1 1', - ], ['x' => 'Alpha']); + ], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'http://google.com'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'ユニコード を基盤技術と'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'आपका स्वागत है'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'Continuación'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'ofreció su dimisión'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => '❤'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => '❤'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => '123'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => '123'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 123], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 123], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'abc123'], ['x' => 'AsciiAlpha']); + $v = new Validator($trans, ['x' => 'abc123'], ['x' => 'Alpha:ascii']); $this->assertFalse($v->passes()); } - public function testValidateAsciiAlphaNum() + public function testValidateAlphaNumWithAsciiOption() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AsciiAlphaNum']); + $v = new Validator($trans, ['x' => 'asls13dlks'], ['x' => 'AlphaNum:ascii']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AsciiAlphaNum']); + $v = new Validator($trans, ['x' => 'http://g232oogle.com'], ['x' => 'AlphaNum:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と123'], ['x' => 'AsciiAlphaNum']); + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と123'], ['x' => 'AlphaNum:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => '१२३'], ['x' => 'AsciiAlphaNum']); // numbers in Hindi + $v = new Validator($trans, ['x' => '१२३'], ['x' => 'AlphaNum:ascii']); // numbers in Hindi $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AsciiAlphaNum']); // eastern arabic numerals + $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaNum:ascii']); // eastern arabic numerals $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AsciiAlphaNum']); + $v = new Validator($trans, ['x' => 'नमस्कार'], ['x' => 'AlphaNum:ascii']); $this->assertFalse($v->passes()); } - public function testValidateAsciiAlphaDash() + public function testValidateAlphaDashWithAsciiOption() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AsciiAlphaDash']); + $v = new Validator($trans, ['x' => 'asls1-_3dlks'], ['x' => 'AlphaDash:ascii']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AsciiAlphaDash']); + $v = new Validator($trans, ['x' => 'http://-g232oogle.com'], ['x' => 'AlphaDash:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と-_123'], ['x' => 'AsciiAlphaDash']); + $v = new Validator($trans, ['x' => 'ユニコードを基盤技術と-_123'], ['x' => 'AlphaDash:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AsciiAlphaDash']); + $v = new Validator($trans, ['x' => 'नमस्कार-_'], ['x' => 'AlphaDash:ascii']); $this->assertFalse($v->passes()); - $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AsciiAlphaDash']); // eastern arabic numerals + $v = new Validator($trans, ['x' => '٧٨٩'], ['x' => 'AlphaDash:ascii']); // eastern arabic numerals $this->assertFalse($v->passes()); } From 6a15a4ebb01c295e8865f4c311a1532b552825ca Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Mon, 23 Jan 2023 23:17:59 +0900 Subject: [PATCH 5/5] fix docs --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index d59c71f25fb5..f17d4f083fa7 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -338,7 +338,7 @@ protected function getDateTime($value) /** * Validate that an attribute contains only alphabetic characters. - * If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters + * If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters. * * @param string $attribute * @param mixed $value