diff --git a/app/Rules/NoBlankCharacters.php b/app/Rules/NoBlankCharacters.php index c9eeadb7e..c938fae13 100644 --- a/app/Rules/NoBlankCharacters.php +++ b/app/Rules/NoBlankCharacters.php @@ -9,23 +9,17 @@ final readonly class NoBlankCharacters implements ValidationRule { - private const string BLANK_CHARACTERS_PATTERN = '/^[\s\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{2028}\x{205F}\x{3000}]*$/u'; - - private const string FORMAT_CHARACTERS_PATTERN = '/\p{Cf}/u'; - /** - * Validate the value of the given attribute. + * Run the validation rule. * - * @param string $attribute The name of the attribute being validated - * @param mixed $value The value of the attribute * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ public function validate(string $attribute, mixed $value, Closure $fail): void { $value = type($value)->asString(); - if (preg_match(self::BLANK_CHARACTERS_PATTERN, $value) || preg_match(self::FORMAT_CHARACTERS_PATTERN, $value)) { - $fail('The :attribute field cannot be empty or contain only blank characters.'); + if (preg_match("/\p{Cf}/u", $value)) { + $fail('The :attribute field cannot contain blank characters.'); } } } diff --git a/tests/Unit/Rules/NoBlankCharactersTest.php b/tests/Unit/Rules/NoBlankCharactersTest.php index a58722849..2228ce750 100644 --- a/tests/Unit/Rules/NoBlankCharactersTest.php +++ b/tests/Unit/Rules/NoBlankCharactersTest.php @@ -4,18 +4,14 @@ use App\Rules\NoBlankCharacters; -test('validation fails for strings containing blank characters', function (string $name) { +test('with blank characters', function (string $name) { $rule = new NoBlankCharacters; - $fail = function (string $errorMessage) { - // Capture the error message - $this->errorMessage = $errorMessage; - }; + $fail = fn (string $errorMessage) => $this->fail($errorMessage); - // Validate the input $rule->validate('name', $name, $fail); - expect(isset($this->errorMessage))->toBeTrue(); + expect(true)->toBeFalse(); })->with([ "\u{200E}", "\u{200E}\u{200E}", @@ -26,18 +22,7 @@ "测试\u{200E}", "ⓣⓔⓢⓣ\u{200E}", ' ', - "\u{2005}", - "\u{2006}", - "\u{2007}", - "\u{2008}", - "\u{2009}", - "\u{200A}", - "\u{2028}", - "\u{205F}", - "\u{3000}", - " \u{2005} ", - "\u{2007}\u{2008}\u{2009}", -]); +])->fails(); test('without blank characters', function (string $name) { $rule = new NoBlankCharacters;