|
| 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 | +import jsonStringify from "json-stringify-deterministic"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @import { ErrorHandler, Json } from "../index.d.ts" |
| 8 | + */ |
| 9 | + |
| 10 | +const ALL_TYPES = new Set(["null", "boolean", "number", "string", "array", "object", "integer"]); |
| 11 | + |
| 12 | +/** @type {ErrorHandler} */ |
| 13 | +const typeConstEnumErrorHandler = async (normalizedErrors, instance, localization) => { |
| 14 | + let allowedTypes = new Set(ALL_TYPES); |
| 15 | + /** @type {string[]} */ |
| 16 | + const failedTypeLocations = []; |
| 17 | + |
| 18 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/type"]) { |
| 19 | + if (!normalizedErrors["https://json-schema.org/keyword/type"][schemaLocation]) { |
| 20 | + failedTypeLocations.push(schemaLocation); |
| 21 | + |
| 22 | + const keyword = await getSchema(schemaLocation); |
| 23 | + /** @type {string | string[]} */ |
| 24 | + const value = Schema.value(keyword); |
| 25 | + const types = Array.isArray(value) ? value : [value]; |
| 26 | + /** @type {Set<string>} */ |
| 27 | + const keywordTypes = new Set(types); |
| 28 | + if (keywordTypes.has("number")) { |
| 29 | + keywordTypes.add("integer"); |
| 30 | + } |
| 31 | + allowedTypes = allowedTypes.intersection(keywordTypes); |
| 32 | + } |
| 33 | + } |
| 34 | + if (allowedTypes.has("number")) { |
| 35 | + allowedTypes.delete("integer"); |
| 36 | + } |
| 37 | + |
| 38 | + /** @type {Set<string> | undefined} */ |
| 39 | + let allowedJson; |
| 40 | + |
| 41 | + /** @type {string[]} */ |
| 42 | + const constEnumLocations = []; |
| 43 | + /** @type {string[]} */ |
| 44 | + const failedConstLocations = []; |
| 45 | + /** @type {string[]} */ |
| 46 | + const failedEnumLocations = []; |
| 47 | + let typeFiltered = false; |
| 48 | + |
| 49 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/const"]) { |
| 50 | + constEnumLocations.push(schemaLocation); |
| 51 | + if (!normalizedErrors["https://json-schema.org/keyword/const"][schemaLocation]) { |
| 52 | + failedConstLocations.push(schemaLocation); |
| 53 | + } |
| 54 | + |
| 55 | + const keyword = await getSchema(schemaLocation); |
| 56 | + const keywordJson = new Set(); |
| 57 | + if (allowedTypes.has(Schema.typeOf(keyword))) { |
| 58 | + keywordJson.add(jsonStringify(Schema.value(keyword))); |
| 59 | + } else { |
| 60 | + typeFiltered = true; |
| 61 | + } |
| 62 | + |
| 63 | + allowedJson = allowedJson?.intersection(keywordJson) ?? keywordJson; |
| 64 | + } |
| 65 | + |
| 66 | + for (const schemaLocation in normalizedErrors["https://json-schema.org/keyword/enum"]) { |
| 67 | + constEnumLocations.push(schemaLocation); |
| 68 | + if (!normalizedErrors["https://json-schema.org/keyword/enum"][schemaLocation]) { |
| 69 | + failedEnumLocations.push(schemaLocation); |
| 70 | + } |
| 71 | + |
| 72 | + const keyword = await getSchema(schemaLocation); |
| 73 | + const keywordJson = new Set(); |
| 74 | + for await (const enumValueNode of Schema.iter(keyword)) { |
| 75 | + if (allowedTypes.has(Schema.typeOf(enumValueNode))) { |
| 76 | + keywordJson.add(jsonStringify(Schema.value(enumValueNode))); |
| 77 | + } else { |
| 78 | + typeFiltered = true; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + allowedJson = allowedJson?.intersection(keywordJson) ?? keywordJson; |
| 83 | + } |
| 84 | + |
| 85 | + const failedLocations = failedConstLocations.length > 0 |
| 86 | + ? failedConstLocations |
| 87 | + : failedEnumLocations; |
| 88 | + |
| 89 | + if (failedLocations.length === 0 && failedTypeLocations.length === 0) { |
| 90 | + return []; |
| 91 | + } else if (allowedTypes.size === 0 || allowedJson?.size === 0) { |
| 92 | + return [{ |
| 93 | + message: localization.getBooleanSchemaErrorMessage(), |
| 94 | + instanceLocation: Instance.uri(instance), |
| 95 | + schemaLocations: [...failedTypeLocations, ...constEnumLocations] |
| 96 | + }]; |
| 97 | + } else if (allowedJson?.size) { |
| 98 | + /** @type Json[] */ |
| 99 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-return |
| 100 | + const allowedValues = [...allowedJson ?? []].map((json) => JSON.parse(json)); |
| 101 | + |
| 102 | + return [{ |
| 103 | + message: localization.getEnumErrorMessage(allowedValues), |
| 104 | + instanceLocation: Instance.uri(instance), |
| 105 | + schemaLocations: typeFiltered |
| 106 | + ? [...failedTypeLocations, ...constEnumLocations] |
| 107 | + : failedLocations |
| 108 | + }]; |
| 109 | + } else { |
| 110 | + return [{ |
| 111 | + message: localization.getTypeErrorMessage([...allowedTypes]), |
| 112 | + instanceLocation: Instance.uri(instance), |
| 113 | + schemaLocations: failedTypeLocations |
| 114 | + }]; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +export default typeConstEnumErrorHandler; |
0 commit comments