Skip to content

Commit 8150b4c

Browse files
committed
feat: Define baseDir option
This option let you define the base dir to determine definitions relative path
1 parent 0ee7c49 commit 8150b4c

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/lib/Unifier.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ interface JsonSchemaUnifierOptions {
1212
logs?: boolean;
1313
definitionsPath?: string;
1414
definitionsPathSeparator?: string;
15+
/** If defined, the definitions path will be relative to the value. By default, it's relative to the main schema */
16+
baseDir?: string;
1517
}
1618

1719

@@ -109,15 +111,19 @@ export class JsonSchemaUnifier {
109111
this.log("newPath", path);
110112
const parts = path.split("#");
111113
if (parts[0] === this.mainSchemaPath) return `#${parts[1]}`;
112-
const relativePath = relative(this.mainSchemaPath, parts[0]);
114+
const relativePath = relative(this.options.baseDir ? this.options.baseDir : this.mainSchemaPath, parts[0]);
113115
this.log("Relative path", relativePath);
114-
const defPathParts = [
115-
...relativePath
116-
.replace(/^(\.+[\\/])+/i, "")
117-
.replace(/(\.schema)?\.(json|yaml|yml)$/i, "")
118-
.split("/")
119-
.filter(p => p)
120-
];
116+
const defPathParts = relativePath
117+
.replace(/(\.schema)?\.(json|yaml|yml)$/i, "")
118+
.split("/")
119+
.filter(p => p && p !== ".");
120+
while (defPathParts.includes('..')) {
121+
const index = defPathParts.indexOf('..');
122+
if (index > 0)
123+
defPathParts.splice(index - 1, 2);
124+
else
125+
defPathParts.splice(index, 1);
126+
}
121127
if (parts.length > 1) {
122128
defPathParts.push(...parts[1].split("/").filter(p => p));
123129
}

0 commit comments

Comments
 (0)