1// $ref 引用
2function getPathVal(obj, pathStr) {
3    const pathArr = pathStr.split('/');
4    for (let i = 0; i < pathArr.length; i += 1) {
5        if (obj === undefined) return undefined;
6        obj = pathArr[i] === '' ? obj : obj[pathArr[i]];
7    }
8    return obj;
9}
10
11// 找到ref引用的schema
12export default function findSchemaDefinition($ref, rootSchema = {}) {
13    const origRef = $ref;
14    if ($ref.startsWith('#')) {
15        // Decode URI fragment representation.
16        $ref = decodeURIComponent($ref.substring(1));
17    } else {
18        throw new Error(`Could not find a definition for ${origRef}.`);
19    }
20    const current = getPathVal(rootSchema, $ref);
21
22    if (current === undefined) {
23        throw new Error(`Could not find a definition for ${origRef}.`);
24    }
25    if (current.hasOwnProperty('$ref')) {
26        return findSchemaDefinition(current.$ref, rootSchema);
27    }
28    return current;
29}
30