@rjsf/utils 5.11.2 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2544 -5
- package/dist/index.js.map +7 -0
- package/dist/utils.esm.js +1228 -2113
- package/dist/utils.esm.js.map +7 -1
- package/dist/utils.umd.js +2414 -0
- package/lib/ErrorSchemaBuilder.d.ts +60 -0
- package/lib/ErrorSchemaBuilder.js +103 -0
- package/lib/ErrorSchemaBuilder.js.map +1 -0
- package/lib/allowAdditionalItems.d.ts +8 -0
- package/lib/allowAdditionalItems.js +14 -0
- package/lib/allowAdditionalItems.js.map +1 -0
- package/lib/asNumber.d.ts +10 -0
- package/lib/asNumber.js +36 -0
- package/lib/asNumber.js.map +1 -0
- package/lib/canExpand.d.ts +11 -0
- package/lib/canExpand.js +26 -0
- package/lib/canExpand.js.map +1 -0
- package/lib/constants.d.ts +31 -0
- package/lib/constants.js +32 -0
- package/lib/constants.js.map +1 -0
- package/lib/createErrorHandler.d.ts +7 -0
- package/lib/createErrorHandler.js +31 -0
- package/lib/createErrorHandler.js.map +1 -0
- package/lib/createSchemaUtils.d.ts +10 -0
- package/lib/createSchemaUtils.js +207 -0
- package/lib/createSchemaUtils.js.map +1 -0
- package/lib/dataURItoBlob.d.ts +16 -0
- package/lib/dataURItoBlob.js +43 -0
- package/lib/dataURItoBlob.js.map +1 -0
- package/lib/deepEquals.d.ts +8 -0
- package/lib/deepEquals.js +19 -0
- package/lib/deepEquals.js.map +1 -0
- package/lib/englishStringTranslator.d.ts +10 -0
- package/lib/englishStringTranslator.js +13 -0
- package/lib/englishStringTranslator.js.map +1 -0
- package/lib/enumOptionsDeselectValue.d.ts +14 -0
- package/lib/enumOptionsDeselectValue.js +22 -0
- package/lib/enumOptionsDeselectValue.js.map +1 -0
- package/lib/enumOptionsIndexForValue.d.ts +13 -0
- package/lib/enumOptionsIndexForValue.js +22 -0
- package/lib/enumOptionsIndexForValue.js.map +1 -0
- package/lib/enumOptionsIsSelected.d.ts +8 -0
- package/lib/enumOptionsIsSelected.js +14 -0
- package/lib/enumOptionsIsSelected.js.map +1 -0
- package/lib/enumOptionsSelectValue.d.ts +10 -0
- package/lib/enumOptionsSelectValue.js +23 -0
- package/lib/enumOptionsSelectValue.js.map +1 -0
- package/lib/enumOptionsValueForIndex.d.ts +13 -0
- package/lib/enumOptionsValueForIndex.js +21 -0
- package/lib/enumOptionsValueForIndex.js.map +1 -0
- package/lib/enums.d.ts +72 -0
- package/lib/enums.js +76 -0
- package/lib/enums.js.map +1 -0
- package/lib/findSchemaDefinition.d.ts +20 -0
- package/lib/findSchemaDefinition.js +49 -0
- package/lib/findSchemaDefinition.js.map +1 -0
- package/lib/getDiscriminatorFieldFromSchema.d.ts +8 -0
- package/lib/getDiscriminatorFieldFromSchema.js +20 -0
- package/lib/getDiscriminatorFieldFromSchema.js.map +1 -0
- package/lib/getInputProps.d.ts +10 -0
- package/lib/getInputProps.js +41 -0
- package/lib/getInputProps.js.map +1 -0
- package/lib/getSchemaType.d.ts +13 -0
- package/lib/getSchemaType.js +29 -0
- package/lib/getSchemaType.js.map +1 -0
- package/lib/getSubmitButtonOptions.d.ts +10 -0
- package/lib/getSubmitButtonOptions.js +25 -0
- package/lib/getSubmitButtonOptions.js.map +1 -0
- package/lib/getTemplate.d.ts +10 -0
- package/lib/getTemplate.js +19 -0
- package/lib/getTemplate.js.map +1 -0
- package/lib/getUiOptions.d.ts +9 -0
- package/lib/getUiOptions.js +25 -0
- package/lib/getUiOptions.js.map +1 -0
- package/lib/getWidget.d.ts +13 -0
- package/lib/getWidget.js +118 -0
- package/lib/getWidget.js.map +1 -0
- package/lib/guessType.d.ts +7 -0
- package/lib/guessType.js +29 -0
- package/lib/guessType.js.map +1 -0
- package/lib/hasWidget.d.ts +10 -0
- package/lib/hasWidget.js +23 -0
- package/lib/hasWidget.js.map +1 -0
- package/lib/hashForSchema.d.ts +8 -0
- package/lib/hashForSchema.js +29 -0
- package/lib/hashForSchema.js.map +1 -0
- package/lib/idGenerators.d.ts +47 -0
- package/lib/idGenerators.js +73 -0
- package/lib/idGenerators.js.map +1 -0
- package/lib/index.d.ts +57 -0
- package/lib/index.js +58 -0
- package/lib/index.js.map +1 -0
- package/lib/isConstant.d.ts +8 -0
- package/lib/isConstant.js +11 -0
- package/lib/isConstant.js.map +1 -0
- package/lib/isCustomWidget.d.ts +7 -0
- package/lib/isCustomWidget.js +13 -0
- package/lib/isCustomWidget.js.map +1 -0
- package/lib/isFixedItems.d.ts +8 -0
- package/lib/isFixedItems.js +11 -0
- package/lib/isFixedItems.js.map +1 -0
- package/lib/isObject.d.ts +7 -0
- package/lib/isObject.js +16 -0
- package/lib/isObject.js.map +1 -0
- package/lib/labelValue.d.ts +13 -0
- package/lib/labelValue.js +4 -0
- package/lib/labelValue.js.map +1 -0
- package/lib/localToUTC.d.ts +6 -0
- package/lib/localToUTC.js +9 -0
- package/lib/localToUTC.js.map +1 -0
- package/lib/mergeDefaultsWithFormData.d.ts +17 -0
- package/lib/mergeDefaultsWithFormData.js +43 -0
- package/lib/mergeDefaultsWithFormData.js.map +1 -0
- package/lib/mergeObjects.d.ts +11 -0
- package/lib/mergeObjects.js +35 -0
- package/lib/mergeObjects.js.map +1 -0
- package/lib/mergeSchemas.d.ts +10 -0
- package/lib/mergeSchemas.js +35 -0
- package/lib/mergeSchemas.js.map +1 -0
- package/lib/optionsList.d.ts +10 -0
- package/lib/optionsList.js +36 -0
- package/lib/optionsList.js.map +1 -0
- package/lib/orderProperties.d.ts +11 -0
- package/lib/orderProperties.js +38 -0
- package/lib/orderProperties.js.map +1 -0
- package/lib/pad.d.ts +7 -0
- package/lib/pad.js +14 -0
- package/lib/pad.js.map +1 -0
- package/lib/parseDateString.d.ts +9 -0
- package/lib/parseDateString.js +32 -0
- package/lib/parseDateString.js.map +1 -0
- package/lib/parser/ParserValidator.d.ts +70 -0
- package/lib/parser/ParserValidator.js +93 -0
- package/lib/parser/ParserValidator.js.map +1 -0
- package/lib/parser/index.d.ts +4 -0
- package/lib/parser/index.js +3 -0
- package/lib/parser/index.js.map +1 -0
- package/lib/parser/schemaParser.d.ts +9 -0
- package/lib/parser/schemaParser.js +48 -0
- package/lib/parser/schemaParser.js.map +1 -0
- package/lib/rangeSpec.d.ts +9 -0
- package/lib/rangeSpec.js +20 -0
- package/lib/rangeSpec.js.map +1 -0
- package/lib/replaceStringParameters.d.ts +9 -0
- package/lib/replaceStringParameters.js +23 -0
- package/lib/replaceStringParameters.js.map +1 -0
- package/lib/schema/getClosestMatchingOption.d.ts +49 -0
- package/lib/schema/getClosestMatchingOption.js +154 -0
- package/lib/schema/getClosestMatchingOption.js.map +1 -0
- package/lib/schema/getDefaultFormState.d.ts +66 -0
- package/lib/schema/getDefaultFormState.js +351 -0
- package/lib/schema/getDefaultFormState.js.map +1 -0
- package/lib/schema/getDisplayLabel.d.ts +12 -0
- package/lib/schema/getDisplayLabel.js +39 -0
- package/lib/schema/getDisplayLabel.js.map +1 -0
- package/lib/schema/getFirstMatchingOption.d.ts +13 -0
- package/lib/schema/getFirstMatchingOption.js +16 -0
- package/lib/schema/getFirstMatchingOption.js.map +1 -0
- package/lib/schema/getMatchingOption.d.ts +14 -0
- package/lib/schema/getMatchingOption.js +80 -0
- package/lib/schema/getMatchingOption.js.map +1 -0
- package/lib/schema/index.d.ts +14 -0
- package/lib/schema/index.js +15 -0
- package/lib/schema/index.js.map +1 -0
- package/lib/schema/isFilesArray.d.ts +10 -0
- package/lib/schema/isFilesArray.js +21 -0
- package/lib/schema/isFilesArray.js.map +1 -0
- package/lib/schema/isMultiSelect.d.ts +9 -0
- package/lib/schema/isMultiSelect.js +15 -0
- package/lib/schema/isMultiSelect.js.map +1 -0
- package/lib/schema/isSelect.d.ts +9 -0
- package/lib/schema/isSelect.js +21 -0
- package/lib/schema/isSelect.js.map +1 -0
- package/lib/schema/mergeValidationData.d.ts +14 -0
- package/lib/schema/mergeValidationData.js +28 -0
- package/lib/schema/mergeValidationData.js.map +1 -0
- package/lib/schema/retrieveSchema.d.ts +170 -0
- package/lib/schema/retrieveSchema.js +437 -0
- package/lib/schema/retrieveSchema.js.map +1 -0
- package/lib/schema/sanitizeDataForNewSchema.d.ts +49 -0
- package/lib/schema/sanitizeDataForNewSchema.js +173 -0
- package/lib/schema/sanitizeDataForNewSchema.js.map +1 -0
- package/lib/schema/toIdSchema.d.ts +13 -0
- package/lib/schema/toIdSchema.js +59 -0
- package/lib/schema/toIdSchema.js.map +1 -0
- package/lib/schema/toPathSchema.d.ts +11 -0
- package/lib/schema/toPathSchema.js +68 -0
- package/lib/schema/toPathSchema.js.map +1 -0
- package/lib/schemaRequiresTrueValue.d.ts +11 -0
- package/lib/schemaRequiresTrueValue.js +34 -0
- package/lib/schemaRequiresTrueValue.js.map +1 -0
- package/lib/shouldRender.d.ts +10 -0
- package/lib/shouldRender.js +14 -0
- package/lib/shouldRender.js.map +1 -0
- package/lib/toConstant.d.ts +9 -0
- package/lib/toConstant.js +18 -0
- package/lib/toConstant.js.map +1 -0
- package/lib/toDateString.d.ts +9 -0
- package/lib/toDateString.js +14 -0
- package/lib/toDateString.js.map +1 -0
- package/lib/toErrorList.d.ts +8 -0
- package/lib/toErrorList.js +34 -0
- package/lib/toErrorList.js.map +1 -0
- package/lib/toErrorSchema.d.ts +21 -0
- package/lib/toErrorSchema.js +41 -0
- package/lib/toErrorSchema.js.map +1 -0
- package/lib/types.d.ts +982 -0
- package/lib/types.js +2 -0
- package/lib/types.js.map +1 -0
- package/lib/unwrapErrorHandler.d.ts +7 -0
- package/lib/unwrapErrorHandler.js +21 -0
- package/lib/unwrapErrorHandler.js.map +1 -0
- package/lib/utcToLocal.d.ts +6 -0
- package/lib/utcToLocal.js +26 -0
- package/lib/utcToLocal.js.map +1 -0
- package/lib/validationDataMerge.d.ts +11 -0
- package/lib/validationDataMerge.js +26 -0
- package/lib/validationDataMerge.js.map +1 -0
- package/lib/withIdRefPrefix.d.ts +8 -0
- package/lib/withIdRefPrefix.js +47 -0
- package/lib/withIdRefPrefix.js.map +1 -0
- package/package.json +20 -13
- package/src/ErrorSchemaBuilder.ts +112 -0
- package/src/allowAdditionalItems.ts +15 -0
- package/src/asNumber.ts +38 -0
- package/src/canExpand.ts +31 -0
- package/src/constants.ts +31 -0
- package/src/createErrorHandler.ts +33 -0
- package/src/createSchemaUtils.ts +298 -0
- package/src/dataURItoBlob.ts +42 -0
- package/src/deepEquals.ts +19 -0
- package/src/englishStringTranslator.ts +14 -0
- package/src/enumOptionsDeselectValue.ts +28 -0
- package/src/enumOptionsIndexForValue.ts +27 -0
- package/src/enumOptionsIsSelected.ts +19 -0
- package/src/enumOptionsSelectValue.ts +28 -0
- package/src/enumOptionsValueForIndex.ts +26 -0
- package/src/enums.ts +74 -0
- package/src/findSchemaDefinition.ts +54 -0
- package/src/getDiscriminatorFieldFromSchema.ts +21 -0
- package/src/getInputProps.ts +55 -0
- package/src/getSchemaType.ts +37 -0
- package/src/getSubmitButtonOptions.ts +32 -0
- package/src/getTemplate.ts +26 -0
- package/src/getUiOptions.ts +32 -0
- package/src/getWidget.tsx +133 -0
- package/src/guessType.ts +28 -0
- package/src/hasWidget.ts +27 -0
- package/src/hashForSchema.ts +31 -0
- package/src/idGenerators.ts +81 -0
- package/src/index.ts +118 -0
- package/src/isConstant.ts +12 -0
- package/src/isCustomWidget.ts +19 -0
- package/src/isFixedItems.ts +12 -0
- package/src/isObject.ts +15 -0
- package/src/labelValue.ts +16 -0
- package/src/localToUTC.ts +8 -0
- package/src/mergeDefaultsWithFormData.ts +53 -0
- package/src/mergeObjects.ts +39 -0
- package/src/mergeSchemas.ts +38 -0
- package/src/optionsList.ts +41 -0
- package/src/orderProperties.ts +44 -0
- package/src/pad.ts +13 -0
- package/src/parseDateString.ts +33 -0
- package/src/parser/ParserValidator.ts +132 -0
- package/src/parser/index.ts +6 -0
- package/src/parser/schemaParser.ts +60 -0
- package/src/rangeSpec.ts +22 -0
- package/src/replaceStringParameters.ts +22 -0
- package/src/schema/getClosestMatchingOption.ts +191 -0
- package/src/schema/getDefaultFormState.ts +447 -0
- package/src/schema/getDisplayLabel.ts +59 -0
- package/src/schema/getFirstMatchingOption.ts +27 -0
- package/src/schema/getMatchingOption.ts +95 -0
- package/src/schema/index.ts +29 -0
- package/src/schema/isFilesArray.ts +27 -0
- package/src/schema/isMultiSelect.ts +21 -0
- package/src/schema/isSelect.ts +26 -0
- package/src/schema/mergeValidationData.ts +38 -0
- package/src/schema/retrieveSchema.ts +614 -0
- package/src/schema/sanitizeDataForNewSchema.ts +197 -0
- package/src/schema/toIdSchema.ts +105 -0
- package/src/schema/toPathSchema.ts +121 -0
- package/src/schemaRequiresTrueValue.ts +40 -0
- package/src/shouldRender.ts +16 -0
- package/src/toConstant.ts +19 -0
- package/src/toDateString.ts +15 -0
- package/src/toErrorList.ts +41 -0
- package/src/toErrorSchema.ts +43 -0
- package/src/types.ts +1139 -0
- package/src/unwrapErrorHandler.ts +25 -0
- package/src/utcToLocal.ts +30 -0
- package/src/validationDataMerge.ts +31 -0
- package/src/withIdRefPrefix.ts +49 -0
- package/dist/index.d.ts +0 -1911
- package/dist/utils.cjs.development.js +0 -3522
- package/dist/utils.cjs.development.js.map +0 -1
- package/dist/utils.cjs.production.min.js +0 -2
- package/dist/utils.cjs.production.min.js.map +0 -1
- package/dist/utils.umd.development.js +0 -3504
- package/dist/utils.umd.development.js.map +0 -1
- package/dist/utils.umd.production.min.js +0 -2
- package/dist/utils.umd.production.min.js.map +0 -1
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import set from 'lodash/set';
|
|
3
|
+
import times from 'lodash/times';
|
|
4
|
+
import transform from 'lodash/transform';
|
|
5
|
+
import mergeAllOf, { Options } from 'json-schema-merge-allof';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
ADDITIONAL_PROPERTIES_KEY,
|
|
9
|
+
ADDITIONAL_PROPERTY_FLAG,
|
|
10
|
+
ALL_OF_KEY,
|
|
11
|
+
ANY_OF_KEY,
|
|
12
|
+
DEPENDENCIES_KEY,
|
|
13
|
+
IF_KEY,
|
|
14
|
+
ONE_OF_KEY,
|
|
15
|
+
REF_KEY,
|
|
16
|
+
PROPERTIES_KEY,
|
|
17
|
+
ITEMS_KEY,
|
|
18
|
+
} from '../constants';
|
|
19
|
+
import findSchemaDefinition, { splitKeyElementFromObject } from '../findSchemaDefinition';
|
|
20
|
+
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema';
|
|
21
|
+
import guessType from '../guessType';
|
|
22
|
+
import isObject from '../isObject';
|
|
23
|
+
import mergeSchemas from '../mergeSchemas';
|
|
24
|
+
import { FormContextType, GenericObjectType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
25
|
+
import getFirstMatchingOption from './getFirstMatchingOption';
|
|
26
|
+
|
|
27
|
+
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
|
|
28
|
+
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
|
|
29
|
+
* potentially recursive resolution.
|
|
30
|
+
*
|
|
31
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
32
|
+
* @param schema - The schema for which retrieving a schema is desired
|
|
33
|
+
* @param [rootSchema={}] - The root schema that will be forwarded to all the APIs
|
|
34
|
+
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
35
|
+
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
|
|
36
|
+
*/
|
|
37
|
+
export default function retrieveSchema<
|
|
38
|
+
T = any,
|
|
39
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
40
|
+
F extends FormContextType = any
|
|
41
|
+
>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S = {} as S, rawFormData?: T): S {
|
|
42
|
+
return retrieveSchemaInternal<T, S, F>(validator, schema, rootSchema, rawFormData)[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Resolves a conditional block (if/else/then) by removing the condition and merging the appropriate conditional branch
|
|
46
|
+
* with the rest of the schema. If `expandAllBranches` is true, then the `retrieveSchemaInteral()` results for both
|
|
47
|
+
* conditions will be returned.
|
|
48
|
+
*
|
|
49
|
+
* @param validator - An implementation of the `ValidatorType` interface that is used to detect valid schema conditions
|
|
50
|
+
* @param schema - The schema for which resolving a condition is desired
|
|
51
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
52
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and
|
|
53
|
+
* dependencies as a list of schemas
|
|
54
|
+
* @param [formData] - The current formData to assist retrieving a schema
|
|
55
|
+
* @returns - A list of schemas with the appropriate conditions resolved, possibly with all branches expanded
|
|
56
|
+
*/
|
|
57
|
+
export function resolveCondition<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
58
|
+
validator: ValidatorType<T, S, F>,
|
|
59
|
+
schema: S,
|
|
60
|
+
rootSchema: S,
|
|
61
|
+
expandAllBranches: boolean,
|
|
62
|
+
formData?: T
|
|
63
|
+
): S[] {
|
|
64
|
+
const { if: expression, then, else: otherwise, ...resolvedSchemaLessConditional } = schema;
|
|
65
|
+
|
|
66
|
+
const conditionValue = validator.isValid(expression as S, formData || ({} as T), rootSchema);
|
|
67
|
+
let resolvedSchemas = [resolvedSchemaLessConditional as S];
|
|
68
|
+
let schemas: S[] = [];
|
|
69
|
+
if (expandAllBranches) {
|
|
70
|
+
if (then && typeof then !== 'boolean') {
|
|
71
|
+
schemas = schemas.concat(
|
|
72
|
+
retrieveSchemaInternal<T, S, F>(validator, then as S, rootSchema, formData, expandAllBranches)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (otherwise && typeof otherwise !== 'boolean') {
|
|
76
|
+
schemas = schemas.concat(
|
|
77
|
+
retrieveSchemaInternal<T, S, F>(validator, otherwise as S, rootSchema, formData, expandAllBranches)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
const conditionalSchema = conditionValue ? then : otherwise;
|
|
82
|
+
if (conditionalSchema && typeof conditionalSchema !== 'boolean') {
|
|
83
|
+
schemas = schemas.concat(
|
|
84
|
+
retrieveSchemaInternal<T, S, F>(validator, conditionalSchema as S, rootSchema, formData, expandAllBranches)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (schemas.length) {
|
|
89
|
+
resolvedSchemas = schemas.map((s) => mergeSchemas(resolvedSchemaLessConditional, s) as S);
|
|
90
|
+
}
|
|
91
|
+
return resolvedSchemas.flatMap((s) =>
|
|
92
|
+
retrieveSchemaInternal<T, S, F>(validator, s, rootSchema, formData, expandAllBranches)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Given a list of lists of allOf, anyOf or oneOf values, create a list of lists of all permutations of the values. The
|
|
97
|
+
* `listOfLists` is expected to be all resolved values of the 1st...nth schemas within an `allOf`, `anyOf` or `oneOf`.
|
|
98
|
+
* From those lists, build a matrix for each `xxxOf` where there is more than one schema for a row in the list of lists.
|
|
99
|
+
*
|
|
100
|
+
* For example:
|
|
101
|
+
* - If there are three xxxOf rows (A, B, C) and they have been resolved such that there is only one A, two B and three
|
|
102
|
+
* C schemas then:
|
|
103
|
+
* - The permutation for the first row is `[[A]]`
|
|
104
|
+
* - The permutations for the second row are `[[A,B1], [A,B2]]`
|
|
105
|
+
* - The permutations for the third row are `[[A,B1,C1], [A,B1,C2], [A,B1,C3], [A,B2,C1], [A,B2,C2], [A,B2,C3]]`
|
|
106
|
+
*
|
|
107
|
+
* @param listOfLists - The list of lists of elements that represent the allOf, anyOf or oneOf resolved values in order
|
|
108
|
+
* @returns - The list of all permutations of schemas for a set of `xxxOf`s
|
|
109
|
+
*/
|
|
110
|
+
export function getAllPermutationsOfXxxOf<S extends StrictRJSFSchema = RJSFSchema>(listOfLists: S[][]) {
|
|
111
|
+
const allPermutations: S[][] = listOfLists.reduce<S[][]>(
|
|
112
|
+
(permutations, list) => {
|
|
113
|
+
// When there are more than one set of schemas for a row, duplicate the set of permutations and add in the values
|
|
114
|
+
if (list.length > 1) {
|
|
115
|
+
return list.flatMap((element) => times(permutations.length, (i) => [...permutations[i]].concat(element)));
|
|
116
|
+
}
|
|
117
|
+
// Otherwise just push in the single value into the current set of permutations
|
|
118
|
+
permutations.forEach((permutation) => permutation.push(list[0]));
|
|
119
|
+
return permutations;
|
|
120
|
+
},
|
|
121
|
+
[[]] as S[][] // Start with an empty list
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return allPermutations;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Resolves references and dependencies within a schema and its 'allOf' children. Passes the `expandAllBranches` flag
|
|
128
|
+
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `resolveDependencies()` helper calls. If
|
|
129
|
+
* `expandAllBranches` is true, then all possible dependencies and/or allOf branches are returned.
|
|
130
|
+
*
|
|
131
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
132
|
+
* @param schema - The schema for which resolving a schema is desired
|
|
133
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
134
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
135
|
+
* as a list of schemas
|
|
136
|
+
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
137
|
+
* @returns - The list of schemas having its references, dependencies and allOf schemas resolved
|
|
138
|
+
*/
|
|
139
|
+
export function resolveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
140
|
+
validator: ValidatorType<T, S, F>,
|
|
141
|
+
schema: S,
|
|
142
|
+
rootSchema: S,
|
|
143
|
+
expandAllBranches: boolean,
|
|
144
|
+
formData?: T
|
|
145
|
+
): S[] {
|
|
146
|
+
if (REF_KEY in schema) {
|
|
147
|
+
return resolveReference<T, S, F>(validator, schema, rootSchema, expandAllBranches, formData);
|
|
148
|
+
}
|
|
149
|
+
if (DEPENDENCIES_KEY in schema) {
|
|
150
|
+
const resolvedSchemas = resolveDependencies<T, S, F>(validator, schema, rootSchema, expandAllBranches, formData);
|
|
151
|
+
return resolvedSchemas.flatMap((s) => {
|
|
152
|
+
return retrieveSchemaInternal<T, S, F>(validator, s, rootSchema, formData, expandAllBranches);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
if (ALL_OF_KEY in schema && Array.isArray(schema.allOf)) {
|
|
156
|
+
const allOfSchemaElements: S[][] = schema.allOf.map((allOfSubschema) =>
|
|
157
|
+
retrieveSchemaInternal<T, S, F>(validator, allOfSubschema as S, rootSchema, formData, expandAllBranches)
|
|
158
|
+
);
|
|
159
|
+
const allPermutations = getAllPermutationsOfXxxOf<S>(allOfSchemaElements);
|
|
160
|
+
return allPermutations.map((permutation) => ({ ...schema, allOf: permutation }));
|
|
161
|
+
}
|
|
162
|
+
// No $ref or dependencies or allOf attribute was found, returning the original schema.
|
|
163
|
+
return [schema];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Resolves references within a schema and then returns the `retrieveSchemaInternal()` of the resolved schema. Passes
|
|
167
|
+
* the `expandAllBranches` flag down to the `retrieveSchemaInternal()` helper call.
|
|
168
|
+
*
|
|
169
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
170
|
+
* @param schema - The schema for which resolving a reference is desired
|
|
171
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
172
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
173
|
+
* as a list of schemas
|
|
174
|
+
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
175
|
+
* @returns - The list schemas retrieved after having all references resolved
|
|
176
|
+
*/
|
|
177
|
+
export function resolveReference<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
178
|
+
validator: ValidatorType<T, S, F>,
|
|
179
|
+
schema: S,
|
|
180
|
+
rootSchema: S,
|
|
181
|
+
expandAllBranches: boolean,
|
|
182
|
+
formData?: T
|
|
183
|
+
): S[] {
|
|
184
|
+
// Drop the $ref property of the source schema.
|
|
185
|
+
const { $ref, ...localSchema } = schema;
|
|
186
|
+
// Retrieve the referenced schema definition.
|
|
187
|
+
const refSchema = findSchemaDefinition<S>($ref, rootSchema);
|
|
188
|
+
// Update referenced schema definition with local schema properties.
|
|
189
|
+
return retrieveSchemaInternal<T, S, F>(
|
|
190
|
+
validator,
|
|
191
|
+
{ ...refSchema, ...localSchema },
|
|
192
|
+
rootSchema,
|
|
193
|
+
formData,
|
|
194
|
+
expandAllBranches
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Resolves all references within a schema's properties and array items.
|
|
199
|
+
*
|
|
200
|
+
* @param schema - The schema for which resolving all references is desired
|
|
201
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
202
|
+
* @returns - given schema will all references resolved
|
|
203
|
+
*/
|
|
204
|
+
export function resolveAllReferences<S extends StrictRJSFSchema = RJSFSchema>(schema: S, rootSchema: S): S {
|
|
205
|
+
let resolvedSchema: S = schema;
|
|
206
|
+
// resolve top level ref
|
|
207
|
+
if (REF_KEY in resolvedSchema) {
|
|
208
|
+
const { $ref, ...localSchema } = resolvedSchema;
|
|
209
|
+
// Retrieve the referenced schema definition.
|
|
210
|
+
const refSchema = findSchemaDefinition<S>($ref, rootSchema);
|
|
211
|
+
resolvedSchema = { ...refSchema, ...localSchema };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (PROPERTIES_KEY in resolvedSchema) {
|
|
215
|
+
const updatedProps = transform(
|
|
216
|
+
resolvedSchema[PROPERTIES_KEY]!,
|
|
217
|
+
(result, value, key: string) => {
|
|
218
|
+
result[key] = resolveAllReferences(value as S, rootSchema);
|
|
219
|
+
},
|
|
220
|
+
{} as RJSFSchema
|
|
221
|
+
);
|
|
222
|
+
resolvedSchema = { ...resolvedSchema, [PROPERTIES_KEY]: updatedProps };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (
|
|
226
|
+
ITEMS_KEY in resolvedSchema &&
|
|
227
|
+
!Array.isArray(resolvedSchema.items) &&
|
|
228
|
+
typeof resolvedSchema.items !== 'boolean'
|
|
229
|
+
) {
|
|
230
|
+
resolvedSchema = { ...resolvedSchema, items: resolveAllReferences(resolvedSchema.items as S, rootSchema) };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return resolvedSchema;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Creates new 'properties' items for each key in the `formData`
|
|
237
|
+
*
|
|
238
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
239
|
+
* @param theSchema - The schema for which the existing additional properties is desired
|
|
240
|
+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param validator
|
|
241
|
+
* @param [aFormData] - The current formData, if any, to assist retrieving a schema
|
|
242
|
+
* @returns - The updated schema with additional properties stubbed
|
|
243
|
+
*/
|
|
244
|
+
export function stubExistingAdditionalProperties<
|
|
245
|
+
T = any,
|
|
246
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
247
|
+
F extends FormContextType = any
|
|
248
|
+
>(validator: ValidatorType<T, S, F>, theSchema: S, rootSchema?: S, aFormData?: T): S {
|
|
249
|
+
// Clone the schema so that we don't ruin the consumer's original
|
|
250
|
+
const schema = {
|
|
251
|
+
...theSchema,
|
|
252
|
+
properties: { ...theSchema.properties },
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// make sure formData is an object
|
|
256
|
+
const formData: GenericObjectType = aFormData && isObject(aFormData) ? aFormData : {};
|
|
257
|
+
Object.keys(formData).forEach((key) => {
|
|
258
|
+
if (key in schema.properties) {
|
|
259
|
+
// No need to stub, our schema already has the property
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let additionalProperties: S['additionalProperties'] = {};
|
|
264
|
+
if (typeof schema.additionalProperties !== 'boolean') {
|
|
265
|
+
if (REF_KEY in schema.additionalProperties!) {
|
|
266
|
+
additionalProperties = retrieveSchema<T, S, F>(
|
|
267
|
+
validator,
|
|
268
|
+
{ $ref: get(schema.additionalProperties, [REF_KEY]) } as S,
|
|
269
|
+
rootSchema,
|
|
270
|
+
formData as T
|
|
271
|
+
);
|
|
272
|
+
} else if ('type' in schema.additionalProperties!) {
|
|
273
|
+
additionalProperties = { ...schema.additionalProperties };
|
|
274
|
+
} else if (ANY_OF_KEY in schema.additionalProperties! || ONE_OF_KEY in schema.additionalProperties!) {
|
|
275
|
+
additionalProperties = {
|
|
276
|
+
type: 'object',
|
|
277
|
+
...schema.additionalProperties,
|
|
278
|
+
};
|
|
279
|
+
} else {
|
|
280
|
+
additionalProperties = { type: guessType(get(formData, [key])) };
|
|
281
|
+
}
|
|
282
|
+
} else {
|
|
283
|
+
additionalProperties = { type: guessType(get(formData, [key])) };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// The type of our new key should match the additionalProperties value;
|
|
287
|
+
schema.properties[key] = additionalProperties;
|
|
288
|
+
// Set our additional property flag so we know it was dynamically added
|
|
289
|
+
set(schema.properties, [key, ADDITIONAL_PROPERTY_FLAG], true);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
return schema;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Internal handler that retrieves an expanded schema that has had all of its conditions, additional properties,
|
|
296
|
+
* references and dependencies resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData`
|
|
297
|
+
* that is used to do the potentially recursive resolution. If `expandAllBranches` is true, then all possible branches
|
|
298
|
+
* of the schema and its references, conditions and dependencies are returned.
|
|
299
|
+
*
|
|
300
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
301
|
+
* @param schema - The schema for which retrieving a schema is desired
|
|
302
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
303
|
+
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
304
|
+
* @param [expandAllBranches=false] - Flag, if true, will return all possible branches of conditions, any/oneOf and
|
|
305
|
+
* dependencies as a list of schemas
|
|
306
|
+
* @returns - The schema(s) resulting from having its conditions, additional properties, references and dependencies
|
|
307
|
+
* resolved. Multiple schemas may be returned if `expandAllBranches` is true.
|
|
308
|
+
*/
|
|
309
|
+
export function retrieveSchemaInternal<
|
|
310
|
+
T = any,
|
|
311
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
312
|
+
F extends FormContextType = any
|
|
313
|
+
>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, rawFormData?: T, expandAllBranches = false): S[] {
|
|
314
|
+
if (!isObject(schema)) {
|
|
315
|
+
return [{} as S];
|
|
316
|
+
}
|
|
317
|
+
const resolvedSchemas = resolveSchema<T, S, F>(validator, schema, rootSchema, expandAllBranches, rawFormData);
|
|
318
|
+
return resolvedSchemas.flatMap((s: S) => {
|
|
319
|
+
let resolvedSchema = s;
|
|
320
|
+
if (IF_KEY in resolvedSchema) {
|
|
321
|
+
return resolveCondition<T, S, F>(validator, resolvedSchema, rootSchema, expandAllBranches, rawFormData as T);
|
|
322
|
+
}
|
|
323
|
+
if (ALL_OF_KEY in resolvedSchema) {
|
|
324
|
+
// resolve allOf schemas
|
|
325
|
+
if (expandAllBranches) {
|
|
326
|
+
return [...(resolvedSchema.allOf as S[])];
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
resolvedSchema = mergeAllOf(resolvedSchema, {
|
|
330
|
+
deep: false,
|
|
331
|
+
} as Options) as S;
|
|
332
|
+
} catch (e) {
|
|
333
|
+
console.warn('could not merge subschemas in allOf:\n', e);
|
|
334
|
+
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
|
|
335
|
+
return resolvedSchemaWithoutAllOf as S;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const hasAdditionalProperties =
|
|
339
|
+
ADDITIONAL_PROPERTIES_KEY in resolvedSchema && resolvedSchema.additionalProperties !== false;
|
|
340
|
+
if (hasAdditionalProperties) {
|
|
341
|
+
return stubExistingAdditionalProperties<T, S, F>(validator, resolvedSchema, rootSchema, rawFormData as T);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return resolvedSchema;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Resolves an `anyOf` or `oneOf` within a schema (if present) to the list of schemas returned from
|
|
349
|
+
* `retrieveSchemaInternal()` for the best matching option. If `expandAllBranches` is true, then a list of schemas for ALL
|
|
350
|
+
* options are retrieved and returned.
|
|
351
|
+
*
|
|
352
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
353
|
+
* @param schema - The schema for which retrieving a schema is desired
|
|
354
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
355
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
356
|
+
* as a list of schemas
|
|
357
|
+
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema, defaults to an empty object
|
|
358
|
+
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
359
|
+
*/
|
|
360
|
+
export function resolveAnyOrOneOfSchemas<
|
|
361
|
+
T = any,
|
|
362
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
363
|
+
F extends FormContextType = any
|
|
364
|
+
>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, rawFormData?: T) {
|
|
365
|
+
let anyOrOneOf: S[] | undefined;
|
|
366
|
+
const { oneOf, anyOf, ...remaining } = schema;
|
|
367
|
+
if (Array.isArray(oneOf)) {
|
|
368
|
+
anyOrOneOf = oneOf as S[];
|
|
369
|
+
} else if (Array.isArray(anyOf)) {
|
|
370
|
+
anyOrOneOf = anyOf as S[];
|
|
371
|
+
}
|
|
372
|
+
if (anyOrOneOf) {
|
|
373
|
+
// Ensure that during expand all branches we pass an object rather than undefined so that all options are interrogated
|
|
374
|
+
const formData = rawFormData === undefined && expandAllBranches ? ({} as T) : rawFormData;
|
|
375
|
+
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
|
|
376
|
+
anyOrOneOf = anyOrOneOf.map((s) => {
|
|
377
|
+
return resolveAllReferences(s, rootSchema);
|
|
378
|
+
});
|
|
379
|
+
// Call this to trigger the set of isValid() calls that the schema parser will need
|
|
380
|
+
const option = getFirstMatchingOption<T, S, F>(validator, formData, anyOrOneOf, rootSchema, discriminator);
|
|
381
|
+
if (expandAllBranches) {
|
|
382
|
+
return anyOrOneOf.map((item) => mergeSchemas(remaining, item) as S);
|
|
383
|
+
}
|
|
384
|
+
schema = mergeSchemas(remaining, anyOrOneOf[option]) as S;
|
|
385
|
+
}
|
|
386
|
+
return [schema];
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Resolves dependencies within a schema and its 'anyOf/oneOf' children. Passes the `expandAllBranches` flag down to
|
|
390
|
+
* the `resolveAnyOrOneOfSchema()` and `processDependencies()` helper calls.
|
|
391
|
+
*
|
|
392
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
393
|
+
* @param schema - The schema for which resolving a dependency is desired
|
|
394
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
395
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
396
|
+
* as a list of schemas
|
|
397
|
+
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
398
|
+
* @returns - The list of schemas with their dependencies resolved
|
|
399
|
+
*/
|
|
400
|
+
export function resolveDependencies<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
401
|
+
validator: ValidatorType<T, S, F>,
|
|
402
|
+
schema: S,
|
|
403
|
+
rootSchema: S,
|
|
404
|
+
expandAllBranches: boolean,
|
|
405
|
+
formData?: T
|
|
406
|
+
): S[] {
|
|
407
|
+
// Drop the dependencies from the source schema.
|
|
408
|
+
const { dependencies, ...remainingSchema } = schema;
|
|
409
|
+
const resolvedSchemas = resolveAnyOrOneOfSchemas<T, S, F>(
|
|
410
|
+
validator,
|
|
411
|
+
remainingSchema as S,
|
|
412
|
+
rootSchema,
|
|
413
|
+
expandAllBranches,
|
|
414
|
+
formData
|
|
415
|
+
);
|
|
416
|
+
return resolvedSchemas.flatMap((resolvedSchema) =>
|
|
417
|
+
processDependencies<T, S, F>(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData)
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Processes all the `dependencies` recursively into the list of `resolvedSchema`s as needed. Passes the
|
|
422
|
+
* `expandAllBranches` flag down to the `withDependentSchema()` and the recursive `processDependencies()` helper calls.
|
|
423
|
+
*
|
|
424
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
425
|
+
* @param dependencies - The set of dependencies that needs to be processed
|
|
426
|
+
* @param resolvedSchema - The schema for which processing dependencies is desired
|
|
427
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
428
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
429
|
+
* as a list of schemas
|
|
430
|
+
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
431
|
+
* @returns - The schema with the `dependencies` resolved into it
|
|
432
|
+
*/
|
|
433
|
+
export function processDependencies<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
434
|
+
validator: ValidatorType<T, S, F>,
|
|
435
|
+
dependencies: S['dependencies'],
|
|
436
|
+
resolvedSchema: S,
|
|
437
|
+
rootSchema: S,
|
|
438
|
+
expandAllBranches: boolean,
|
|
439
|
+
formData?: T
|
|
440
|
+
): S[] {
|
|
441
|
+
let schemas = [resolvedSchema];
|
|
442
|
+
// Process dependencies updating the local schema properties as appropriate.
|
|
443
|
+
for (const dependencyKey in dependencies) {
|
|
444
|
+
// Skip this dependency if its trigger property is not present.
|
|
445
|
+
if (!expandAllBranches && get(formData, [dependencyKey]) === undefined) {
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
// Skip this dependency if it is not included in the schema (such as when dependencyKey is itself a hidden dependency.)
|
|
449
|
+
if (resolvedSchema.properties && !(dependencyKey in resolvedSchema.properties)) {
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const [remainingDependencies, dependencyValue] = splitKeyElementFromObject(
|
|
453
|
+
dependencyKey,
|
|
454
|
+
dependencies as GenericObjectType
|
|
455
|
+
);
|
|
456
|
+
if (Array.isArray(dependencyValue)) {
|
|
457
|
+
schemas[0] = withDependentProperties<S>(resolvedSchema, dependencyValue);
|
|
458
|
+
} else if (isObject(dependencyValue)) {
|
|
459
|
+
schemas = withDependentSchema<T, S, F>(
|
|
460
|
+
validator,
|
|
461
|
+
resolvedSchema,
|
|
462
|
+
rootSchema,
|
|
463
|
+
dependencyKey,
|
|
464
|
+
dependencyValue as S,
|
|
465
|
+
expandAllBranches,
|
|
466
|
+
formData
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
return schemas.flatMap((schema) =>
|
|
470
|
+
processDependencies<T, S, F>(validator, remainingDependencies, schema, rootSchema, expandAllBranches, formData)
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
return schemas;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** Updates a schema with additionally required properties added
|
|
477
|
+
*
|
|
478
|
+
* @param schema - The schema for which resolving a dependent properties is desired
|
|
479
|
+
* @param [additionallyRequired] - An optional array of additionally required names
|
|
480
|
+
* @returns - The schema with the additional required values merged in
|
|
481
|
+
*/
|
|
482
|
+
export function withDependentProperties<S extends StrictRJSFSchema = RJSFSchema>(
|
|
483
|
+
schema: S,
|
|
484
|
+
additionallyRequired?: string[]
|
|
485
|
+
) {
|
|
486
|
+
if (!additionallyRequired) {
|
|
487
|
+
return schema;
|
|
488
|
+
}
|
|
489
|
+
const required = Array.isArray(schema.required)
|
|
490
|
+
? Array.from(new Set([...schema.required, ...additionallyRequired]))
|
|
491
|
+
: additionallyRequired;
|
|
492
|
+
return { ...schema, required: required };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** Merges a dependent schema into the `schema` dealing with oneOfs and references. Passes the `expandAllBranches` flag
|
|
496
|
+
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `withExactlyOneSubschema()` helper calls.
|
|
497
|
+
*
|
|
498
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
499
|
+
* @param schema - The schema for which resolving a dependent schema is desired
|
|
500
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
501
|
+
* @param dependencyKey - The key name of the dependency
|
|
502
|
+
* @param dependencyValue - The potentially dependent schema
|
|
503
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
504
|
+
* as a list of schemas
|
|
505
|
+
* @param [formData]- The current formData to assist retrieving a schema
|
|
506
|
+
* @returns - The list of schemas with the dependent schema resolved into them
|
|
507
|
+
*/
|
|
508
|
+
export function withDependentSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
509
|
+
validator: ValidatorType<T, S, F>,
|
|
510
|
+
schema: S,
|
|
511
|
+
rootSchema: S,
|
|
512
|
+
dependencyKey: string,
|
|
513
|
+
dependencyValue: S,
|
|
514
|
+
expandAllBranches: boolean,
|
|
515
|
+
formData?: T
|
|
516
|
+
): S[] {
|
|
517
|
+
const dependentSchemas = retrieveSchemaInternal<T, S, F>(
|
|
518
|
+
validator,
|
|
519
|
+
dependencyValue,
|
|
520
|
+
rootSchema,
|
|
521
|
+
formData,
|
|
522
|
+
expandAllBranches
|
|
523
|
+
);
|
|
524
|
+
return dependentSchemas.flatMap((dependent) => {
|
|
525
|
+
const { oneOf, ...dependentSchema } = dependent;
|
|
526
|
+
schema = mergeSchemas(schema, dependentSchema) as S;
|
|
527
|
+
// Since it does not contain oneOf, we return the original schema.
|
|
528
|
+
if (oneOf === undefined) {
|
|
529
|
+
return schema;
|
|
530
|
+
}
|
|
531
|
+
// Resolve $refs inside oneOf.
|
|
532
|
+
const resolvedOneOfs = oneOf.map((subschema) => {
|
|
533
|
+
if (typeof subschema === 'boolean' || !(REF_KEY in subschema)) {
|
|
534
|
+
return [subschema as S];
|
|
535
|
+
}
|
|
536
|
+
return resolveReference<T, S, F>(validator, subschema as S, rootSchema, expandAllBranches, formData);
|
|
537
|
+
});
|
|
538
|
+
const allPermutations = getAllPermutationsOfXxxOf(resolvedOneOfs);
|
|
539
|
+
return allPermutations.flatMap((resolvedOneOf) =>
|
|
540
|
+
withExactlyOneSubschema<T, S, F>(
|
|
541
|
+
validator,
|
|
542
|
+
schema,
|
|
543
|
+
rootSchema,
|
|
544
|
+
dependencyKey,
|
|
545
|
+
resolvedOneOf,
|
|
546
|
+
expandAllBranches,
|
|
547
|
+
formData
|
|
548
|
+
)
|
|
549
|
+
);
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/** Returns a list of `schema`s with the best choice from the `oneOf` options merged into it. If `expandAllBranches` is
|
|
554
|
+
* true, then a list of schemas for ALL options are retrieved and returned. Passes the `expandAllBranches` flag down to
|
|
555
|
+
* the `retrieveSchemaInternal()` helper call.
|
|
556
|
+
*
|
|
557
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used to validate oneOf options
|
|
558
|
+
* @param schema - The schema for which resolving a oneOf subschema is desired
|
|
559
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
560
|
+
* @param dependencyKey - The key name of the oneOf dependency
|
|
561
|
+
* @param oneOf - The list of schemas representing the oneOf options
|
|
562
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
563
|
+
* as a list of schemas
|
|
564
|
+
* @param [formData] - The current formData to assist retrieving a schema
|
|
565
|
+
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
566
|
+
*/
|
|
567
|
+
export function withExactlyOneSubschema<
|
|
568
|
+
T = any,
|
|
569
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
570
|
+
F extends FormContextType = any
|
|
571
|
+
>(
|
|
572
|
+
validator: ValidatorType<T, S, F>,
|
|
573
|
+
schema: S,
|
|
574
|
+
rootSchema: S,
|
|
575
|
+
dependencyKey: string,
|
|
576
|
+
oneOf: S['oneOf'],
|
|
577
|
+
expandAllBranches: boolean,
|
|
578
|
+
formData?: T
|
|
579
|
+
): S[] {
|
|
580
|
+
const validSubschemas = oneOf!.filter((subschema) => {
|
|
581
|
+
if (typeof subschema === 'boolean' || !subschema || !subschema.properties) {
|
|
582
|
+
return false;
|
|
583
|
+
}
|
|
584
|
+
const { [dependencyKey]: conditionPropertySchema } = subschema.properties;
|
|
585
|
+
if (conditionPropertySchema) {
|
|
586
|
+
const conditionSchema: S = {
|
|
587
|
+
type: 'object',
|
|
588
|
+
properties: {
|
|
589
|
+
[dependencyKey]: conditionPropertySchema,
|
|
590
|
+
},
|
|
591
|
+
} as S;
|
|
592
|
+
return validator.isValid(conditionSchema, formData, rootSchema) || expandAllBranches;
|
|
593
|
+
}
|
|
594
|
+
return false;
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
if (!expandAllBranches && validSubschemas!.length !== 1) {
|
|
598
|
+
console.warn("ignoring oneOf in dependencies because there isn't exactly one subschema that is valid");
|
|
599
|
+
return [schema];
|
|
600
|
+
}
|
|
601
|
+
return validSubschemas.flatMap((s) => {
|
|
602
|
+
const subschema: S = s as S;
|
|
603
|
+
const [dependentSubschema] = splitKeyElementFromObject(dependencyKey, subschema.properties as GenericObjectType);
|
|
604
|
+
const dependentSchema = { ...subschema, properties: dependentSubschema };
|
|
605
|
+
const schemas = retrieveSchemaInternal<T, S, F>(
|
|
606
|
+
validator,
|
|
607
|
+
dependentSchema,
|
|
608
|
+
rootSchema,
|
|
609
|
+
formData,
|
|
610
|
+
expandAllBranches
|
|
611
|
+
);
|
|
612
|
+
return schemas.map((s) => mergeSchemas(schema, s) as S);
|
|
613
|
+
});
|
|
614
|
+
}
|