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
91 changes: 45 additions & 46 deletions src/error-handlers/multipleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,41 @@ const multipleOfErrorHandler = {
/** @type ErrorObject[] */
const errors = [];

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

for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/multipleOf"]) {
const multipleOf = /** @type number */ (normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation].value);
if (normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation].valid === false) {
hasError = true;
errors.push({
message: context.localization.getMultipleOfErrorMessage(multipleOf),
instanceLocation: Instance.uri(instance),
schemaLocations: [schemaLocation]
});
}

const multipleOf = /** @type number */ (normalizedErrors["https://json-schema.org/keyword/multipleOf"][schemaLocation].value);

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

if (combinedMultipleOf !== null && hasError) {
errors.push({
message: context.localization.getMultipleOfErrorMessage(combinedMultipleOf),
instanceLocation: Instance.uri(instance),
schemaLocations
});
if (errors.length === 0 || multiples.length === 1) {
return errors;
}

const combinedMultipleOf = lcm(multiples);
// If the LCM can't be computed exactly, describe the original failing
// constraints instead.
if (combinedMultipleOf === null) {
return errors;
}

return errors;
return [{
message: context.localization.getMultipleOfErrorMessage(combinedMultipleOf),
instanceLocation: Instance.uri(instance),
schemaLocations
}];
},

success: (normalizedOutput, instance, context) => {
Expand All @@ -46,42 +55,32 @@ const multipleOfErrorHandler = {
}
};

/**
* @param {number} value
* @returns {number}
*/
const countDecimals = (value) => value.toString().split(".")[1]?.length || 0;
/** @type (value: number) => number */
const countDecimals = (value) => {
const [mantissa, exponent = "0"] = value.toString().split("e");
return Math.max((mantissa.split(".")[1]?.length ?? 0) - Number(exponent), 0);
};

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
const gcd = (a, b) => {
const m = 10 ** Math.max(countDecimals(a), countDecimals(b));
let x = Math.round(a * m);
let y = Math.round(b * m);
/** @type (a: number, b: number) => number */
const gcd = (a, b) => b === 0 ? Math.abs(a) : gcd(b, a % b);

/** @type (multiples: number[]) => number | null */
const lcm = (multiples) => {
const m = 10 ** Math.max(...multiples.map(countDecimals));
let combined = 1;
for (const multiple of multiples) {
const integer = Math.round(multiple * m);
if (!Number.isSafeInteger(integer)) {
return null;
}

while (y !== 0) {
const temp = y;
y = x % y;
x = temp;
combined = Math.abs(combined / gcd(combined, integer) * integer);
if (!Number.isSafeInteger(combined)) {
return null;
}
}

return Math.abs(x) / m;
};

/**
* @param {number} a
* @param {number} b
* @returns {number}
*/
const lcm = (a, b) => {
const m = 10 ** Math.max(countDecimals(a), countDecimals(b));
const x = Math.round(a * m);
const y = Math.round(b * m);

return Math.abs(x * y) / (gcd(x, y) * m);
return combined / m;
};

export default multipleOfErrorHandler;
9 changes: 7 additions & 2 deletions src/localization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { translations } from "./translations/index.js";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { FluentBundle, FluentNumber, FluentResource } from "@fluent/bundle";

/**
* @import { FluentVariable} from "@fluent/bundle"
Expand Down Expand Up @@ -106,10 +106,15 @@ export class Localization {
* @type (messageId: string, args?: Record<string, FluentVariable>) => string
*/
format(messageId, args = {}) {
// Fluent rounds to three fractional digits by default, which would display
// a value like 0.0000001 as 0.
const localizedArgs = Object.fromEntries(Object.entries(args).map(([name, value]) => [
name, typeof value === "number" ? new FluentNumber(value, { maximumSignificantDigits: 21 }) : value
]));
for (const bundle of [this.bundle, getBundle(DEFAULT_LOCALE)]) {
const message = bundle?.getMessage(messageId);
if (bundle && message?.value) {
return bundle.formatPattern(message.value, args);
return bundle.formatPattern(message.value, localizedArgs);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/test-suite/tests/maximum.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
"instance": 3,
"errors": []
},
{
"description": "maximum with more than three decimal places",
"schema": {
"maximum": 1.2345
},
"instance": 2,
"errors": [
{
"messageId": "maximum-message",
"messageParams": {
"maximum": "1.2345"
},
"instanceLocation": "#",
"schemaLocations": ["#/maximum"]
}
]
},
{
"description": "exclusiveMaximum",
"compatibility": "6",
Expand Down
17 changes: 17 additions & 0 deletions src/test-suite/tests/minimum.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
"instance": 5,
"errors": []
},
{
"description": "minimum with more than three decimal places",
"schema": {
"minimum": 1e-7
},
"instance": 0,
"errors": [
{
"messageId": "minimum-message",
"messageParams": {
"minimum": "0.0000001"
},
"instanceLocation": "#",
"schemaLocations": ["#/minimum"]
}
]
},
{
"description": "exclusiveMinimum",
"compatibility": "6",
Expand Down
Loading
Loading