mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-19 01:41:36 +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;
|
namespace App\ValidationRules;
|
||||||
|
|
||||||
use Cron\CronExpression;
|
use Cron\CronExpression;
|
||||||
use Illuminate\Contracts\Validation\Rule;
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
class CronRule implements Rule
|
class CronRule implements ValidationRule
|
||||||
{
|
{
|
||||||
private bool $acceptCustom;
|
private bool $acceptCustom;
|
||||||
|
|
||||||
@ -14,13 +14,14 @@ public function __construct(bool $acceptCustom = false)
|
|||||||
$this->acceptCustom = $acceptCustom;
|
$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');
|
if (CronExpression::isValidExpression($value)) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
public function message(): string
|
if ($this->acceptCustom && $value === 'custom') {
|
||||||
{
|
return;
|
||||||
return __('Invalid frequency');
|
}
|
||||||
|
$fail('Invalid frequency')->translate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,19 @@
|
|||||||
|
|
||||||
namespace App\ValidationRules;
|
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) {
|
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);
|
return;
|
||||||
}
|
}
|
||||||
|
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 true;
|
return;
|
||||||
}
|
}
|
||||||
|
$fail('Domain is not valid')->translate();
|
||||||
public function message(): string
|
|
||||||
{
|
|
||||||
return __('Domain is not valid');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,27 +2,16 @@
|
|||||||
|
|
||||||
namespace App\ValidationRules;
|
namespace App\ValidationRules;
|
||||||
|
|
||||||
use Illuminate\Contracts\Validation\Rule;
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
class RestrictedIPAddressesRule implements Rule
|
class RestrictedIPAddressesRule implements ValidationRule
|
||||||
{
|
{
|
||||||
/**
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
* Determine if the validation rule passes.
|
|
||||||
*
|
|
||||||
* @param string $attribute
|
|
||||||
* @param mixed $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function passes($attribute, $value)
|
|
||||||
{
|
{
|
||||||
return ! in_array($value, config('core.restricted_ip_addresses'));
|
if (! in_array($value, config('core.restricted_ip_addresses'))) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
/**
|
$fail('IP address is restricted')->translate();
|
||||||
* @return array|\Illuminate\Contracts\Translation\Translator|string|null
|
|
||||||
*/
|
|
||||||
public function message()
|
|
||||||
{
|
|
||||||
return __('IP address is restricted.');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,35 +2,21 @@
|
|||||||
|
|
||||||
namespace App\ValidationRules;
|
namespace App\ValidationRules;
|
||||||
|
|
||||||
use Illuminate\Contracts\Validation\Rule;
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
use phpseclib3\Crypt\PublicKeyLoader;
|
use phpseclib3\Crypt\PublicKeyLoader;
|
||||||
use phpseclib3\Exception\NoKeyLoadedException;
|
use phpseclib3\Exception\NoKeyLoadedException;
|
||||||
|
|
||||||
class SshKeyRule implements Rule
|
class SshKeyRule implements ValidationRule
|
||||||
{
|
{
|
||||||
/**
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
* Determine if the validation rule passes.
|
|
||||||
*
|
|
||||||
* @param string $attribute
|
|
||||||
* @param mixed $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function passes($attribute, $value)
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
PublicKeyLoader::load($value);
|
PublicKeyLoader::load($value);
|
||||||
|
|
||||||
return true;
|
return;
|
||||||
} catch (NoKeyLoadedException $e) {
|
} catch (NoKeyLoadedException) {
|
||||||
return false;
|
$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