How to check decimal places #454
-
I need to generate a json schema starting from a java class that have a property like this @XmlValue
@DecimalMin(value = "0.01", inclusive = true)
@Digits(integer = 18, fraction = 2)
protected BigDecimal value; the tool generates this "value" : {
"type" : "number",
"minimum" : 0.01
} Please who knows how can the fraction be checked? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @theseeker58, The fraction could be enforced via the configBuilder.forFields().withNumberMultipleOfResolver(field -> Optional
.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(Digits.class))
.map(annotation -> BigDecimal.ONE.divide(BigDecimal.TEN.pow(annotation.fraction()), annotation.fraction(), RoundingMode.FLOOR)).orElse(null)); The maximum number of digits before the decimal separator could similarly be enforced as an configBuilder.forFields().withNumberExclusiveMaximumResolver(field -> Optional
.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(Digits.class))
.map(annotation -> BigDecimal.TEN.pow(1 + annotation.integer())).orElse(null)); That should produce output like the following: "value" : {
"type" : "number",
"minimum" : 0.01,
"exclusiveMaximum": 1000000000000000000,
"multipleOf": 0.01
} Beyond these, I don't think the JSON Schema Specification has particular keywords for the allowed number of digits before and after the decimal separator. |
Beta Was this translation helpful? Give feedback.
-
Thank you @CarstenWickner |
Beta Was this translation helpful? Give feedback.
Hi @theseeker58,
The fraction could be enforced via the
multipleOf
keyword (if you don't mind possible trailing zeros), which you can set like this (refer to documentation):The maximum number of digits before the decimal separator could similarly be enforced as an
exclusiveMaximum
(refer to documentation) like this: