add custom required one field validator
This commit is contained in:
parent
0fd552f29c
commit
6a6840fc79
|
@ -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>
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -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}")
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue