Skip to content

shihyuho/jsr303-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSR303 Validators

A collection of jsr-303 validators I used

Build Status

<dependency>
  <groupId>com.github.shihyuho</groupId>
  <artifactId>jsr303-validator</artifactId>
  <version>1.0</version>
</dependency>

AssertThat

Integration with Apache commons JEXL

@AssertThat(
	value = "this.age >= 18 || (this.age < 18  && not empty(this.parent))",
    propertyNode = "age")
@Data
public class MyBean {
	private int age;
	private String parent;
	@AssertThat("this != null && this.isAfter(forName('java.time.LocalDate').now().plusDays(7))")
	private LocalDate expire;
	// ...
}
  • this - Represent the Object where @AssertThat located.
@AssertThat("this != null") // 'this' referring to expire field
private LocalDate expire;
@AssertThat("this != null") // 'this' referring to SomeObject
public class SomeObject {
}
  • namespace - Indicate the namespace referring to a class.
@AssertThat(
	value = "math:abs(this) > 10",
	namespaces = {
		@Namespace(prefix = "math", clazz = Math.class)
	}
)
private int index;
  • propertyNode - Indecate a property node the ConstraintViolation will be associated to.
@AssertThat(
	value = "this.age >= 18 || (this.age < 18  && not empty(this.parent))",
	propertyNode = "age")
public class MyBean {
	private int age;
	private String parent;
}
ConstraintViolationImpl {
	interpolatedMessage="com.github.shihyuho.jsr303.constraints.AssertThat.message", 
	propertyPath="age",
	rootBeanClass=...,
	messageTemplate='com.github.shihyuho.jsr303.constraints.AssertThat.message'
}

Supports Spring Expression Language as well

@AssertThat(
	engine = AssertThat.Engine.SpEL, // JEXL is the defualt EL engine
	value =
	    "#{this.age >= 18 || (this.age < 18  && !T(org.springframework.util.StringUtils).isEmpty(this.parent))}",
	propertyNode = "age"
)
public class MyBean {
    private int age;
    private String parent;
}

Custom Annotation

public enum Grade {
  A, B, C, D, E, F, ;
}
@Documented
@Constraint(validatedBy = {})
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@AssertThat(
  "this != null && " +
  "forName('java.util.Arrays').asList(" +
    "forName('com.github.shihyuho.jsr303.constraints.Grade').valueOf('A'), " +
    "forName('com.github.shihyuho.jsr303.constraints.Grade').valueOf('B')" +
  ").contains(this)")
public @interface GradeAboveB {
	String message() default "{com.github.shihyuho.jsr303.constraints.GradeAboveB.message}";
	Class<?>[] groups() default {};
	Class<? extends Payload>[] payload() default {};
}
@Data
public class PassExams {
	private Student student;
	private Exam exam;
	@GradeAboveB private Grade grade;
}