Created
February 16, 2021 12:42
-
-
Save wpik/2e23707e90e71861eaa6924e81111c0a to your computer and use it in GitHub Desktop.
Spring and validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.validation.Constraint; | |
import javax.validation.Payload; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.ElementType.FIELD; | |
import static java.lang.annotation.ElementType.METHOD; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
@Documented | |
@Constraint(validatedBy = IsoDateStringValidator.class) | |
@Target({METHOD, FIELD}) | |
@Retention(RUNTIME) | |
public @interface IsoDateString { | |
String message() default "date must be in ISO-8601 format YYYY-MM-DD"; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.extern.slf4j.Slf4j; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
import java.time.LocalDate; | |
import java.time.format.DateTimeParseException; | |
@Slf4j | |
public class IsoDateStringValidator implements ConstraintValidator<IsoDateString, String> { | |
public boolean isValid(String date, ConstraintValidatorContext context) { | |
try { | |
LocalDate.parse(date); | |
return true; | |
} catch (DateTimeParseException e) { | |
log.error("Could not parse date {}, {}", date, e.getMessage()); | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.validation.Constraint; | |
import javax.validation.Payload; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.ElementType.FIELD; | |
import static java.lang.annotation.ElementType.METHOD; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
@Documented | |
@Constraint(validatedBy = NonNegativeIntegerStringValidator.class) | |
@Target({METHOD, FIELD}) | |
@Retention(RUNTIME) | |
public @interface NonNegativeIntegerString { | |
String message() default "must be a non negative number"; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
import java.util.regex.Pattern; | |
public class NonNegativeIntegerStringValidator implements ConstraintValidator<NonNegativeIntegerString, String> { | |
private static Pattern pattern = Pattern.compile("^[\\d]*$"); | |
public boolean isValid(String number, ConstraintValidatorContext context) { | |
return number != null && pattern.matcher(number).matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment