Search code examples
spring-bootspring-validator

How can I set up a Spring Boot Validator that can use 2x items passed but the requirement is one of either value?


Our restful service is setup to accept "vendorOrderReference":"Create Order Postman" but business has now decided they want to change the word vendor to client (facepalm) so it would now be "clientOrderReference":"Create Order Postman". The problem is the Spring Validator is set up with

@NotBlank(message = "{createOrderRequest.vendorOrderReference.required}")
private String vendorOrderReference;

I've been trying to figure out how can I check if the endpoint gets the property vendorOrderReference or clientOrderReference and just use the one passed for private String vendorOrderReference;

Basically abstract the property label so I don't have the nightmare cascade in the code base business's "simple request" is causing.

I looked into existing annotations and tried to figure out how to create custom ones but I've had no success.


Solution

  • Could you use @JsonAlias to ensure it's mapped using either or property value vendorOrderReference or createOrderReference coupled with the @NotBlank message?

    I think this might meet your requirements from the sound of the above?