Skip to content

Commit 626bdc0

Browse files
authored
feat: combine multipleOf errors into a single message (#150)
* combined multipleOf messages * all redefinement are resolved * added 3-value test case
1 parent 4b22392 commit 626bdc0

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/error-handlers/multipleOf.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const multipleOfErrorHandler = async (normalizedErrors, instance, localization)
1111
/** @type ErrorObject[] */
1212
const errors = [];
1313

14+
/** @type (number | null) */
15+
let combinedMultipleOf = null;
16+
/** @type string[] */
17+
const schemaLocations = [];
18+
1419
for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/multipleOf"]) {
1520
if (normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation]) {
1621
continue;
@@ -19,14 +24,42 @@ const multipleOfErrorHandler = async (normalizedErrors, instance, localization)
1924
const keyword = await getSchema(schemaLocation);
2025
const multipleOf = /** @type number */ (Schema.value(keyword));
2126

27+
combinedMultipleOf = combinedMultipleOf === null ? multipleOf : lcm(combinedMultipleOf, multipleOf);
28+
schemaLocations.push(schemaLocation);
29+
}
30+
31+
if (combinedMultipleOf !== null) {
2232
errors.push({
23-
message: localization.getMultipleOfErrorMessage(multipleOf),
33+
message: localization.getMultipleOfErrorMessage(combinedMultipleOf),
2434
instanceLocation: Instance.uri(instance),
25-
schemaLocations: [schemaLocation]
35+
schemaLocations
2636
});
2737
}
2838

2939
return errors;
3040
};
3141

42+
/**
43+
* @param {number} a
44+
* @param {number} b
45+
* @returns {number}
46+
*/
47+
const gcd = (a, b) => {
48+
while (b !== 0) {
49+
const temp = b;
50+
b = a % b;
51+
a = temp;
52+
}
53+
return Math.abs(a);
54+
};
55+
56+
/**
57+
* @param {number} a
58+
* @param {number} b
59+
* @returns {number}
60+
*/
61+
const lcm = (a, b) => {
62+
return Math.abs(a * b) / gcd(a, b);
63+
};
64+
3265
export default multipleOfErrorHandler;

src/test-suite/tests/multipleOf.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
},
2626
"instance": 6,
2727
"errors": []
28+
},
29+
{
30+
"description": "multiple multipleOf constraints (LCM of 4, 6, and 8)",
31+
"schema": {
32+
"allOf": [
33+
{ "multipleOf": 4 },
34+
{ "multipleOf": 6 },
35+
{ "multipleOf": 8 }
36+
]
37+
},
38+
"instance": 10,
39+
"errors": [
40+
{
41+
"messageId": "multipleOf-message",
42+
"messageParams": { "multipleOf": "24" },
43+
"instanceLocation": "#",
44+
"schemaLocations": ["#/allOf/0/multipleOf", "#/allOf/1/multipleOf", "#/allOf/2/multipleOf"]
45+
}
46+
]
2847
}
2948
]
3049
}

0 commit comments

Comments
 (0)