-
Notifications
You must be signed in to change notification settings - Fork 376
/
resolvers.ts
190 lines (173 loc) · 6.13 KB
/
resolvers.ts
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import isEmpty from 'lodash/isEmpty';
import get from 'lodash/get';
import { JsonSchema } from '../models';
import { decode, encode } from './path';
/**
* Map for storing refs and the respective schemas they are pointing to.
*/
export interface ReferenceSchemaMap {
[ref: string]: JsonSchema;
}
const isObjectSchema = (schema: JsonSchema): boolean => {
return schema.properties !== undefined;
};
const isArraySchema = (schema: JsonSchema): boolean => {
return schema.type === 'array' && schema.items !== undefined;
};
export const resolveData = (instance: any, dataPath: string): any => {
if (isEmpty(dataPath)) {
return instance;
}
const dataPathSegments = dataPath.split('.');
return dataPathSegments
.map(segment => decodeURIComponent(segment))
.reduce((curInstance, decodedSegment) => {
if (!curInstance || !curInstance.hasOwnProperty(decodedSegment)) {
return undefined;
}
return curInstance[decodedSegment];
}, instance);
};
/**
* Finds all references inside the given schema.
*
* @param schema The {@link JsonSchema} to find the references in
* @param result The initial result map, default: empty map (this parameter is used for recursion
* inside the function)
* @param resolveTuples Whether arrays of tuples should be considered; default: false
*/
export const findAllRefs = (
schema: JsonSchema,
result: ReferenceSchemaMap = {},
resolveTuples = false
): ReferenceSchemaMap => {
if (isObjectSchema(schema)) {
Object.keys(schema.properties).forEach(key =>
findAllRefs(schema.properties[key], result)
);
}
if (isArraySchema(schema)) {
if (Array.isArray(schema.items)) {
if (resolveTuples) {
const items: JsonSchema[] = schema.items;
items.forEach(child => findAllRefs(child, result));
}
} else {
findAllRefs(schema.items, result);
}
}
if (Array.isArray(schema.anyOf)) {
const anyOf: JsonSchema[] = schema.anyOf;
anyOf.forEach(child => findAllRefs(child, result));
}
if (schema.$ref !== undefined) {
result[schema.$ref] = schema;
}
return result;
};
const invalidSegment = (pathSegment: string) =>
pathSegment === '#' || pathSegment === undefined || pathSegment === '';
/**
* Resolve the given schema path in order to obtain a subschema.
* @param {JsonSchema} schema the root schema from which to start
* @param {string} schemaPath the schema path to be resolved
* @param {JsonSchema} rootSchema the actual root schema
* @returns {JsonSchema} the resolved sub-schema
*/
export const resolveSchema = (
schema: JsonSchema,
schemaPath: string,
rootSchema?: JsonSchema
): JsonSchema => {
if (isEmpty(schema)) {
return undefined;
}
const validPathSegments = schemaPath.split('/').map(decode);
let resultSchema = schema;
for (let i = 0; i < validPathSegments.length; i++) {
let pathSegment = validPathSegments[i];
resultSchema =
resultSchema === undefined || resultSchema.$ref === undefined
? resultSchema
: resolveSchema(schema, resultSchema.$ref);
if (invalidSegment(pathSegment)) {
// skip invalid segments
continue;
}
let curSchema = get(resultSchema, pathSegment);
if (!curSchema) {
// resolving was not successful, check whether the scope omitted an oneOf, allOf or anyOf and resolve anyway
const schemas = [].concat(
resultSchema?.oneOf ?? [],
resultSchema?.allOf ?? [],
resultSchema?.anyOf ?? []
);
for (let item of schemas) {
curSchema = resolveSchema(item, validPathSegments.slice(i).map(encode).join('/'));
if (curSchema) {
break;
}
}
if (curSchema) {
// already resolved rest of the path
resultSchema = curSchema;
break;
}
}
resultSchema = curSchema;
}
// TODO: because schema is already scoped we might end up with refs pointing
// outside of the current schema. It would be better if we'd always could deal
// with absolute paths here, so that we don't need to keep two different
// schemas around
if (resultSchema !== undefined && resultSchema.$ref !== undefined) {
try {
return retrieveResolvableSchema(schema, resultSchema.$ref);
} catch (e) {
return retrieveResolvableSchema(rootSchema, resultSchema.$ref);
}
}
return resultSchema;
};
/**
* Normalizes the schema and resolves the given ref.
*
* @param {JsonSchema} full the JSON schema to resolved the reference against
* @param {string} reference the reference to be resolved
* @returns {JsonSchema} the resolved sub-schema
*/
// disable rule because resolve is mutually recursive
// tslint:disable:only-arrow-functions
function retrieveResolvableSchema(
full: JsonSchema,
reference: string
): JsonSchema {
// tslint:enable:only-arrow-functions
const child = resolveSchema(full, reference);
const allRefs = findAllRefs(child);
const innerSelfReference = allRefs[reference];
if (innerSelfReference !== undefined) {
innerSelfReference.$ref = '#';
}
return child;
}