Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project does not adhere to [Semantic Versioning](https://semver.org/) and m
- Opt-in `ExecutionConfig.maxEvaluationSteps` and `maxEvaluationDepth` limits for validation and schema walking, including walking without assertions. Exhaustion throws `ValidationLimitExceededException` instead of returning a partial validity result. Both limits default to unlimited; they bound admitted evaluation steps and schema frames, not work inside a keyword. (#1276)

### Changed
- Ignore zero `multipleOf` divisors returned by custom validators, regardless of scale, matching the default conversion. (#1328)
- Display `multipleOf` divisors in error messages using plain notation when it fits within 32 characters or is no longer than `BigDecimal.toString()`, including values returned by custom validators. Check the length before expanding to plain notation to avoid excessive padding for extreme scales. (#1328)
- `MultipleOfValidator.getDivisor` and `getDividend` now preserve exact integral and BigDecimal values. Custom validators, including overrides calling `super`, receive these exact values; validation uses hook results without further rounding. Integral dividend scales can change (for example, `2.0` becomes `2`), which affects scale-sensitive comparisons in overrides. Mixed integral/Double or Float inputs above 2^53 can produce different results. Precision already lost when parsing floating-point values is tracked separately in #1293. (#1286)
- Extend document-root unknown-keyword suppression to external `$ref` JSON Pointer targets. Keep container and direct-schema caches separate so direct registry loads retain normal diagnostics regardless of registry load order, including rejection by `DisallowUnknownKeywordFactory`. Within one schema, a whole-document reference following a JSON Pointer reference can still reuse the container and suppress root diagnostics, including strict-factory rejection. Apply the container annotation fallback after nullable/discriminator special handling and make the container marker volatile. (#1325)
- Treat unknown document-root fields as annotations when loading a schema by JSON Pointer fragment, while preserving known keyword processing and diagnostics in subschemas. With `DisallowUnknownKeywordFactory`, such fragment loads now succeed instead of throwing on unknown root fields; direct document loads still throw. If the selected schema references the root with `$ref: "#"`, unknown root fields also remain annotations and do not reach the unknown-keyword factory. (#1205)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* {@link KeywordValidator} for multipleOf.
*/
public class MultipleOfValidator extends BaseKeywordValidator implements KeywordValidator {
private static final int MAX_PLAIN_DIVISOR_LENGTH = 32;

private final BigDecimal divisor;
private final BigInteger denominator;
private final long longDivisor;
Expand All @@ -39,10 +41,11 @@ public class MultipleOfValidator extends BaseKeywordValidator implements Keyword
public MultipleOfValidator(SchemaLocation schemaLocation, JsonNode schemaNode,
Schema parentSchema, SchemaContext schemaContext) {
super(KeywordType.MULTIPLE_OF, schemaNode, schemaLocation, parentSchema, schemaContext);
this.divisor = getDivisor(schemaNode);
BigDecimal divisor = getDivisor(schemaNode);
this.divisor = divisor == null || divisor.signum() == 0 ? null : divisor;
this.denominator = this.divisor == null ? null : this.divisor.unscaledValue().abs();
this.longDivisor = integralDivisor(this.divisor);
this.divisorMessage = this.divisor == null ? null : this.divisor.toString();
this.divisorMessage = formatDivisorMessage(this.divisor);
}

public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
Expand Down Expand Up @@ -72,6 +75,26 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
}
}

private static String formatDivisorMessage(BigDecimal value) {
if (value == null) {
return null;
}
long precision = value.precision();
long scale = value.scale();
long length = scale <= 0 ? precision - scale : Math.max(precision + 1, scale + 2);
if (value.signum() < 0) {
length++;
}
if (length <= MAX_PLAIN_DIVISOR_LENGTH) {
return value.toPlainString();
}
String text = value.toString();
if (length > text.length()) {
return text;
}
return text.indexOf('E') < 0 ? text : value.toPlainString();
}

private static long integralDivisor(BigDecimal value) {
if (value == null) {
return 0;
Expand Down Expand Up @@ -132,8 +155,13 @@ private static boolean isMultipleOf(BigDecimal dividend, BigDecimal divisor, Big
* {@link BigDecimal#valueOf(double)} using their {@code doubleValue()}.
* Validation uses the returned value without further rounding, including when
* this method is overridden.
* A null or zero return value causes this constraint to be ignored, including
* when this method is overridden.
* The default conversion removes trailing zeros unless doing so would overflow the scale.
* Error messages preserve the string representation of the returned value.
* Error messages use the returned value's plain notation when it fits within
* 32 characters or is no longer than its {@link BigDecimal#toString()}
* representation. Otherwise, that representation is used.
* This also applies to values returned by overrides.
*
* @param schemaNode the schema node
* @return the divisor or null if the input is not correct
Expand Down
98 changes: 90 additions & 8 deletions src/test/java/com/networknt/schema/MultipleOfValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ void exactDecimalsFromReader(String divisor, String dividend, int expectedErrors

@ParameterizedTest
@CsvSource({
"100, 3, must be multiple of 1E+2",
"100.0, 3, must be multiple of 1E+2",
"100.00, 3, must be multiple of 1E+2",
"100, 3, must be multiple of 100",
"100.0, 3, must be multiple of 100",
"100.00, 3, must be multiple of 100",
"1000, 3, must be multiple of 1000",
"2.50E+3, 3, must be multiple of 2500",
"2.0, 3, must be multiple of 2",
"9007199254740993, 2, must be multiple of 9007199254740993"
})
Expand All @@ -315,7 +317,7 @@ void exactDivisorMessage(String divisor, String dividend, String expectedMessage
}

@ParameterizedTest
@CsvSource({"2.0, 2", "100.00, 1E+2", "0.0000100, 0.00001",
@CsvSource({"2.0, 2", "100.00, 100", "0.0000100, 0.00001",
"9007199254740993.0, 9007199254740993", "1e400, 1E+400"})
void exactDecimalDivisorMessage(String divisor, String expectedDivisor) {
ObjectNode schemaNode = JsonNodeFactory.instance.objectNode();
Expand Down Expand Up @@ -344,8 +346,13 @@ protected BigDecimal getDivisor(JsonNode node) {
}

@ParameterizedTest
@ValueSource(strings = {"3.0", "100.00"})
void overriddenDivisorRetainsMessageRepresentation(String value) {
@CsvSource({"3.0, 3.0", "100.00, 100.00", "1E+2, 100", "1E-7, 0.0000001", "1E+400, 1E+400",
"123456789012345678901234567890000, 123456789012345678901234567890000",
"1.2345678901234567890123456789E+32, 123456789012345678901234567890000",
"1.2345678901234567890123456789E+33, 1234567890123456789012345678900000",
"1.2345678901234567890123456789E+34, 1.2345678901234567890123456789E+34",
"1234567890123456789012345678901234, 1234567890123456789012345678901234"})
void overriddenDivisorMessage(String value, String expectedDivisor) {
Schema parent = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12)
.getSchema("{\"multipleOf\":2}");
MultipleOfValidator validator = new MultipleOfValidator(SchemaLocation.of("#/multipleOf"),
Expand All @@ -356,11 +363,86 @@ protected BigDecimal getDivisor(JsonNode node) {
}
};
ExecutionContext context = parent.createExecutionContext();
JsonNode node = DecimalNode.valueOf(new BigDecimal("0.00000015"));
validator.validate(context, node, node, new NodePath(PathType.JSON_POINTER));
assertEquals(1, context.getErrors().size());
assertEquals(expectedDivisor, context.getErrors().get(0).getArguments()[0]);
assertEquals("must be multiple of " + expectedDivisor, context.getErrors().get(0).getMessage());
}

@ParameterizedTest
@ValueSource(ints = {0, 2, -100, 100, 30, Integer.MIN_VALUE, Integer.MAX_VALUE})
void ignoresZeroDivisorFromOverride(int scale) {
Schema parent = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12)
.getSchema("{\"multipleOf\":2}");
MultipleOfValidator validator = new MultipleOfValidator(SchemaLocation.of("#/multipleOf"),
IntNode.valueOf(2), parent, parent.getSchemaContext()) {
@Override
protected BigDecimal getDivisor(JsonNode node) {
return new BigDecimal(BigInteger.ZERO, scale);
}
};
for (String value : new String[] {"1", "1.5", "0.00000015", "0"}) {
ExecutionContext context = parent.createExecutionContext();
JsonNode node = DecimalNode.valueOf(new BigDecimal(value));
validator.validate(context, node, node, new NodePath(PathType.JSON_POINTER));
assertTrue(context.getErrors().isEmpty(), "scale=" + scale + ", dividend=" + value);
}
}

@ParameterizedTest
@CsvSource({
"1e31, 10000000000000000000000000000000",
"1e32, 1E+32",
"1e-30, 0.000000000000000000000000000001",
"1e-31, 1E-31",
"-1e30, -1000000000000000000000000000000",
"-1e31, -1E+31",
"-1e-29, -0.00000000000000000000000000001",
"-1e-30, -1E-30",
"1e400, 1E+400",
"1.5e31, 15000000000000000000000000000000",
"1.5e32, 1.5E+32",
"1.5e-29, 0.000000000000000000000000000015",
"1.5e-30, 1.5E-30",
"-1.5e30, -1500000000000000000000000000000",
"-1.5e31, -1.5E+31",
"-1.5e-28, -0.00000000000000000000000000015",
"-1.5e-29, -1.5E-29",
"123456789012345678901234567890000, 123456789012345678901234567890000",
"1.2345678901234567890123456789E+33, 1234567890123456789012345678900000",
"1.2345678901234567890123456789E+34, 1.2345678901234567890123456789E+34",
"-123456789012345678901234567890000, -123456789012345678901234567890000",
"-1.2345678901234567890123456789E+33, -1234567890123456789012345678900000",
"-1.2345678901234567890123456789E+34, -1.2345678901234567890123456789E+34"})
void divisorMessageBoundsPlainNotation(String text, String expectedDivisor) {
BigDecimal divisor = new BigDecimal(text);
ObjectNode schemaNode = JsonNodeFactory.instance.objectNode();
schemaNode.set("multipleOf", DecimalNode.valueOf(divisor));
Schema schema = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12)
.getSchema(schemaNode);
BigDecimal dividend = divisor.multiply(new BigDecimal("1.5"));
List<Error> errors = schema.validate(DecimalNode.valueOf(dividend));
assertEquals(1, errors.size());
assertEquals("must be multiple of " + expectedDivisor, errors.get(0).getMessage());
}

@Test
void inheritedDivisorUsesPlainNotation() {
Schema parent = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12)
.getSchema("{\"multipleOf\":100}");
MultipleOfValidator validator = new MultipleOfValidator(SchemaLocation.of("#/multipleOf"),
IntNode.valueOf(100), parent, parent.getSchemaContext()) {
@Override
protected BigDecimal getDivisor(JsonNode node) {
return super.getDivisor(node);
}
};
ExecutionContext context = parent.createExecutionContext();
JsonNode node = IntNode.valueOf(1);
validator.validate(context, node, node, new NodePath(PathType.JSON_POINTER));
assertEquals(1, context.getErrors().size());
assertEquals(value, context.getErrors().get(0).getArguments()[0]);
assertEquals("must be multiple of " + value, context.getErrors().get(0).getMessage());
assertEquals("must be multiple of 100", context.getErrors().get(0).getMessage());
}

@Test
Expand Down
Loading