-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: require one regular character to submit posts, comments, and mes…
…sages
- Loading branch information
1 parent
d267bd9
commit cb8ae82
Showing
7 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Support\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class ContainsRegularCharacter implements ValidationRule | ||
{ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
// Check for at least one letter, number, punctuation mark, or symbol. | ||
$requireOneRegularCharacter = '/[\p{L}\p{N}\p{P}\p{S}]/u'; | ||
|
||
if (!preg_match($requireOneRegularCharacter, $value)) { | ||
$fail('validation.contains_regular_characters')->translate(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Rules; | ||
|
||
use App\Support\Rules\ContainsRegularCharacter; | ||
use Illuminate\Support\Facades\Validator; | ||
use Tests\TestCase; | ||
|
||
class ContainsRegularCharacterTest extends TestCase | ||
{ | ||
public function testItPassesWhenInputContainsRegularCharacters(): void | ||
{ | ||
$data = ['body' => 'This is a valid comment with letters and symbols!']; | ||
|
||
$validator = Validator::make($data, [ | ||
'body' => ['required', 'string', new ContainsRegularCharacter()], | ||
]); | ||
|
||
$this->assertFalse($validator->fails()); | ||
} | ||
|
||
public function testItFailsWhenInputOnlyContainsControlCharacters(): void | ||
{ | ||
$data = ['body' => "\u{200B}\u{200E}\u{200F}"]; | ||
|
||
$validator = Validator::make($data, [ | ||
'body' => ['required', 'string', new ContainsRegularCharacter()], | ||
]); | ||
|
||
$this->assertTrue($validator->fails()); | ||
} | ||
|
||
public function testItFailsWhenInputIsEmpty(): void | ||
{ | ||
$data = ['body' => '']; | ||
|
||
$validator = Validator::make($data, [ | ||
'body' => ['required', 'string', new ContainsRegularCharacter()], | ||
]); | ||
|
||
$this->assertTrue($validator->fails()); | ||
} | ||
|
||
public function testItPassesWhenInputIsOnlySymbols(): void | ||
{ | ||
$data = ['body' => '***!!!']; | ||
|
||
$validator = Validator::make($data, [ | ||
'body' => ['required', 'string', new ContainsRegularCharacter()], | ||
]); | ||
|
||
$this->assertFalse($validator->fails()); | ||
} | ||
} |