-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from mehrancodes/validate-subdomain-pattern
Validate subdomain pattern
- Loading branch information
Showing
6 changed files
with
106 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class BranchNameRegex implements DataAwareRule, ValidationRule | ||
{ | ||
protected array $data = []; | ||
|
||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$pattern = $this->data['subdomain_pattern']; | ||
|
||
if (! empty($pattern) && ! preg_match($pattern, $value)) { | ||
$fail('The subdomain regex pattern must be match with the branch name.'); | ||
} | ||
} | ||
|
||
public function setData(array $data): static | ||
{ | ||
$this->data = $data; | ||
|
||
return $this; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -180,7 +180,7 @@ | |
*/ | ||
|
||
'attributes' => [ | ||
'db_name' => 'FORGE_DB_NAME' | ||
'db_name' => 'FORGE_DB_NAME', | ||
], | ||
|
||
]; |
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,36 @@ | ||
<?php | ||
|
||
use App\Rules\BranchNameRegex; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
test('it validates the subdomain pattern matches the branch name', function ($data, $failed) { | ||
$validator = Validator::make($data, [ | ||
'branch' => ['required', new BranchNameRegex], | ||
'subdomain_pattern' => ['string'], | ||
]); | ||
|
||
expect($validator->fails())->toBe($failed); | ||
}) | ||
->with([ | ||
[ | ||
'actual' => [ | ||
'branch' => 'user-notification', | ||
'subdomain_pattern' => '/feature\/(.+)/i', | ||
], | ||
'failed' => true, | ||
], | ||
[ | ||
'actual' => [ | ||
'branch' => 'feature/user-notification', | ||
'subdomain_pattern' => '/feature\/(.+)/i', | ||
], | ||
'failed' => false, | ||
], | ||
[ | ||
'actual' => [ | ||
'branch' => 'feature/user-notification', | ||
'subdomain_pattern' => '', | ||
], | ||
'failed' => false, | ||
], | ||
]); |