Skip to content

Commit

Permalink
Merge pull request #373 from NielsDoucet/add-jsr303-validator
Browse files Browse the repository at this point in the history
Add optional jsr 303 validator.
  • Loading branch information
jmrozanec authored Apr 7, 2019
2 parents 61714f6 + 573fd08 commit 2645f6e
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>

<!--test dependencies-->
<dependency>
Expand Down Expand Up @@ -108,6 +114,18 @@
<version>2.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/cronutils/validation/Cron.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.cronutils.validation;

import com.cronutils.model.CronType;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = CronValidator.class)
@Inherited
@Documented
public @interface Cron {

String message() default "UNUSED";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

CronType type();

}
37 changes: 37 additions & 0 deletions src/main/java/com/cronutils/validation/CronValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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<Cron, String> {

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;
}
}
}
70 changes: 70 additions & 0 deletions src/test/java/com/cronutils/validation/CronValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.cronutils.validation;

import com.cronutils.model.CronType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import java.util.Set;

import static org.junit.Assert.*;

@RunWith(Parameterized.class)
public class CronValidatorTest {

private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

private final String expression;
private final boolean valid;

public CronValidatorTest(String expression, boolean valid) {
this.expression = expression;
this.valid = valid;
}

@Parameterized.Parameters(name = "{0} ")
public static Object[] expressions() {
return new Object[][]{
{"0 0 * * * *", true},
{"*/10 * * * * *", true},
{"0 0 8-10 * * *", true},
{"0 0 6,19 * * *", true},
{"0 0/30 8-10 * * *", true},
{"0 0 9-17 * * MON-FRI", true},
{"0 0 0 25 12 ?", true},
{"0 0 0 L 12 ?", false},
{"1,2, * * * * *", false},
{"1- * * * * *", false}
};
}

@Test
public void validateExamples() {
TestPojo testPojo = new TestPojo(expression);
Set<ConstraintViolation<TestPojo>> violations = validator.validate(testPojo);

if (valid) {
assertTrue(violations.isEmpty());
} else {
assertFalse(violations.isEmpty());
}
}

public static class TestPojo {
@Cron(type = CronType.SPRING)
private final String cron;

public TestPojo(String cron) {
this.cron = cron;
}

public String getCron() {
return cron;
}

}
}

0 comments on commit 2645f6e

Please sign in to comment.