0

I just found that @Pattern might work (only) with CharSequences.

How can I validate a single char?

//@Pattern(regex = "[YN]") // @@?
private char someYn; // 'Y' or 'N'

Will it blend?

Using an AttributeConverter is not an option.

Oh, is a single char also a CharSequence?

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

1 Answers1

1

You can write your own ConstraintValidator:

https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html

Where you can provide annotation and validation type for which you can define your logic And then you can annotate your field with your custom annotation

Here is link with steps how to make it :

https://dzone.com/articles/create-your-own-constraint-with-bean-validation-20

As per javax validation documentation :

@Pattern(regex=, flag=) String.

Additionally supported by HV: any sub-type of CharSequence.

Checks if the annotated string matches the regular expression regex considering the given flag

So actually with Character using @Pattern you will get the error

Thanks

Mykhailo Moskura
  • 2,073
  • 1
  • 9
  • 20