0

I am developing a Spring REST application.

I have a DTO

private String name;
@
private String nationality;
private String matchType;
private List<NC_Field> ncFields = new ArrayList();
// Getters and Setters

I have 3 tables

  1. Field Table
  2. Name Clearance Table
  3. NC_Fields Table

enter image description here

user3343543
  • 143
  • 2
  • 14

1 Answers1

0

You can have a custom validator defined with whatever logic you want. Then you can create a custom annotation for the validator and use it in your DTO just like @NotNull

public class CustomValidator implements ConstraintValidator<MyObject, String> {
  .
  .
  .
}

--

@Constraint(validatedBy = { CustomValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface ContactInfo {
    String message() default "Invalid value";

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

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

refer to this link for example: https://www.baeldung.com/spring-dynamic-dto-validation

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
  • How to check if nationality is INDIAN, then PAN is mandatory. Else it is not. Likewise there are some more conditions. – user3343543 Oct 05 '18 at 10:04
  • when you implement `ConstraintValidator` you need to override the validate method. This is the place where you can pretty much write entire logic – Amit Phaltankar Oct 07 '18 at 22:18