I'm in need to apply a validation on a double value which needs to match with PI. I'm thinking to use a @Pattern(regex="3.14159265359"). Is it the best way I can apply such a constraint using Hibernate Validation Constraints? Thanks
1 Answer
@Pattern is only defined for string type (CharSequence really). If your data type is a double you cannot use it, unless you write a custom ConstraintValidator. You could use DecimalMin in combination with DecimalMax potentially allowing for some imprecision. Alternatively, you could write your own constraint @Pi which for example allows to specify a delta. @Pi is probably the best solution, provided you really need this validation.
.has special meaning (any character), so it's probably not the right choice. Why do you need to have this on your object/validate it anyway? PI is a constant, it can't be changed, so I don't really see a use case where having it as some mutable field which you're going to validate would be of any use.