0

I have a POJO as follows:

public class ClosureCodeReasonRequest {
    @NotNull(message = MessageConstants.CLOSURE_CODE_BLANK_ERROR)
    @NotBlank(message = MessageConstants.CLOSURE_CODE_BLANK_ERROR)
    private String closureCode;

    @NotNull(message = MessageConstants.REASON_TITLE_BLANK_ERROR)
    @NotBlank(message = MessageConstants.REASON_TITLE_BLANK_ERROR)
    @Size(max = 50, message = MessageConstants.REASON_TITLE_TOO_LONG)
    private String reasonTitle;

    @NotEmpty
    private List<String> programList;

    @NotNull
    @NotBlank
    private String isActive;

    @NotNull
    @NotBlank
    private Long version;
}

In the above POJO, value of isActive can be either "true" or "false" and length of programList can be either 1 or 2 and contents will be among "Test1" and "Test2".

Is there any built in annotation that can be used for these requirements or do I have to create a new one?

manish
  • 19,695
  • 5
  • 67
  • 91
Joy
  • 4,197
  • 14
  • 61
  • 131
  • Any reason why the code is not `private boolean active` instead of `private String isActive`? `@Size(min = 1, max = 2) private List programList` will work for the second requirement. JSR-303 does not have any mechanism for validating the content of a `Collection`. You will have to write a custom validator for validating the collection content. – manish Mar 15 '18 at 11:33

1 Answers1

0

What about using @Pattern?

for isActive you could use @Pattern(regexp="(true|false)") for programList you could use @Size(2) with @Pattern(regexp="(Test1|Test2)?

I didn't tested that, but you can do it on your own.

bilak
  • 4,526
  • 3
  • 35
  • 75