From e290f0bb421f9c7730843053914452db82a1d606 Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Fri, 24 May 2024 16:27:41 +1000 Subject: [PATCH] Fix URI resolution for inline $refs in schemas Fixes #81 --- src/uri.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/uri.js b/src/uri.js index a691427..e013e6e 100644 --- a/src/uri.js +++ b/src/uri.js @@ -1,11 +1,17 @@ -/* +/** * This is just a hack to adapt the jsonschema lib to work without a polyfill for the "url" lib. */ -// https://nodejs.org/api/url.html#urlresolvefrom-to +/** + * @see {@link https://nodejs.org/api/url.html#urlresolvefrom-to} + * @param {string} from + * @param {string} to + * @returns {string} + */ export const resolve = (from, to) => { if (!to) return from; - const resolved = new URL(to, new URL(from, "resolve://")); + if (to.startsWith("#")) return (from || "") + to; + const resolved = new URL(to, new URL(from, "resolve://resolve/")); if (resolved.protocol === "resolve:") { const { pathname, search, hash } = resolved; @@ -15,4 +21,8 @@ export const resolve = (from, to) => { return resolved.toString(); }; +/** + * @param {string} uri + * @returns {URL} + */ export const parse = (uri) => new URL(uri);