forked from rjsf-team/react-jsonschema-form
/
retrieveSchema.ts
498 lines (477 loc) · 16 KB
/
retrieveSchema.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import get from "lodash/get";
import set from "lodash/set";
import mergeAllOf, { Options } from "json-schema-merge-allof";
import {
ADDITIONAL_PROPERTIES_KEY,
ADDITIONAL_PROPERTY_FLAG,
ALL_OF_KEY,
DEPENDENCIES_KEY,
REF_KEY,
} from "../constants";
import findSchemaDefinition, {
splitKeyElementFromObject,
} from "../findSchemaDefinition";
import guessType from "../guessType";
import isObject from "../isObject";
import mergeSchemas from "../mergeSchemas";
import {
GenericObjectType,
RJSFSchema,
StrictRJSFSchema,
ValidatorType,
} from "../types";
import getMatchingOption from "./getMatchingOption";
/** Resolves a conditional block (if/else/then) by removing the condition and merging the appropriate conditional branch
* with the rest of the schema
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that is used to detect valid schema conditions
* @param schema - The schema for which resolving a condition is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param formData - The current formData to assist retrieving a schema
* @returns - A schema with the appropriate condition resolved
*/
export function resolveCondition<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(validator: ValidatorType<T, S>, schema: S, rootSchema: S, formData: T) {
const {
if: expression,
then,
else: otherwise,
...resolvedSchemaLessConditional
} = schema;
const conditionalSchema = validator.isValid(
expression as S,
formData,
rootSchema
)
? then
: otherwise;
if (conditionalSchema && typeof conditionalSchema !== "boolean") {
return retrieveSchema<T, S>(
validator,
mergeSchemas(
resolvedSchemaLessConditional,
retrieveSchema(validator, conditionalSchema, rootSchema, formData)
) as S,
rootSchema,
formData
);
}
return retrieveSchema<T, S>(
validator,
resolvedSchemaLessConditional as S,
rootSchema,
formData
);
}
/** Resolves references and dependencies within a schema and its 'allOf' children.
* Called internally by retrieveSchema.
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a schema is desired
* @param [rootSchema={}] - The root schema that will be forwarded to all the APIs
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @returns - The schema having its references and dependencies resolved
*/
export function resolveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema>(
validator: ValidatorType<T, S>,
schema: S,
rootSchema: S = {} as S,
formData?: T
): S {
if (REF_KEY in schema) {
return resolveReference<T, S>(validator, schema, rootSchema, formData);
}
if (DEPENDENCIES_KEY in schema) {
const resolvedSchema = resolveDependencies<T, S>(
validator,
schema,
rootSchema,
formData
);
return retrieveSchema<T, S>(
validator,
resolvedSchema,
rootSchema,
formData
);
}
if (ALL_OF_KEY in schema) {
return {
...schema,
allOf: schema.allOf!.map((allOfSubschema) =>
retrieveSchema<T, S>(
validator,
allOfSubschema as S,
rootSchema,
formData
)
),
};
}
// No $ref or dependencies attribute found, returning the original schema.
return schema;
}
/** Resolves references within a schema and its 'allOf' children.
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a reference is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @returns - The schema having its references resolved
*/
export function resolveReference<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(validator: ValidatorType<T, S>, schema: S, rootSchema: S, formData?: T): S {
// Retrieve the referenced schema definition.
const $refSchema = findSchemaDefinition<S>(schema.$ref, rootSchema);
// Drop the $ref property of the source schema.
const { $ref, ...localSchema } = schema;
// Update referenced schema definition with local schema properties.
return retrieveSchema<T, S>(
validator,
{ ...$refSchema, ...localSchema },
rootSchema,
formData
);
}
/** Creates new 'properties' items for each key in the `formData`
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be used when necessary
* @param theSchema - The schema for which the existing additional properties is desired
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param validator
* @param [aFormData] - The current formData, if any, to assist retrieving a schema
* @returns - The updated schema with additional properties stubbed
*/
export function stubExistingAdditionalProperties<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
theSchema: S,
rootSchema?: S,
aFormData?: T
): S {
// Clone the schema so we don't ruin the consumer's original
const schema = {
...theSchema,
properties: { ...theSchema.properties },
};
// make sure formData is an object
const formData: GenericObjectType =
aFormData && isObject(aFormData) ? aFormData : {};
Object.keys(formData).forEach((key) => {
if (key in schema.properties) {
// No need to stub, our schema already has the property
return;
}
let additionalProperties: S["additionalProperties"] = {};
if (typeof schema.additionalProperties !== "boolean") {
if (REF_KEY in schema.additionalProperties!) {
additionalProperties = retrieveSchema<T, S>(
validator,
{ $ref: get(schema.additionalProperties, [REF_KEY]) } as S,
rootSchema,
formData as T
);
} else if ("type" in schema.additionalProperties!) {
additionalProperties = { ...schema.additionalProperties };
} else {
additionalProperties = { type: guessType(get(formData, [key])) };
}
} else {
additionalProperties = { type: guessType(get(formData, [key])) };
}
// The type of our new key should match the additionalProperties value;
schema.properties[key] = additionalProperties;
// Set our additional property flag so we know it was dynamically added
set(schema.properties, [key, ADDITIONAL_PROPERTY_FLAG], true);
});
return schema;
}
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
* potentially recursive resolution.
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param schema - The schema for which retrieving a schema is desired
* @param [rootSchema={}] - The root schema that will be forwarded to all the APIs
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
*/
export default function retrieveSchema<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
schema: S,
rootSchema: S = {} as S,
rawFormData?: T
): S {
if (!isObject(schema)) {
return {} as S;
}
let resolvedSchema = resolveSchema<T, S>(
validator,
schema,
rootSchema,
rawFormData
);
if ("if" in schema) {
return resolveCondition<T, S>(
validator,
schema,
rootSchema,
rawFormData as T
);
}
const formData: GenericObjectType = rawFormData || {};
if (ALL_OF_KEY in schema) {
try {
resolvedSchema = mergeAllOf(resolvedSchema, {
deep: false,
} as Options) as S;
} catch (e) {
console.warn("could not merge subschemas in allOf:\n" + e);
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
return resolvedSchemaWithoutAllOf as S;
}
}
const hasAdditionalProperties =
ADDITIONAL_PROPERTIES_KEY in resolvedSchema &&
resolvedSchema.additionalProperties !== false;
if (hasAdditionalProperties) {
return stubExistingAdditionalProperties<T, S>(
validator,
resolvedSchema,
rootSchema,
formData as T
);
}
return resolvedSchema;
}
/** Resolves dependencies within a schema and its 'allOf' children.
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a dependency is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @returns - The schema with its dependencies resolved
*/
export function resolveDependencies<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(validator: ValidatorType<T, S>, schema: S, rootSchema: S, formData?: T): S {
// Drop the dependencies from the source schema.
const { dependencies, ...remainingSchema } = schema;
let resolvedSchema: S = remainingSchema as S;
if (Array.isArray(resolvedSchema.oneOf)) {
resolvedSchema = resolvedSchema.oneOf[
getMatchingOption<T, S>(
validator,
formData,
resolvedSchema.oneOf as S[],
rootSchema
)
] as S;
} else if (Array.isArray(resolvedSchema.anyOf)) {
resolvedSchema = resolvedSchema.anyOf[
getMatchingOption<T, S>(
validator,
formData,
resolvedSchema.anyOf as S[],
rootSchema
)
] as S;
}
return processDependencies<T, S>(
validator,
dependencies,
resolvedSchema,
rootSchema,
formData
);
}
/** Processes all the `dependencies` recursively into the `resolvedSchema` as needed
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param dependencies - The set of dependencies that needs to be processed
* @param resolvedSchema - The schema for which processing dependencies is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @returns - The schema with the `dependencies` resolved into it
*/
export function processDependencies<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
dependencies: S["dependencies"],
resolvedSchema: S,
rootSchema: S,
formData?: T
): S {
let schema = resolvedSchema;
// Process dependencies updating the local schema properties as appropriate.
for (const dependencyKey in dependencies) {
// Skip this dependency if its trigger property is not present.
if (get(formData, [dependencyKey]) === undefined) {
continue;
}
// Skip this dependency if it is not included in the schema (such as when dependencyKey is itself a hidden dependency.)
if (schema.properties && !(dependencyKey in schema.properties)) {
continue;
}
const [remainingDependencies, dependencyValue] = splitKeyElementFromObject(
dependencyKey,
dependencies as GenericObjectType
);
if (Array.isArray(dependencyValue)) {
schema = withDependentProperties(schema, dependencyValue);
} else if (isObject(dependencyValue)) {
schema = withDependentSchema<T, S>(
validator,
schema,
rootSchema,
dependencyKey,
dependencyValue as S,
formData
);
}
return processDependencies<T, S>(
validator,
remainingDependencies,
schema,
rootSchema,
formData
);
}
return schema;
}
/** Updates a schema with additionally required properties added
*
* @param schema - The schema for which resolving a dependent properties is desired
* @param [additionallyRequired] - An optional array of additionally required names
* @returns - The schema with the additional required values merged in
*/
export function withDependentProperties<
S extends StrictRJSFSchema = RJSFSchema
>(schema: S, additionallyRequired?: string[]) {
if (!additionallyRequired) {
return schema;
}
const required = Array.isArray(schema.required)
? Array.from(new Set([...schema.required, ...additionallyRequired]))
: additionallyRequired;
return { ...schema, required: required };
}
/** Merges a dependent schema into the `schema` dealing with oneOfs and references
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a dependent schema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param dependencyKey - The key name of the dependency
* @param dependencyValue - The potentially dependent schema
* @param formData- The current formData to assist retrieving a schema
* @returns - The schema with the dependent schema resolved into it
*/
export function withDependentSchema<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
schema: S,
rootSchema: S,
dependencyKey: string,
dependencyValue: S,
formData?: T
) {
const { oneOf, ...dependentSchema } = retrieveSchema<T, S>(
validator,
dependencyValue,
rootSchema,
formData
);
schema = mergeSchemas(schema, dependentSchema) as S;
// Since it does not contain oneOf, we return the original schema.
if (oneOf === undefined) {
return schema;
}
// Resolve $refs inside oneOf.
const resolvedOneOf = oneOf.map((subschema) => {
if (typeof subschema === "boolean" || !(REF_KEY in subschema)) {
return subschema;
}
return resolveReference<T, S>(
validator,
subschema as S,
rootSchema,
formData
);
});
return withExactlyOneSubschema<T, S>(
validator,
schema,
rootSchema,
dependencyKey,
resolvedOneOf,
formData
);
}
/** Returns a `schema` with the best choice from the `oneOf` options merged into it
*
* @param validator - An implementation of the `ValidatorType<T, S>` interface that will be used to validate oneOf options
* @param schema - The schema for which resolving a oneOf subschema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param dependencyKey - The key name of the oneOf dependency
* @param oneOf - The list of schemas representing the oneOf options
* @param [formData] - The current formData to assist retrieving a schema
* @returns The schema with the best choice of oneOf schemas merged into
*/
export function withExactlyOneSubschema<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
schema: S,
rootSchema: S,
dependencyKey: string,
oneOf: S["oneOf"],
formData?: T
): S {
const validSubschemas = oneOf!.filter((subschema) => {
if (typeof subschema === "boolean" || !subschema || !subschema.properties) {
return false;
}
const { [dependencyKey]: conditionPropertySchema } = subschema.properties;
if (conditionPropertySchema) {
const conditionSchema: S = {
type: "object",
properties: {
[dependencyKey]: conditionPropertySchema,
},
} as S;
const { errors } = validator.validateFormData(formData, conditionSchema);
return errors.length === 0;
}
return false;
});
if (validSubschemas!.length !== 1) {
console.warn(
"ignoring oneOf in dependencies because there isn't exactly one subschema that is valid"
);
return schema;
}
const subschema: S = validSubschemas[0] as S;
const [dependentSubschema] = splitKeyElementFromObject(
dependencyKey,
subschema.properties as GenericObjectType
);
const dependentSchema = { ...subschema, properties: dependentSubschema };
return mergeSchemas(
schema,
retrieveSchema<T, S>(validator, dependentSchema, rootSchema, formData)
) as S;
}