I have a search criteria for a Project model. A Project can be searched using an id or project name.
@Data
@Builder
public class ProjectSearchCriteria {
@IsNumberValidatorConstraint(message = "invalid input for id")
private String id;
private String projectName;
}
I have also created a custom validator to check whether the id is a number (Number validation). This is also working perfectly.
But my question is that is there any possibility for me to tell spring; to perform Number validation only if id is not null?
e.g:
http://localhost:8081/api/projects?id=1 (id needs to be validated)
http://localhost:8081/api/projects?projectName=project1 (No need to validate the id)
Any ideas on how I can get this working?
Cheers