@apidevtools/json-schema-ref-parser
Advanced tools
Comparing version 11.0.0 to 11.1.0
@@ -133,3 +133,5 @@ "use strict"; | ||
// console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path); | ||
const $refPath = url.resolve(path, $ref.$ref); | ||
const isExternalRef = ref_js_1.default.isExternal$Ref($ref); | ||
const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root"; | ||
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref); | ||
const cache = dereferencedCache.get($refPath); | ||
@@ -136,0 +138,0 @@ if (cache) { |
@@ -91,2 +91,9 @@ "use strict"; | ||
} | ||
else if (!args.path && args.schema && args.schema.$id) { | ||
// when schema id has defined an URL should use that hostname to request the references, | ||
// instead of using the current page URL | ||
const params = url.parse(args.schema.$id); | ||
const port = params.protocol === "https:" ? 443 : 80; | ||
args.path = `${params.protocol}//${params.hostname}:${port}`; | ||
} | ||
// Resolve the absolute path of the schema | ||
@@ -93,0 +100,0 @@ args.path = url.resolve(url.cwd(), args.path); |
@@ -72,2 +72,8 @@ import type { HTTPResolverOptions, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js"; | ||
onDereference?(path: string, value: JSONSchemaObject): void; | ||
/** | ||
* Whether a reference should resolve relative to its directory/path, or from the cwd | ||
* | ||
* Default: `relative` | ||
*/ | ||
externalReferenceResolution?: "relative" | "root"; | ||
}; | ||
@@ -74,0 +80,0 @@ } |
@@ -72,2 +72,3 @@ "use strict"; | ||
excludedPathMatcher: () => false, | ||
referenceResolution: "relative", | ||
}, | ||
@@ -74,0 +75,0 @@ }; |
@@ -105,4 +105,4 @@ "use strict"; | ||
async function resolve$Ref($ref, path, $refs, options) { | ||
// console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path); | ||
const resolvedPath = url.resolve(path, $ref.$ref); | ||
const shouldResolveOnCwd = options.dereference.externalReferenceResolution === "root"; | ||
const resolvedPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref); | ||
const withoutHash = url.stripHash(resolvedPath); | ||
@@ -109,0 +109,0 @@ // $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath); |
@@ -100,3 +100,3 @@ "use strict"; | ||
else { | ||
const redirectTo = url.resolve(u, res.headers.location); | ||
const redirectTo = url.resolve(u.href, res.headers.location); | ||
return download(redirectTo, httpOptions, redirects); | ||
@@ -103,0 +103,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export declare const parse: (u: any) => URL; | ||
export declare const parse: (u: string | URL) => URL; | ||
/** | ||
@@ -7,3 +7,3 @@ * Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. | ||
*/ | ||
export declare function resolve(from: any, to: any): string; | ||
export declare function resolve(from: string, to: string): string; | ||
/** | ||
@@ -95,2 +95,2 @@ * Returns the current working directory (in Node) or the current page URL (in browsers). | ||
export declare function safePointerToPath(pointer: any): any; | ||
export declare function relative(from: string | undefined, to: string | undefined): string; | ||
export declare function relative(from: string, to: string): string; |
@@ -51,3 +51,4 @@ "use strict"; | ||
function resolve(from, to) { | ||
const resolvedUrl = new URL(to, new URL(from, "resolve://")); | ||
const fromUrl = new URL((0, convert_path_to_posix_1.default)(from), "resolve://"); | ||
const resolvedUrl = new URL((0, convert_path_to_posix_1.default)(to), fromUrl); | ||
if (resolvedUrl.protocol === "resolve:") { | ||
@@ -54,0 +55,0 @@ // `from` is a relative URL. |
@@ -172,3 +172,5 @@ import $Ref from "./ref.js"; | ||
const $refPath = url.resolve(path, $ref.$ref); | ||
const isExternalRef = $Ref.isExternal$Ref($ref); | ||
const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root"; | ||
const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref); | ||
@@ -175,0 +177,0 @@ const cache = dereferencedCache.get($refPath); |
@@ -103,2 +103,9 @@ import $Refs from "./refs.js"; | ||
pathType = "file"; | ||
} else if (!args.path && args.schema && args.schema.$id) { | ||
// when schema id has defined an URL should use that hostname to request the references, | ||
// instead of using the current page URL | ||
const params = url.parse(args.schema.$id); | ||
const port = params.protocol === "https:" ? 443 : 80; | ||
args.path = `${params.protocol}//${params.hostname}:${port}`; | ||
} | ||
@@ -105,0 +112,0 @@ |
@@ -86,2 +86,9 @@ import jsonParser from "./parsers/json.js"; | ||
onDereference?(path: string, value: JSONSchemaObject): void; | ||
/** | ||
* Whether a reference should resolve relative to its directory/path, or from the cwd | ||
* | ||
* Default: `relative` | ||
*/ | ||
externalReferenceResolution?: "relative" | "root"; | ||
}; | ||
@@ -153,4 +160,5 @@ } | ||
excludedPathMatcher: () => false, | ||
referenceResolution: "relative", | ||
}, | ||
}; | ||
} as $RefParserOptions; | ||
return cloneDeep(defaults); | ||
@@ -157,0 +165,0 @@ }; |
@@ -98,3 +98,3 @@ import { ono } from "@jsdevtools/ono"; | ||
set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type) { | ||
const absPath = url.resolve(this._root$Ref.path, path); | ||
const absPath = url.resolve(this._root$Ref.path!, path); | ||
const withoutHash = url.stripHash(absPath); | ||
@@ -117,3 +117,3 @@ const $ref = this._$refs[withoutHash]; | ||
_get$Ref(path: any) { | ||
path = url.resolve(this._root$Ref.path, path); | ||
path = url.resolve(this._root$Ref.path!, path); | ||
const withoutHash = url.stripHash(path); | ||
@@ -150,3 +150,3 @@ return this._$refs[withoutHash]; | ||
_resolve(path: string, pathFromRoot: string, options?: any) { | ||
const absPath = url.resolve(this._root$Ref.path, path); | ||
const absPath = url.resolve(this._root$Ref.path!, path); | ||
const withoutHash = url.stripHash(absPath); | ||
@@ -153,0 +153,0 @@ const $ref = this._$refs[withoutHash]; |
@@ -95,5 +95,4 @@ import $Ref from "./ref.js"; | ||
async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options: Options) { | ||
// console.log('Resolving $ref pointer "%s" at %s', $ref.$ref, path); | ||
const resolvedPath = url.resolve(path, $ref.$ref); | ||
const shouldResolveOnCwd = options.dereference.externalReferenceResolution === "root"; | ||
const resolvedPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref!); | ||
const withoutHash = url.stripHash(resolvedPath); | ||
@@ -100,0 +99,0 @@ |
@@ -89,3 +89,3 @@ import { ono } from "@jsdevtools/ono"; | ||
} else { | ||
const redirectTo = url.resolve(u, res.headers.location); | ||
const redirectTo = url.resolve(u.href, res.headers.location as string); | ||
return download(redirectTo, httpOptions, redirects); | ||
@@ -92,0 +92,0 @@ } |
@@ -19,3 +19,3 @@ import convertPathToPosix from "./convert-path-to-posix"; | ||
export const parse = (u: any) => new URL(u); | ||
export const parse = (u: string | URL) => new URL(u); | ||
@@ -27,4 +27,5 @@ /** | ||
*/ | ||
export function resolve(from: any, to: any) { | ||
const resolvedUrl = new URL(to, new URL(from, "resolve://")); | ||
export function resolve(from: string, to: string) { | ||
const fromUrl = new URL(convertPathToPosix(from), "resolve://"); | ||
const resolvedUrl = new URL(convertPathToPosix(to), fromUrl); | ||
if (resolvedUrl.protocol === "resolve:") { | ||
@@ -284,3 +285,3 @@ // `from` is a relative URL. | ||
export function relative(from: string | undefined, to: string | undefined) { | ||
export function relative(from: string, to: string) { | ||
if (!isFileSystemPath(from) || !isFileSystemPath(to)) { | ||
@@ -287,0 +288,0 @@ return resolve(from, to); |
{ | ||
"name": "@apidevtools/json-schema-ref-parser", | ||
"version": "11.0.0", | ||
"version": "11.1.0", | ||
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
307180
7138