mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-17 17:01:37 +00:00
Refactor validation rules to implement the new ValidationRule interface (#249)
This commit is contained in:
parent
11e3b167cc
commit
3d67153912
@ -3,9 +3,9 @@
|
||||
namespace App\ValidationRules;
|
||||
|
||||
use Cron\CronExpression;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class CronRule implements Rule
|
||||
class CronRule implements ValidationRule
|
||||
{
|
||||
private bool $acceptCustom;
|
||||
|
||||
@ -14,13 +14,14 @@ public function __construct(bool $acceptCustom = false)
|
||||
$this->acceptCustom = $acceptCustom;
|
||||
}
|
||||
|
||||
public function passes($attribute, $value): bool
|
||||
public function validate(string $attribute, mixed $value, \Closure $fail): void
|
||||
{
|
||||
return CronExpression::isValidExpression($value) || ($this->acceptCustom && $value === 'custom');
|
||||
}
|
||||
|
||||
public function message(): string
|
||||
{
|
||||
return __('Invalid frequency');
|
||||
if (CronExpression::isValidExpression($value)) {
|
||||
return;
|
||||
}
|
||||
if ($this->acceptCustom && $value === 'custom') {
|
||||
return;
|
||||
}
|
||||
$fail('Invalid frequency')->translate();
|
||||
}
|
||||
}
|
||||
|
@ -2,21 +2,19 @@
|
||||
|
||||
namespace App\ValidationRules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class DomainRule implements Rule
|
||||
class DomainRule implements ValidationRule
|
||||
{
|
||||
public function passes($attribute, $value): bool
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if ($value) {
|
||||
return preg_match("/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/", $value);
|
||||
if (! $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function message(): string
|
||||
{
|
||||
return __('Domain is not valid');
|
||||
if (preg_match("/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/", $value) === 1) {
|
||||
return;
|
||||
}
|
||||
$fail('Domain is not valid')->translate();
|
||||
}
|
||||
}
|
||||
|
@ -2,27 +2,16 @@
|
||||
|
||||
namespace App\ValidationRules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class RestrictedIPAddressesRule implements Rule
|
||||
class RestrictedIPAddressesRule implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
return ! in_array($value, config('core.restricted_ip_addresses'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\Illuminate\Contracts\Translation\Translator|string|null
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return __('IP address is restricted.');
|
||||
if (! in_array($value, config('core.restricted_ip_addresses'))) {
|
||||
return;
|
||||
}
|
||||
$fail('IP address is restricted')->translate();
|
||||
}
|
||||
}
|
||||
|
@ -2,35 +2,21 @@
|
||||
|
||||
namespace App\ValidationRules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use phpseclib3\Crypt\PublicKeyLoader;
|
||||
use phpseclib3\Exception\NoKeyLoadedException;
|
||||
|
||||
class SshKeyRule implements Rule
|
||||
class SshKeyRule implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
try {
|
||||
PublicKeyLoader::load($value);
|
||||
|
||||
return true;
|
||||
} catch (NoKeyLoadedException $e) {
|
||||
return false;
|
||||
return;
|
||||
} catch (NoKeyLoadedException) {
|
||||
$fail('Invalid key')->translate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\Illuminate\Contracts\Translation\Translator|string|null
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return __('Invalid key');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user