|
| 1 | +import { getSchema } from "@hyperjump/json-schema/experimental"; |
| 2 | +import * as Schema from "@hyperjump/browser"; |
| 3 | +import * as Instance from "@hyperjump/json-schema/instance/experimental"; |
| 4 | + |
| 5 | +/** |
| 6 | + * @import { NumberConstraints } from "../localization.js" |
| 7 | + * @import { ErrorHandler } from "../index.d.ts" |
| 8 | + */ |
| 9 | + |
| 10 | +/** @type ErrorHandler */ |
| 11 | +const numberRangeHandler = async (normalizedErrors, instance, localization) => { |
| 12 | + /** @type NumberConstraints */ |
| 13 | + const constraints = {}; |
| 14 | + |
| 15 | + /** @type string[] */ |
| 16 | + const failedSchemaLocations = []; |
| 17 | + |
| 18 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/minimum"]) { |
| 19 | + if (!normalizedErrors["https://json-schema.org/keyword/minimum"][schemaLocation]) { |
| 20 | + failedSchemaLocations.push(schemaLocation); |
| 21 | + } |
| 22 | + |
| 23 | + const keyword = await getSchema(schemaLocation); |
| 24 | + /** @type number */ |
| 25 | + const minimum = Schema.value(keyword); |
| 26 | + constraints.minimum = Math.max(constraints.minimum ?? Number.MIN_VALUE, minimum); |
| 27 | + } |
| 28 | + |
| 29 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/exclusiveMinimum"]) { |
| 30 | + if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMinimum"][schemaLocation]) { |
| 31 | + failedSchemaLocations.push(schemaLocation); |
| 32 | + } |
| 33 | + |
| 34 | + const keyword = await getSchema(schemaLocation); |
| 35 | + /** @type number */ |
| 36 | + const minimum = Schema.value(keyword); |
| 37 | + constraints.minimum = Math.max(constraints.minimum ?? Number.MIN_VALUE, minimum); |
| 38 | + if (constraints.minimum === minimum) { |
| 39 | + constraints.exclusiveMinimum = true; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/maximum"]) { |
| 44 | + if (!normalizedErrors["https://json-schema.org/keyword/maximum"][schemaLocation]) { |
| 45 | + failedSchemaLocations.push(schemaLocation); |
| 46 | + } |
| 47 | + |
| 48 | + const keyword = await getSchema(schemaLocation); |
| 49 | + /** @type number */ |
| 50 | + const maximum = Schema.value(keyword); |
| 51 | + constraints.maximum = Math.min(constraints.maximum ?? Number.MAX_VALUE, maximum); |
| 52 | + } |
| 53 | + |
| 54 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/exclusiveMaximum"]) { |
| 55 | + if (!normalizedErrors["https://json-schema.org/keyword/exclusiveMaximum"][schemaLocation]) { |
| 56 | + failedSchemaLocations.push(schemaLocation); |
| 57 | + } |
| 58 | + |
| 59 | + const keyword = await getSchema(schemaLocation); |
| 60 | + /** @type number */ |
| 61 | + const maximum = Schema.value(keyword); |
| 62 | + constraints.maximum = Math.min(constraints.maximum ?? Number.MAX_VALUE, maximum); |
| 63 | + if (constraints.maximum === maximum) { |
| 64 | + constraints.exclusiveMaximum = true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (failedSchemaLocations.length > 0) { |
| 69 | + return [ |
| 70 | + { |
| 71 | + message: localization.getNumberErrorMessage(constraints), |
| 72 | + instanceLocation: Instance.uri(instance), |
| 73 | + schemaLocation: failedSchemaLocations.length > 1 ? failedSchemaLocations : failedSchemaLocations[0] |
| 74 | + } |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + return []; |
| 79 | +}; |
| 80 | + |
| 81 | +export default numberRangeHandler; |
0 commit comments