Skip to content

Commit 3f419f4

Browse files
authored
Fix multipleOf error message for fractional digits greater than 3 (#1210)
1 parent 0dde00c commit 3f419f4

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/main/java/com/networknt/schema/keyword/MultipleOfValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
4747
if (dividend.divideAndRemainder(this.divisor)[1].abs().compareTo(BigDecimal.ZERO) > 0) {
4848
executionContext.addError(error().instanceNode(node).instanceLocation(instanceLocation)
4949
.evaluationPath(executionContext.getEvaluationPath()).locale(executionContext.getExecutionConfig().getLocale())
50-
.arguments(this.divisor)
50+
.arguments(this.divisor.toString()) // String is used as the MessageFormat NumberFormat considers 3 fractional digits by default
5151
.build());
5252
}
5353
}
@@ -66,7 +66,7 @@ protected BigDecimal getDivisor(JsonNode schemaNode) {
6666
if (divisor != 0) {
6767
// convert to BigDecimal since double type is not accurate enough to do the
6868
// division and multiple
69-
return schemaNode.isBigDecimal() ? schemaNode.decimalValue() : BigDecimal.valueOf(divisor);
69+
return schemaNode.isBigDecimal() ? schemaNode.decimalValue().stripTrailingZeros() : BigDecimal.valueOf(divisor).stripTrailingZeros();
7070
}
7171
}
7272
return null;

src/test/java/com/networknt/schema/MultipleOfValidatorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,15 @@ void testTypeLoose() {
8989
messages = typeLoose.validate(validTypeLooseInputData, InputFormat.JSON);
9090
assertEquals(0, messages.size());
9191
}
92+
93+
@Test
94+
void messageFormatPrecision() {
95+
String schemaData = "{ \"type\": \"object\", \"properties\": { \"value1\": { \"type\": \"number\", \"multipleOf\": 0.00001 } } }";
96+
SchemaRegistry factory = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
97+
Schema schema = factory.getSchema(schemaData);
98+
String inputData = "{\"value1\":123.000001}";
99+
100+
List<Error> messages = schema.validate(inputData, InputFormat.JSON);
101+
assertEquals("must be multiple of 0.00001", messages.get(0).getMessage());
102+
}
92103
}

0 commit comments

Comments
 (0)