![JAR search and dependency download from the Maven repository](/logo.png)
com.cronutils.validation.CronValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cron-utils Show documentation
Show all versions of cron-utils Show documentation
A Java library to parse, migrate and validate crons as well as describe them in human readable
language
package com.cronutils.validation;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CronValidator implements ConstraintValidator {
private CronType type;
@Override
public void initialize(Cron constraintAnnotation) {
this.type = constraintAnnotation.type();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(type);
CronParser cronParser = new CronParser(cronDefinition);
try {
cronParser.parse(value).validate();
return true;
} catch (IllegalArgumentException e) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(e.getMessage()).addConstraintViolation();
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy