0

I have an endpoint in my spring-boot application which accepts from & to ids. Now in my domain, I have following code:

@Data
class UserAccount {

    @NotNull
    @NotEmpty
    private final String from;

    @NotNull
    @NotEmpty
    private final String to;

    @JsonCreator
    public UserAccount(@JsonProperty("from") String from,
                    @JsonProperty("to") String to,) {
        this.from = from;
        this.to = to;
    }
}

Is there a way to validate that from & to fields should not be same using annotations?

Minato Namikaze
  • 576
  • 1
  • 7
  • 22

1 Answers1

0

Lombok does not have this feature as of today. Lombok used to reduce boilerplate code. You asked for something that more like business logic and should be written out explicitly (from the Lombok point of view):

public UserAccount(@NonNull String from, @NonNull String to) {
    this.from = from;
    this.to = to;
    if (form.equals(to)) {
        throw new IllegalArgumentException("Invalid combination of from and to fields.");
    }
}

The onX experimental feature might be also interesting: https://projectlombok.org/features/experimental/onX Please note that @NonNull and @NotNull are not the same.

Istvan
  • 613
  • 5
  • 5