I had to create my own validator in my work, and since it is a new framework, I had a lot of trouble as a beginner of && Java who has no materials at all.
As a result, I made a note of what I expected.
The current task requires such an implementation, which is common for input validation.
Elementary? I think that Micronaut can be realized by Pattern (regexp =" pattern ", message =" message ")
, but in the current project, it is processed by a handler for each annotation, and this implementation was inconvenient.
** Added on 2020/03/04 It is necessary to add @Singleton
to the Validator class, so I added it. ** **
Bean Validation to check if any of the following is satisfied for the EmployeeNumber field
--Value is NULL --Shape of UXXXX, NXXX, KXXXX
package com.myproject.annotation;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.validation.validator.constraints.ConstraintValidator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Singleton;
import javax.validation.Constraint;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.regex.Pattern;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({FIELD})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {EmployeeNumberValidator.class})
public @interface EmployeeNumber {
String message() default "Employee number is the first letter of the company name+Please enter in 4-digit half-width numbers.";
}
@Singleton
class EmployeeNumberValidator
implements ConstraintValidator<EmployeeNumber, String> {
private static final Pattern EmployeeNumberPattern = Pattern.compile("^[UNK]\\d{4}+$");
@Override
public boolean isValid(@Nullable String value, @Nonnull AnnotationValue<EmployeeNumber> annotationMetadata, @Nonnull io.micronaut.validation.validator.constraints.ConstraintValidatorContext context) {
if (value == null) {
return true;
}
return EmployeeNumberPattern.matcher(value).matches();
}
}
This time, the validator and annotation are put together, but if you want to cut out to another file, set the validator to public
and ʻimport` on the annotation side.
The annotations attached to the annotation definition interface itself are not specific to Micronaut and will appear immediately after searching, so I will not explain them (I do not understand well).
package com.myproject.request;
import com.myproject.annotation.EmployeeNumber;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.micronaut.core.annotation.Introspected;
import lombok.Data;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
@Introspected
@Data
@JsonFormat
public class EmployeeSearchRequest {
@NotNull
private String officeCode;
private String employeeId;
@EmployeeNumber
private String employeeName;
}
Initially, when I implemented it by referring to the article that implements javax.validation.ConstraintValidator
, the phenomenon that it was not picked up by the handler of ConstraintViolationException
occurred even though it was annotated.
** ʻO.micronaut.validation.validator.constraints.ConstraintValidator` is implemented ** It seems that it was necessary to create a validator.
This was resolved by specifying it in the annotation @Constraint
.
Recommended Posts