-
Notifications
You must be signed in to change notification settings - Fork 20
/
getRef.js
121 lines (121 loc) · 4.67 KB
/
getRef.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { get } from "@sagold/json-pointer";
import splitRef from "./splitRef";
import getTypeOf from "../getTypeOf";
const suffixes = /(#)+$/g;
const isObject = (val) => getTypeOf(val) === "object";
// 1. combined is known
// 2. base or pointer is known
// 3. base + pointer is known
export default function getRef(context, rootSchema, $search) {
var _a, _b, _c, _d, _e;
let $ref;
if (isObject($search)) {
$ref = $search.__ref || $search.$ref;
}
else {
$ref = $search;
}
if ($ref == null) {
return rootSchema;
}
// console.log("\n$ref", $ref);
let schema;
// is it a known $ref?
const $remote = $ref.replace(suffixes, "");
if (context.remotes[$remote] != null) {
schema = context.remotes[$remote];
// console.log("» remote");
if (schema && schema.$ref) {
// console.log("»» reresolve", schema);
// @todo add missing test for the following line
return getRef(context, schema, schema);
}
// console.log("»» return", schema);
return schema;
}
// @ts-expect-error @draft 2019-09
const $anchor = (_a = context.anchors) === null || _a === void 0 ? void 0 : _a[$ref];
if ($anchor) {
// console.log("» anchor", $anchor);
return get(rootSchema, $anchor);
}
if (context.ids[$ref] != null) {
// console.log("» id", context.ids[$ref]);
schema = get(rootSchema, context.ids[$ref]);
if (schema && schema.$ref) {
// @todo add missing test for the following line
return getRef(context, rootSchema, schema);
}
return schema;
}
const $inputRef = $ref;
// is it a ref with host/pointer?
const fragments = splitRef($ref);
if (fragments.length === 0) {
return rootSchema;
}
if (fragments.length === 1) {
// console.log("» frag1", fragments);
// console.log("ids", rootSchema.getContext().ids);
$ref = fragments[0];
if (context.remotes[$ref]) {
// console.log("» remote");
schema = context.remotes[$ref];
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema);
}
}
if (context.ids[$ref]) {
// console.log("» id");
schema = get(rootSchema, context.ids[$ref]);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema);
}
return schema;
}
// @todo why this special case
const rootContextRef = (_b = rootSchema.getContext) === null || _b === void 0 ? void 0 : _b.call(rootSchema).ids[$ref];
if (rootContextRef) {
return getRef(context, rootSchema, rootContextRef);
}
}
if (fragments.length === 2) {
// console.log("» frag2", fragments);
const base = fragments[0];
$ref = fragments[1];
// @todo this is unnecessary due to inconsistencies
const fromRemote = (_c = context.remotes[base]) !== null && _c !== void 0 ? _c : context.remotes[`${base}/`];
if (fromRemote) {
// console.log("» remote");
// We have retrieved a different compiled json-schema. This compiled schema contains a
// separate scope (context) where we might need to work with
// ANCHOR
if (fromRemote.getContext && fromRemote.getContext().anchors[$inputRef] != null) {
// console.log("» remote » anchor");
// an anchor is stored with its scope (id) it is defined in. Thus collisions are
// avoided, but the current condition is required to resolve the anchor for now
return fromRemote.getRef($inputRef);
}
// PATH (get_ref)
if (fromRemote.getRef) {
// console.log("» remote » ref");
// resolve the local part of the reference in the new schema
return fromRemote.getRef($ref);
}
//log("warning: uncompiled remote - context may be wrong", base);
return getRef(context, fromRemote, $ref);
}
// @todo this is unnecessary due to inconsistencies
const fromId = (_d = context.ids[base]) !== null && _d !== void 0 ? _d : context.ids[`${base}/`];
if (fromId) {
// console.log("» id", fromId);
return getRef(context, get(rootSchema, fromId), $ref);
}
}
// console.log("» other");
schema = get(rootSchema, (_e = context.ids[$ref]) !== null && _e !== void 0 ? _e : $ref);
if (schema && schema.$ref) {
return getRef(context, rootSchema, schema);
}
return schema;
}