14

I'm trying to validate if one of two fields are not null in Spring Boot?

I have set that in the method class for the main object:

@NotNull(message = "Username field is required")
private String username;

@NotNull(message = "Email field is required")
private String email;

but that will require to have both fields not null. Then I went with custom validation described here https://lmonkiewicz.com/programming/get-noticed-2017/spring-boot-rest-request-validation/ but I wasn't able to get that example to work. I have to stuck on

User class declaration:

@CombinedNotNull(fields = {"username","email"})
public class User implements {

    private long id = 0L;
    @NotNull(message = "First name field is required")
    private String firstName;

    @NotNull(message = "Last name field is required")
    private String lastName;

    private String username;
    private String email;

    @NotNull(message = "Status field is required")
    private String status;

    ...all methods here...
    ...setters and getters...

}

CombibnedNotNull class:

@Documented
@Retention(RUNTIME)
@Target({ TYPE, ANNOTATION_TYPE })
@Constraint(validatedBy = userValidator.class)
public @interface CombinedNotNull {
        String message() default "username or email is required";
        Class<?>[] groups() default { };
        Class<? extends Payload>[] payload() default { };
}

userValidator class:

@Component
public class userValidator implements ConstraintValidator<CombinedNotNull, User> {

    @Override
    public void initialize(final CombinedNotNull combinedNotNull) {
        fields = combinedNotNull.fields();
    }

    @Override
    public boolean isValid(final User user, final ConstraintValidatorContext context) {
        final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(user);

        for (final String f : fields) {
            final Object fieldValue = beanWrapper.getPropertyValue(f);

            if (fieldValue == null) {
                return false;
            }
        }

        return true;
    }
}

Is there any other way to get this done or should I go with the "complex" example from that page?

11
  • Anyway take a look at my question&answer here stackoverflow.com/questions/46957854/…
    – LppEdd
    Feb 11, 2019 at 20:05
  • Class validation is the optimal way to go. Which errors are you getting?
    – LppEdd
    Feb 11, 2019 at 20:10
  • @LppEdd I have updated OP with the errors. It throws that RUNTIME, FIELD, ANNOTATION_TYPE, PARAMETER cannot find symbols for them. Feb 11, 2019 at 20:43
  • They're static imports. Just qualify them with ElementType or RetentionPolicy (e.g. ElementType.FIELD - RetentionPolicy.RUNTIME)
    – LppEdd
    Feb 11, 2019 at 20:45
  • @LppEdd OK, moved forward and it came up I'm not able to pass two fields into ConstraintValidatorContext Feb 11, 2019 at 21:12

3 Answers 3

10

I'm assuming username OR email must not be null. Not XOR.

Add this getter in User class:

@AssertTrue(message = "username or email is required")
private boolean isUsernameOrEmailExists() {
    return username != null || email != null;
}

In my experience, the method name must follow the getter name convention otherwise this won't work. For examples, getFoo or isBar.

This has a small problem: the field name from this validation error would be usernameOrEmailExists, only 1 error field. If that is not a concern, this might help.


But If you want to have username and email fields when errors occur, you can use this workaround:

public String getUsername() {
    return username;
}

public String getEmail() {
    return email;
}

@AssertTrue(message = "username or email is required")
private boolean isUsername() {
    return isUsernameOrEmailExists();
}

@AssertTrue(message = "username or email is required")
private boolean isEmail() {
    return isUsernameOrEmailExists();
}

private boolean isUsernameOrEmailExists() {
    return username != null || email != null;
}

get... methods are just simple getters for general use, and is... are for validation. This will emit 2 validation errors with username and email fields.

6

I'll try to implement it for you (even if I'm without an IDE).
Inside ConstraintValidator#initialize you can get a hold of the configured fields' names which cannot be null.

@Override
public void initialize(final CombinedNotNull combinedNotNull) {
    fields = combinedNotNull.fields();
}

Inside ConstraintValidator#isValid you can use those fields' names to check the Object fields.

@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(value);
    
    for (final String f : fields) {
       final Object fieldValue = beanWrapper.getPropertyValue(f);
       
       if (fieldValue == null) {
          return false;
       }
    }

    return true;
}

Annotation:

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = CombinedNotNullValidator.class)
public @interface CombinedNotNull {
    String message() default "username or email is required";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    /**
     * Fields to validate against null.
     */
    String[] fields() default {};
}

The annotation could be applied as

@CombinedNotNull(fields = {
      "fieldName1",
      "fieldName2"
})
public class MyClassToValidate { ... }

To learn how to create a Class-level constraint annotation, refer always to the official documentation. Docs

7
  • Somehow for @CombinedNotNull(fields = { "user","email"}) fields cannot be resolved Feb 11, 2019 at 21:41
  • @JackTheKnife well you need to adjust your annotation. Maybe post the actual code.
    – LppEdd
    Feb 11, 2019 at 21:43
  • @JackTheKnife see my updated answer. Look that I renamed the validator from "userValidator" to "CombinedNotNullValidator" and added a String array "fields"
    – LppEdd
    Feb 11, 2019 at 21:58
  • OK. I see what I have missed. Now I need to change little bit ConstraintValidator#isValid as I'm looking for username OR email is not null (one of them must be present) Feb 11, 2019 at 22:06
  • 1
    @JackTheKnife you can generalize this constraint by not hardcoding the fields name
    – LppEdd
    Feb 11, 2019 at 22:07
6

If you want to validate that exactly one is set and the others are null:

Annotation

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME)
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Constraint(validatedBy = OneNotNullValidator.class)
public @interface OneNotNull {
    String message() default "Exactly one of the fields must be set and the other must be null";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    /**
     * Fields to validate against null.
     */
    String[] fields() default {};
}

Validator

import java.util.Arrays;
import java.util.Objects;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.BeanWrapperImpl;
import org.springframework.stereotype.Component;

@Component
public class OneNotNullValidator implements ConstraintValidator<OneNotNull, Object> {
    private String[] fields;

    @Override
    public void initialize(final OneNotNull combinedNotNull) {
        fields = combinedNotNull.fields();
    }

    @Override
    public boolean isValid(final Object obj, final ConstraintValidatorContext context) {
        final BeanWrapperImpl beanWrapper = new BeanWrapperImpl(obj);

        return Arrays.stream(fields)
                .map(beanWrapper::getPropertyValue)
                .filter(Objects::isNull)
                .count()
                == 1;
    }
}

Usage

@OneNotNull(
    fields = {"username","email"},
    message="Either username or email must be set"
)
public class User {

    private String username;
    private String email;

   // ...
}
1
  • Hi! Why are you using @Component annotation?
    – Davide C
    Aug 21, 2022 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.