|
| 1 | +import * as Browser from "@hyperjump/browser"; |
| 2 | +import { registerSchema, unregisterSchema } from "@hyperjump/json-schema/draft-2020-12"; |
| 3 | +import { getSchema } from "@hyperjump/json-schema/experimental"; |
| 4 | +import { pointerSegments } from "@hyperjump/json-pointer"; |
| 5 | +import { randomUUID } from "crypto"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @import { OutputFormat, OutputUnit, NormalizedError, SchemaObject} from "../index.d.ts"; |
| 9 | + * @import { SchemaDocument } from "@hyperjump/json-schema/experimental"; |
| 10 | + * @import { Browser as BrowserType } from "@hyperjump/browser"; |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {OutputFormat} errorOutput |
| 15 | + * @param {SchemaObject} schema |
| 16 | + * @returns {Promise<NormalizedError[]>} |
| 17 | + */ |
| 18 | +export async function normalizeOutputFormat(errorOutput, schema) { |
| 19 | + /** @type {NormalizedError[]} */ |
| 20 | + const output = []; |
| 21 | + |
| 22 | + if (!errorOutput || errorOutput.valid !== false) { |
| 23 | + throw new Error("error Output must follow Draft 2019-09"); |
| 24 | + } |
| 25 | + |
| 26 | + const keywords = new Set([ |
| 27 | + "type", "minLength", "maxLength", "minimum", "maximum", "format", "pattern", |
| 28 | + "enum", "const", "required", "items", "properties", "allOf", "anyOf", "oneOf", |
| 29 | + "not", "contains", "uniqueItems", "additionalProperties", "minItems", "maxItems", |
| 30 | + "minProperties", "maxProperties", "dependentRequired", "dependencies" |
| 31 | + ]); |
| 32 | + |
| 33 | + /** @type {(errorOutput: OutputUnit) => Promise<void>} */ |
| 34 | + async function collectErrors(error) { |
| 35 | + if (error.valid) return; |
| 36 | + |
| 37 | + if (!("instanceLocation" in error) || !("absoluteKeywordLocation" in error || "keywordLocation" in error)) { |
| 38 | + throw new Error("error Output must follow Draft 2019-09"); |
| 39 | + } |
| 40 | + |
| 41 | + const absoluteKeywordLocation = error.absoluteKeywordLocation |
| 42 | + ?? await toAbsoluteKeywordLocation(schema, /** @type string */ (error.keywordLocation)); |
| 43 | + |
| 44 | + const fragment = absoluteKeywordLocation.split("#")[1]; |
| 45 | + const lastSegment = fragment.split("/").filter(Boolean).pop(); |
| 46 | + |
| 47 | + // make a check here to remove the schemaLocation. |
| 48 | + if (lastSegment && keywords.has(lastSegment)) { |
| 49 | + output.push({ |
| 50 | + valid: false, |
| 51 | + absoluteKeywordLocation, |
| 52 | + instanceLocation: normalizeInstanceLocation(error.instanceLocation) |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + if (error.errors) { |
| 57 | + for (const nestedError of error.errors) { |
| 58 | + await collectErrors(nestedError); // Recursive |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!errorOutput.errors) { |
| 64 | + throw new Error("error Output must follow Draft 2019-09"); |
| 65 | + } |
| 66 | + |
| 67 | + for (const err of errorOutput.errors) { |
| 68 | + await collectErrors(err); |
| 69 | + } |
| 70 | + |
| 71 | + return output; |
| 72 | +} |
| 73 | + |
| 74 | +/** @type {(location: string) => string} */ |
| 75 | +function normalizeInstanceLocation(location) { |
| 76 | + return location.startsWith("/") || location === "" ? `#${location}` : location; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Convert keywordLocation to absoluteKeywordLocation |
| 81 | + * @param {SchemaObject} schema |
| 82 | + * @param {string} keywordLocation |
| 83 | + * @returns {Promise<string>} |
| 84 | + */ |
| 85 | +export async function toAbsoluteKeywordLocation(schema, keywordLocation) { |
| 86 | + const uri = `urn:uuid:${randomUUID()}`; |
| 87 | + try { |
| 88 | + registerSchema(schema, uri); |
| 89 | + |
| 90 | + let browser = await getSchema(uri); |
| 91 | + for (const segment of pointerSegments(keywordLocation)) { |
| 92 | + browser = /** @type BrowserType<SchemaDocument> */ (await Browser.step(segment, browser)); |
| 93 | + } |
| 94 | + |
| 95 | + return `${browser.document.baseUri}#${browser.cursor}`; |
| 96 | + } finally { |
| 97 | + unregisterSchema(uri); |
| 98 | + } |
| 99 | +} |
0 commit comments