Refactor validation rules to implement the new ValidationRule interface (#249)

This commit is contained in:
Manuel Christlieb
2024-07-03 00:20:07 +02:00
committed by GitHub
parent 11e3b167cc
commit 3d67153912
4 changed files with 35 additions and 61 deletions

View File

@ -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();
}
}