From 108a73f5557092bab4281101f7f8813760a14570 Mon Sep 17 00:00:00 2001 From: Tetsuro Yoshikawa Date: Mon, 11 Jan 2021 13:08:07 +0900 Subject: [PATCH 1/2] fix: treated as false if there is a newline at the end. test: Add a test case where the last string is a newline code when alpha_numeric_punct rule. fix: treated as false if there is a newline at the end. test: Add a test case where the last string is a newline code when alpha_numeric_space rule. fix: treated as false if there is a newline at the end. test: Add a test case where the last string is a newline code when decimal rule. fix: treated as false if there is a newline at the end. fix: treated as false if there is a newline at the end for alpha_numeric_space. test: Add a test case where the last string is a newline code when numeric rule. fix: treated as false if there is a newline at the end for numeric rule. test: Add a test case where the last string is a newline code when decimal rule. fix: treated as false if there is a newline at the end for alpha_dash rule. test: Add a test case where the last string is a newline code when integer rule. fix: treated as false if there is a newline at the end for integer rule. fix: typo. --- system/Validation/FormatRules.php | 14 +++++------ tests/system/Validation/FormatRulesTest.php | 28 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index 57b211be5efe..d089fc5ad8d5 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -44,7 +44,7 @@ public function alpha_space(?string $value = null): bool return true; } - return (bool) preg_match('/^[A-Z ]+$/i', $value); + return (bool) preg_match('/\A[A-Z ]+\z/i', $value); } /** @@ -56,7 +56,7 @@ public function alpha_space(?string $value = null): bool */ public function alpha_dash(?string $str = null): bool { - return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); + return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str); } /** @@ -72,7 +72,7 @@ public function alpha_dash(?string $str = null): bool */ public function alpha_numeric_punct($str) { - return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str); + return (bool) preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str); } /** @@ -96,7 +96,7 @@ public function alpha_numeric(?string $str = null): bool */ public function alpha_numeric_space(?string $str = null): bool { - return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str); + return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str); } /** @@ -123,7 +123,7 @@ public function string($str = null): bool */ public function decimal(?string $str = null): bool { - return (bool) preg_match('/^[-+]?[0-9]{0,}\.?[0-9]+$/', $str); + return (bool) preg_match('/\A[-+]?[0-9]{0,}\.?[0-9]+\z/', $str); } /** @@ -147,7 +147,7 @@ public function hex(?string $str = null): bool */ public function integer(?string $str = null): bool { - return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); + return (bool) preg_match('/\A[\-+]?[0-9]+\z/', $str); } /** @@ -181,7 +181,7 @@ public function is_natural_no_zero(?string $str = null): bool */ public function numeric(?string $str = null): bool { - return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); + return (bool) preg_match('/\A[\-+]?[0-9]*\.?[0-9]+\z/', $str); } /** diff --git a/tests/system/Validation/FormatRulesTest.php b/tests/system/Validation/FormatRulesTest.php index d7b171fe098a..56145c703201 100644 --- a/tests/system/Validation/FormatRulesTest.php +++ b/tests/system/Validation/FormatRulesTest.php @@ -424,6 +424,10 @@ public function alphaSpaceProvider() FormatRulesTest::ALPHABET . ' ', true, ], + [ + FormatRulesTest::ALPHABET . "\n", + false, + ], [ FormatRulesTest::ALPHABET . '1', false, @@ -506,6 +510,10 @@ public function alphaNumericPunctProvider() FormatRulesTest::ALPHANUMERIC . '`', false, ], + [ + FormatRulesTest::ALPHANUMERIC . "\n", + false, + ], [ FormatRulesTest::ALPHANUMERIC . '@', false, @@ -599,6 +607,10 @@ public function alphaNumericSpaceProvider() ' ' . FormatRulesTest::ALPHANUMERIC . '-', false, ], + [ + ' ' . FormatRulesTest::ALPHANUMERIC . "\n", + false, + ], [ null, false, @@ -636,6 +648,10 @@ public function alphaDashProvider() FormatRulesTest::ALPHANUMERIC . '-\ ', false, ], + [ + FormatRulesTest::ALPHANUMERIC . "-\n", + false, + ], [ null, false, @@ -722,6 +738,10 @@ public function numericProvider() '+42', true, ], + [ + "+42\n", + false, + ], [ '123a', false, @@ -771,6 +791,10 @@ public function integerProvider() '-1', true, ], + [ + "+42\n", + false, + ], [ '123a', false, @@ -824,6 +848,10 @@ public function decimalProvider() '0', true, ], + [ + "0\n", + false, + ], [ '1.0a', false, From 9bdba2eb1f78cdda805049ff36335518ef783e8a Mon Sep 17 00:00:00 2001 From: Tetsuro Yoshikawa Date: Sat, 16 Jan 2021 10:09:01 +0900 Subject: [PATCH 2/2] docs: add regex101.com links. --- system/Validation/FormatRules.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index d089fc5ad8d5..cefe16469b5b 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -44,6 +44,7 @@ public function alpha_space(?string $value = null): bool return true; } + // @see https://regex101.com/r/LhqHPO/1 return (bool) preg_match('/\A[A-Z ]+\z/i', $value); } @@ -56,7 +57,8 @@ public function alpha_space(?string $value = null): bool */ public function alpha_dash(?string $str = null): bool { - return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str); + // @see https://regex101.com/r/XfVY3d/1 + return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str); } /** @@ -72,6 +74,7 @@ public function alpha_dash(?string $str = null): bool */ public function alpha_numeric_punct($str) { + // @see https://regex101.com/r/6N8dDY/1 return (bool) preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str); } @@ -96,6 +99,7 @@ public function alpha_numeric(?string $str = null): bool */ public function alpha_numeric_space(?string $str = null): bool { + // @see https://regex101.com/r/0AZDME/1 return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str); } @@ -123,6 +127,7 @@ public function string($str = null): bool */ public function decimal(?string $str = null): bool { + // @see https://regex101.com/r/HULifl/1/ return (bool) preg_match('/\A[-+]?[0-9]{0,}\.?[0-9]+\z/', $str); } @@ -181,6 +186,7 @@ public function is_natural_no_zero(?string $str = null): bool */ public function numeric(?string $str = null): bool { + // @see https://regex101.com/r/bb9wtr/1 return (bool) preg_match('/\A[\-+]?[0-9]*\.?[0-9]+\z/', $str); } @@ -253,6 +259,7 @@ public function valid_json(string $str = null): bool */ public function valid_email(string $str = null): bool { + // @see https://regex101.com/r/wlJG1t/1/ if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches)) { $str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);