forked from rjsf-team/react-jsonschema-form
/
toPathSchema.ts
87 lines (82 loc) · 2.4 KB
/
toPathSchema.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
import get from "lodash/get";
import set from "lodash/set";
import {
ALL_OF_KEY,
ADDITIONAL_PROPERTIES_KEY,
DEPENDENCIES_KEY,
ITEMS_KEY,
NAME_KEY,
PROPERTIES_KEY,
REF_KEY,
RJSF_ADDITONAL_PROPERTIES_FLAG,
} from "../constants";
import {
PathSchema,
RJSFSchema,
StrictRJSFSchema,
ValidatorType,
} from "../types";
import retrieveSchema from "./retrieveSchema";
/** Generates an `PathSchema` object for the `schema`, recursively
*
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
* @param schema - The schema for which the `PathSchema` is desired
* @param [name=''] - The base name for the schema
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @returns - The `PathSchema` object for the `schema`
*/
export default function toPathSchema<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
schema: S,
name = "",
rootSchema?: S,
formData?: T
): PathSchema<T> {
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
const _schema = retrieveSchema<T, S>(
validator,
schema,
rootSchema,
formData
);
return toPathSchema<T, S>(validator, _schema, name, rootSchema, formData);
}
const pathSchema: PathSchema = {
[NAME_KEY]: name.replace(/^\./, ""),
} as PathSchema;
if (
ADDITIONAL_PROPERTIES_KEY in schema &&
schema[ADDITIONAL_PROPERTIES_KEY] !== false
) {
set(pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
}
if (ITEMS_KEY in schema && Array.isArray(formData)) {
formData.forEach((element, i: number) => {
pathSchema[i] = toPathSchema<T>(
validator,
schema.items as RJSFSchema,
`${name}.${i}`,
rootSchema,
element
);
});
} else if (PROPERTIES_KEY in schema) {
for (const property in schema.properties) {
const field = get(schema, [PROPERTIES_KEY, property]);
pathSchema[property] = toPathSchema<T, S>(
validator,
field,
`${name}.${property}`,
rootSchema,
// It's possible that formData is not an object -- this can happen if an
// array item has just been added, but not populated with data yet
get(formData, [property])
);
}
}
return pathSchema as PathSchema<T>;
}