Browse Source

Implement CurrentYear validator

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
ef0ae6b61d
2 changed files with 49 additions and 0 deletions
  1. +24
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/validation/CurrentYear.java
  2. +25
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/validation/CurrentYearValidator.java

+ 24
- 0
bookstore/src/main/java/com/fjordtek/bookstore/validation/CurrentYear.java View File

@ -0,0 +1,24 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.validation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import javax.validation.Constraint;
import javax.validation.Payload;
@Documented
@Constraint(validatedBy = CurrentYearValidator.class)
@Target(value={ElementType.TYPE_USE, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CurrentYear {
String message() default "Year must not be in the future";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}

+ 25
- 0
bookstore/src/main/java/com/fjordtek/bookstore/validation/CurrentYearValidator.java View File

@ -0,0 +1,25 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.validation;
import java.time.Year;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.stereotype.Component;
@Component
public class CurrentYearValidator implements ConstraintValidator<CurrentYear, Integer> {
private static final int yearNow = Year.now().getValue();
@Override
public void initialize(CurrentYear year) {
}
@Override
public boolean isValid(Integer year, ConstraintValidatorContext constraintValidatorContext) {
return year <= yearNow;
}
}

Loading…
Cancel
Save