I have a POJO with some fields annotated with @NotEmpty:

public class SampleFormInputDTO {

  @NotEmpty
  private String textarea;

  private int myInt = 0;

  @NotEmpty
  private String myText = "somevalue";

  public String getTextarea() {
    return textarea;
  }

  public void setTextarea(String textarea) {
    this.textarea = textarea;
  }
}

The intention is that the fields will be checked to ensure they contain a value i.e not null and not empty.

If I create an instance of SampleFormInputDTO using no-args constructor, the field textarea will be null initially so should and does fail validation as expected.

SampleFormInputDTO sampleFormInputDTO = new SampleFormInputDTO();

ValidatorFactory validatorFactory =
    Validation.byDefaultProvider()
        .configure()
        .messageInterpolator(new ParameterMessageInterpolator())
        .buildValidatorFactory();

Validator validator = validatorFactory.getValidator(sampleFormInputDTO);

Set<ConstraintViolation<SampleFormInputDTO>> violationSet = validator.validate();

I'm wondering if it is possible to dynamically / programmatically indicate to the validator instance to not validate a specific constraint annotation for a specific field?

Suppose I've determined, as part of handling a REST API invocation that I want the field textarea of type SampleFormInputDTO to allow empty strings dynamically, but only for that specific field. Not affecting any constraint annotations that may be present on other fields in the same POJO.

Is this possible?

2

There are 2 best solutions below

4
mark_o On

You might want to take a look at the validation groups.

public class SampleFormInputDTO {

  @NotEmpty(groups = Group1.class)
  private String textarea;

  private int myInt = 0;

  @NotEmpty
  private String myText = "somevalue";

  public String getTextarea() {
    return textarea;
  }

  public void setTextarea(String textarea) {
    this.textarea = textarea;
  }
}

Then you can control which constraints are included in validation and which are not, for example:

validator.validate(sampleFormInputDTO);

will only check myText property, but then something like:

validator.validate(sampleFormInputDTO, Group1.class, Default.class);

will validate both.

2
kikicoder On

So if I understood correctly, your problem is that you want to validate the fields but in case an empty object has been submitted you do not want to through an exception, you can try two scenarios, first one is to put the validation inside the args constructor, therefor when using the no args constructore no validation well happen, check this ex:

public class Employee {
...
  public Employee (@NotNull String name) { ... }

  public void setSalary(
      @NotNull
      @Digits(integer=6, fraction=2) BigDecimal salary,
      @NotNull
      @ValidCurrency
      String currencyType) {
    ...
  }
...
}

link is here

or you can build your own custom validation: check here

also I believe you can use the regex and provide a pattern that suits your case check this