Add a regex validation rule for the restrictRule setting

This commit is contained in:
Bubka 2024-09-20 08:30:39 +02:00
parent 6102a4b3f6
commit 012af3177b
3 changed files with 50 additions and 8 deletions

View File

@ -3,6 +3,7 @@
namespace App\Api\v1\Requests;
use App\Rules\IsValidEmailList;
use App\Rules\IsValidRegex;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
@ -25,14 +26,28 @@ public function authorize()
*/
public function rules()
{
$routeParam = $this->route()?->parameter('settingName');
if ($routeParam == 'restrictList') {
$rule = [
'value' => [
new IsValidEmailList,
],
];
}
else if ($routeParam == 'restrictRule') {
$rule = [
'value' => [
new IsValidRegex,
],
];
}
else {
$rule = [
'value' => [
'required',
],
];
if ($this->route()?->parameter('settingName') == 'restrictList') {
$rule['value'][] = new IsValidEmailList;
}
return $rule;

View File

@ -0,0 +1,26 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class IsValidRegex implements ValidationRule
{
/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail) : void
{
try {
preg_match('/' . $value . '/', '');
if (preg_last_error() !== PREG_NO_ERROR) {
$fail('validation.IsValidRegex')->translate();
}
}
catch (\Throwable $ex) {
$fail('validation.IsValidRegex')->translate();
}
}
}

View File

@ -143,6 +143,7 @@
'single' => 'When using :attribute it must be the only parameter in this request body',
'onlyCustomOtpWithUri' => 'The uri parameter must be provided alone or only in combination with the \'custom_otp\' parameter',
'IsValidRegex' => 'The :attribute must be a valid regex pattern.',
/*
|--------------------------------------------------------------------------
@ -187,7 +188,7 @@
],
'ids' => [
'regex' => 'IDs must be comma separated, without trailing comma.',
]
],
],
/*