Skip to content
Open
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
37 changes: 35 additions & 2 deletions src/error-handlers/multipleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const multipleOfErrorHandler = async (normalizedErrors, instance, localization)
/** @type ErrorObject[] */
const errors = [];

/** @type (number | null) */
let combinedMultipleOf = null;
/** @type string[] */
const schemaLocations = [];

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/multipleOf"]) {
if (normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation]) {
continue;
Expand All @@ -19,14 +24,42 @@ const multipleOfErrorHandler = async (normalizedErrors, instance, localization)
const keyword = await getSchema(schemaLocation);
const multipleOf = /** @type number */ (Schema.value(keyword));

combinedMultipleOf = combinedMultipleOf === null ? multipleOf : lcm(combinedMultipleOf, multipleOf);
schemaLocations.push(schemaLocation);
}

if (combinedMultipleOf !== null) {
errors.push({
message: localization.getMultipleOfErrorMessage(multipleOf),
message: localization.getMultipleOfErrorMessage(combinedMultipleOf),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
schemaLocations
});
}

return errors;
};

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
const gcd = (a, b) => {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return Math.abs(a);
};

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
const lcm = (a, b) => {
return Math.abs(a * b) / gcd(a, b);
};

export default multipleOfErrorHandler;
18 changes: 18 additions & 0 deletions src/test-suite/tests/multipleOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
},
"instance": 6,
"errors": []
},
{
"description": "multiple multipleOf constraints (LCM of 3 and 2)",
"schema": {
"allOf": [
{ "multipleOf": 3 },
{ "multipleOf": 2 }
]
},
"instance": 5,
"errors": [
{
"messageId": "multipleOf-message",
"messageParams": { "multipleOf": "6" },
"instanceLocation": "#",
"schemaLocations": ["#/allOf/0/multipleOf", "#/allOf/1/multipleOf"]
}
]
}
]
}