diff --git a/application/Espo/Services/ScheduledJob.php b/application/Espo/Classes/FieldValidators/ScheduledJob/Scheduling/Valid.php similarity index 68% rename from application/Espo/Services/ScheduledJob.php rename to application/Espo/Classes/FieldValidators/ScheduledJob/Scheduling/Valid.php index a69dbb80f6..c4abbfbfa7 100644 --- a/application/Espo/Services/ScheduledJob.php +++ b/application/Espo/Classes/FieldValidators/ScheduledJob/Scheduling/Valid.php @@ -27,40 +27,36 @@ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ -namespace Espo\Services; +namespace Espo\Classes\FieldValidators\ScheduledJob\Scheduling; use Cron\CronExpression; -use Espo\Core\Utils\DateTime; -use Espo\Entities\ScheduledJob as ScheduledJobEntity; +use Espo\Core\FieldValidation\Validator; +use Espo\Core\FieldValidation\Validator\Data; +use Espo\Core\FieldValidation\Validator\Failure; +use Espo\Entities\ScheduledJob; use Espo\ORM\Entity; -use Espo\Core\Exceptions\BadRequest; use Exception; -use stdClass; /** - * @extends Record + * @implements Validator */ -class ScheduledJob extends Record +class Valid implements Validator { - // @todo Move to field validator. - public function processValidation(Entity $entity, stdClass $data): void + public function validate(Entity $entity, string $field, Data $data): ?Failure { - parent::processValidation($entity, $data); - $scheduling = $entity->getScheduling(); - if (!$scheduling) { - throw new BadRequest(); + if ($scheduling === null) { + return null; } try { - $cronExpression = CronExpression::factory($scheduling); - - /** @phpstan-ignore-next-line*/ - $cronExpression->getNextRunDate()->format(DateTime::SYSTEM_DATE_TIME_FORMAT); + new CronExpression($scheduling); } catch (Exception) { - throw new BadRequest("Not valid scheduling expression."); + return Failure::create(); } + + return null; } } diff --git a/application/Espo/Resources/metadata/entityDefs/ScheduledJob.json b/application/Espo/Resources/metadata/entityDefs/ScheduledJob.json index 6eab5ffbfa..8c0da2168a 100644 --- a/application/Espo/Resources/metadata/entityDefs/ScheduledJob.json +++ b/application/Espo/Resources/metadata/entityDefs/ScheduledJob.json @@ -20,7 +20,10 @@ "required": true, "view": "views/scheduled-job/fields/scheduling", "tooltip": true, - "audited": true + "audited": true, + "validatorClassNameList": [ + "Espo\\Classes\\FieldValidators\\ScheduledJob\\Scheduling\\Valid" + ] }, "lastRun": { "type": "datetime",