add custom required one field validator

This commit is contained in:
amentis 2023-12-18 10:30:37 +02:00
parent 0fd552f29c
commit 6a6840fc79
5 changed files with 73 additions and 1 deletions

View File

@ -48,7 +48,12 @@
<groupId>org.eclipse.angus</groupId> <groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId> <artifactId>jakarta.mail</artifactId>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -0,0 +1,21 @@
package eu.eudat.commons.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
@Constraint( validatedBy = { RequiredOneFieldValidator.class } )
@Documented
@Target( { ElementType.TYPE } )
@Retention( RetentionPolicy.RUNTIME )
public @interface RequiredOneField {
String message() default "One field is required";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String[] fieldNames();
}

View File

@ -0,0 +1,40 @@
package eu.eudat.commons.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.PropertyUtils;
public class RequiredOneFieldValidator implements ConstraintValidator<RequiredOneField, Object> {
private String[] fieldNames;
public void initialize(RequiredOneField constraintAnnotation) {
this.fieldNames = constraintAnnotation.fieldNames();
}
public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {
if (object == null) {
return true;
}
int requiredCount = 0;
try {
for (String fieldName:fieldNames){
Object property = PropertyUtils.getProperty(object, fieldName);
if (property != null) {
requiredCount = requiredCount + 1;
}
}
if (requiredCount == 1){
return true;
}
return false;
} catch (Exception e) {
return false;
}
}
}

View File

@ -3,12 +3,16 @@ package eu.eudat.model.persist;
import eu.eudat.commons.enums.DmpUserRole; import eu.eudat.commons.enums.DmpUserRole;
import eu.eudat.commons.validation.ValidEnum; import eu.eudat.commons.validation.ValidEnum;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List; import java.util.List;
public class DmpUserInvitePersist { public class DmpUserInvitePersist {
@Valid @Valid
@NotNull
@NotEmpty
private List<DmpUserInviteTypePersist> users; private List<DmpUserInviteTypePersist> users;
@ValidEnum(message = "{validation.empty}") @ValidEnum(message = "{validation.empty}")

View File

@ -1,8 +1,10 @@
package eu.eudat.model.persist; package eu.eudat.model.persist;
import eu.eudat.commons.validation.RequiredOneField;
import java.util.UUID; import java.util.UUID;
@RequiredOneField(fieldNames={"userId","email"})
public class DmpUserInviteTypePersist { public class DmpUserInviteTypePersist {
private UUID userId; private UUID userId;