0

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
  • This regex will match "3A14159265359", since . 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. Commented Oct 22, 2015 at 10:47

1 Answer 1

1

@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.