@rjsf/utils 6.0.0-alpha.0 → 6.0.0-beta.10
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 +1347 -642
- package/dist/index.js.map +4 -4
- package/dist/utils.esm.js +1324 -619
- package/dist/utils.esm.js.map +4 -4
- package/dist/utils.umd.js +1266 -590
- package/lib/ErrorSchemaBuilder.d.ts +8 -4
- package/lib/ErrorSchemaBuilder.js +10 -8
- package/lib/ErrorSchemaBuilder.js.map +1 -1
- package/lib/allowAdditionalItems.d.ts +1 -1
- package/lib/allowAdditionalItems.js +1 -1
- package/lib/allowAdditionalItems.js.map +1 -1
- package/lib/asNumber.js.map +1 -1
- package/lib/canExpand.d.ts +2 -2
- package/lib/canExpand.js +2 -2
- package/lib/canExpand.js.map +1 -1
- package/lib/constIsAjvDataReference.d.ts +9 -0
- package/lib/constIsAjvDataReference.js +15 -0
- package/lib/constIsAjvDataReference.js.map +1 -0
- package/lib/constants.d.ts +15 -3
- package/lib/constants.js +15 -3
- package/lib/constants.js.map +1 -1
- package/lib/createErrorHandler.d.ts +1 -1
- package/lib/createErrorHandler.js +2 -2
- package/lib/createErrorHandler.js.map +1 -1
- package/lib/createSchemaUtils.d.ts +3 -2
- package/lib/createSchemaUtils.js +56 -46
- package/lib/createSchemaUtils.js.map +1 -1
- package/lib/dataURItoBlob.js.map +1 -1
- package/lib/dateRangeOptions.d.ts +1 -1
- package/lib/dateRangeOptions.js +1 -1
- package/lib/dateRangeOptions.js.map +1 -1
- package/lib/deepEquals.js +1 -1
- package/lib/deepEquals.js.map +1 -1
- package/lib/englishStringTranslator.d.ts +1 -1
- package/lib/englishStringTranslator.js +1 -1
- package/lib/enumOptionsDeselectValue.d.ts +1 -1
- package/lib/enumOptionsDeselectValue.js +4 -4
- package/lib/enumOptionsDeselectValue.js.map +1 -1
- package/lib/enumOptionsIndexForValue.d.ts +1 -1
- package/lib/enumOptionsIndexForValue.js +1 -1
- package/lib/enumOptionsIndexForValue.js.map +1 -1
- package/lib/enumOptionsIsSelected.d.ts +1 -1
- package/lib/enumOptionsIsSelected.js +3 -3
- package/lib/enumOptionsIsSelected.js.map +1 -1
- package/lib/enumOptionsSelectValue.d.ts +1 -1
- package/lib/enumOptionsSelectValue.js +2 -2
- package/lib/enumOptionsSelectValue.js.map +1 -1
- package/lib/enumOptionsValueForIndex.d.ts +1 -1
- package/lib/enumOptionsValueForIndex.js.map +1 -1
- package/lib/enums.d.ts +2 -0
- package/lib/enums.js +2 -0
- package/lib/enums.js.map +1 -1
- package/lib/findSchemaDefinition.d.ts +5 -3
- package/lib/findSchemaDefinition.js +54 -11
- package/lib/findSchemaDefinition.js.map +1 -1
- package/lib/getChangedFields.d.ts +17 -0
- package/lib/getChangedFields.js +42 -0
- package/lib/getChangedFields.js.map +1 -0
- package/lib/getDateElementProps.d.ts +1 -1
- package/lib/getDateElementProps.js.map +1 -1
- package/lib/getDiscriminatorFieldFromSchema.d.ts +1 -1
- package/lib/getDiscriminatorFieldFromSchema.js +4 -3
- package/lib/getDiscriminatorFieldFromSchema.js.map +1 -1
- package/lib/getInputProps.d.ts +1 -1
- package/lib/getInputProps.js +4 -1
- package/lib/getInputProps.js.map +1 -1
- package/lib/getOptionMatchingSimpleDiscriminator.d.ts +1 -1
- package/lib/getOptionMatchingSimpleDiscriminator.js +2 -2
- package/lib/getOptionMatchingSimpleDiscriminator.js.map +1 -1
- package/lib/getSchemaType.d.ts +2 -1
- package/lib/getSchemaType.js +3 -2
- package/lib/getSchemaType.js.map +1 -1
- package/lib/getSubmitButtonOptions.d.ts +1 -1
- package/lib/getSubmitButtonOptions.js +2 -2
- package/lib/getSubmitButtonOptions.js.map +1 -1
- package/lib/getTemplate.d.ts +1 -1
- package/lib/getTemplate.js +9 -0
- package/lib/getTemplate.js.map +1 -1
- package/lib/getTestIds.d.ts +17 -0
- package/lib/getTestIds.js +34 -0
- package/lib/getTestIds.js.map +1 -0
- package/lib/getUiOptions.d.ts +1 -1
- package/lib/getUiOptions.js +2 -2
- package/lib/getUiOptions.js.map +1 -1
- package/lib/getWidget.d.ts +1 -1
- package/lib/getWidget.js +3 -3
- package/lib/getWidget.js.map +1 -1
- package/lib/guessType.d.ts +1 -1
- package/lib/guessType.js.map +1 -1
- package/lib/hasWidget.d.ts +1 -1
- package/lib/hasWidget.js +1 -1
- package/lib/hasWidget.js.map +1 -1
- package/lib/hashForSchema.d.ts +23 -1
- package/lib/hashForSchema.js +24 -6
- package/lib/hashForSchema.js.map +1 -1
- package/lib/idGenerators.d.ts +8 -1
- package/lib/idGenerators.js +11 -2
- package/lib/idGenerators.js.map +1 -1
- package/lib/index.d.ts +63 -60
- package/lib/index.js +63 -60
- package/lib/index.js.map +1 -1
- package/lib/isConstant.d.ts +1 -1
- package/lib/isConstant.js +1 -1
- package/lib/isCustomWidget.d.ts +1 -1
- package/lib/isCustomWidget.js +1 -1
- package/lib/isFixedItems.d.ts +1 -1
- package/lib/isFixedItems.js +1 -1
- package/lib/isObject.d.ts +2 -2
- package/lib/isObject.js +11 -4
- package/lib/isObject.js.map +1 -1
- package/lib/lookupFromFormContext.d.ts +11 -0
- package/lib/lookupFromFormContext.js +20 -0
- package/lib/lookupFromFormContext.js.map +1 -0
- package/lib/mergeDefaultsWithFormData.d.ts +8 -2
- package/lib/mergeDefaultsWithFormData.js +39 -10
- package/lib/mergeDefaultsWithFormData.js.map +1 -1
- package/lib/mergeObjects.d.ts +1 -1
- package/lib/mergeObjects.js +1 -1
- package/lib/mergeObjects.js.map +1 -1
- package/lib/mergeSchemas.d.ts +1 -1
- package/lib/mergeSchemas.js +4 -4
- package/lib/mergeSchemas.js.map +1 -1
- package/lib/optionsList.d.ts +9 -7
- package/lib/optionsList.js +30 -19
- package/lib/optionsList.js.map +1 -1
- package/lib/orderProperties.js.map +1 -1
- package/lib/pad.js.map +1 -1
- package/lib/parseDateString.d.ts +1 -1
- package/lib/parseDateString.js +1 -1
- package/lib/parseDateString.js.map +1 -1
- package/lib/parser/ParserValidator.d.ts +1 -1
- package/lib/parser/ParserValidator.js +6 -6
- package/lib/parser/ParserValidator.js.map +1 -1
- package/lib/parser/index.d.ts +2 -2
- package/lib/parser/index.js +1 -1
- package/lib/parser/schemaParser.d.ts +2 -2
- package/lib/parser/schemaParser.js +6 -6
- package/lib/parser/schemaParser.js.map +1 -1
- package/lib/rangeSpec.d.ts +2 -2
- package/lib/rangeSpec.js.map +1 -1
- package/lib/replaceStringParameters.js.map +1 -1
- package/lib/schema/findFieldInSchema.d.ts +19 -0
- package/lib/schema/findFieldInSchema.js +61 -0
- package/lib/schema/findFieldInSchema.js.map +1 -0
- package/lib/schema/findSelectedOptionInXxxOf.d.ts +16 -0
- package/lib/schema/findSelectedOptionInXxxOf.js +34 -0
- package/lib/schema/findSelectedOptionInXxxOf.js.map +1 -0
- package/lib/schema/getClosestMatchingOption.d.ts +5 -3
- package/lib/schema/getClosestMatchingOption.js +28 -20
- package/lib/schema/getClosestMatchingOption.js.map +1 -1
- package/lib/schema/getDefaultFormState.d.ts +60 -13
- package/lib/schema/getDefaultFormState.js +316 -167
- package/lib/schema/getDefaultFormState.js.map +1 -1
- package/lib/schema/getDisplayLabel.d.ts +3 -2
- package/lib/schema/getDisplayLabel.js +10 -9
- package/lib/schema/getDisplayLabel.js.map +1 -1
- package/lib/schema/getFirstMatchingOption.d.ts +1 -1
- package/lib/schema/getFirstMatchingOption.js +70 -2
- package/lib/schema/getFirstMatchingOption.js.map +1 -1
- package/lib/schema/getFromSchema.d.ts +14 -0
- package/lib/schema/getFromSchema.js +39 -0
- package/lib/schema/getFromSchema.js.map +1 -0
- package/lib/schema/index.d.ts +15 -14
- package/lib/schema/index.js +15 -14
- package/lib/schema/index.js.map +1 -1
- package/lib/schema/isFilesArray.d.ts +3 -2
- package/lib/schema/isFilesArray.js +5 -4
- package/lib/schema/isFilesArray.js.map +1 -1
- package/lib/schema/isMultiSelect.d.ts +3 -2
- package/lib/schema/isMultiSelect.js +4 -3
- package/lib/schema/isMultiSelect.js.map +1 -1
- package/lib/schema/isSelect.d.ts +3 -2
- package/lib/schema/isSelect.js +5 -4
- package/lib/schema/isSelect.js.map +1 -1
- package/lib/schema/retrieveSchema.d.ts +30 -12
- package/lib/schema/retrieveSchema.js +153 -70
- package/lib/schema/retrieveSchema.js.map +1 -1
- package/lib/schema/sanitizeDataForNewSchema.d.ts +3 -2
- package/lib/schema/sanitizeDataForNewSchema.js +12 -11
- package/lib/schema/sanitizeDataForNewSchema.js.map +1 -1
- package/lib/schema/toIdSchema.d.ts +3 -2
- package/lib/schema/toIdSchema.js +30 -27
- package/lib/schema/toIdSchema.js.map +1 -1
- package/lib/schema/toPathSchema.d.ts +3 -2
- package/lib/schema/toPathSchema.js +22 -20
- package/lib/schema/toPathSchema.js.map +1 -1
- package/lib/schemaRequiresTrueValue.d.ts +1 -1
- package/lib/schemaRequiresTrueValue.js.map +1 -1
- package/lib/shouldRender.js +1 -1
- package/lib/toConstant.d.ts +1 -1
- package/lib/toConstant.js +1 -1
- package/lib/toConstant.js.map +1 -1
- package/lib/toDateString.d.ts +1 -1
- package/lib/toErrorList.d.ts +1 -1
- package/lib/toErrorList.js +2 -2
- package/lib/toErrorList.js.map +1 -1
- package/lib/toErrorSchema.d.ts +1 -1
- package/lib/toErrorSchema.js +2 -2
- package/lib/toErrorSchema.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +172 -142
- package/lib/unwrapErrorHandler.d.ts +1 -1
- package/lib/unwrapErrorHandler.js +1 -1
- package/lib/unwrapErrorHandler.js.map +1 -1
- package/lib/utcToLocal.js +1 -1
- package/lib/utcToLocal.js.map +1 -1
- package/lib/validationDataMerge.d.ts +1 -1
- package/lib/validationDataMerge.js +3 -3
- package/lib/validationDataMerge.js.map +1 -1
- package/lib/withIdRefPrefix.d.ts +1 -1
- package/lib/withIdRefPrefix.js +2 -2
- package/lib/withIdRefPrefix.js.map +1 -1
- package/package.json +37 -26
- package/src/ErrorSchemaBuilder.ts +15 -8
- package/src/canExpand.ts +2 -2
- package/src/constIsAjvDataReference.ts +17 -0
- package/src/constants.ts +17 -3
- package/src/createSchemaUtils.ts +140 -50
- package/src/dataURItoBlob.ts +1 -1
- package/src/dateRangeOptions.ts +1 -1
- package/src/enumOptionsDeselectValue.ts +4 -5
- package/src/enumOptionsIndexForValue.ts +1 -1
- package/src/enumOptionsIsSelected.ts +4 -5
- package/src/enumOptionsSelectValue.ts +1 -1
- package/src/enumOptionsValueForIndex.ts +1 -1
- package/src/enums.ts +2 -0
- package/src/findSchemaDefinition.ts +55 -10
- package/src/getChangedFields.ts +40 -0
- package/src/getDateElementProps.ts +2 -2
- package/src/getDiscriminatorFieldFromSchema.ts +2 -1
- package/src/getInputProps.ts +6 -2
- package/src/getOptionMatchingSimpleDiscriminator.ts +2 -2
- package/src/getSchemaType.ts +3 -2
- package/src/getSubmitButtonOptions.ts +1 -1
- package/src/getTemplate.ts +12 -1
- package/src/getTestIds.ts +40 -0
- package/src/getUiOptions.ts +2 -2
- package/src/getWidget.tsx +2 -2
- package/src/hasWidget.ts +1 -1
- package/src/hashForSchema.ts +26 -6
- package/src/idGenerators.ts +10 -0
- package/src/index.ts +21 -2
- package/src/isCustomWidget.ts +1 -1
- package/src/isObject.ts +12 -5
- package/src/labelValue.ts +2 -2
- package/src/lookupFromFormContext.ts +26 -0
- package/src/mergeDefaultsWithFormData.ts +54 -9
- package/src/mergeObjects.ts +24 -21
- package/src/optionsList.ts +31 -22
- package/src/parser/ParserValidator.ts +5 -5
- package/src/parser/schemaParser.ts +6 -6
- package/src/schema/findFieldInSchema.ts +138 -0
- package/src/schema/findSelectedOptionInXxxOf.ts +53 -0
- package/src/schema/getClosestMatchingOption.ts +38 -11
- package/src/schema/getDefaultFormState.ts +461 -193
- package/src/schema/getDisplayLabel.ts +7 -4
- package/src/schema/getFirstMatchingOption.ts +79 -4
- package/src/schema/getFromSchema.ts +100 -0
- package/src/schema/index.ts +6 -4
- package/src/schema/isFilesArray.ts +18 -3
- package/src/schema/isMultiSelect.ts +10 -4
- package/src/schema/isSelect.ts +5 -3
- package/src/schema/retrieveSchema.ts +268 -78
- package/src/schema/sanitizeDataForNewSchema.ts +52 -11
- package/src/schema/toIdSchema.ts +69 -43
- package/src/schema/toPathSchema.ts +49 -16
- package/src/toErrorList.ts +2 -2
- package/src/types.ts +278 -184
- package/src/validationDataMerge.ts +1 -1
- package/src/withIdRefPrefix.ts +1 -1
- package/LICENSE.md +0 -201
- package/lib/schema/getMatchingOption.d.ts +0 -14
- package/lib/schema/getMatchingOption.js +0 -85
- package/lib/schema/getMatchingOption.js.map +0 -1
- package/lib/schema/mergeValidationData.d.ts +0 -14
- package/lib/schema/mergeValidationData.js +0 -28
- package/lib/schema/mergeValidationData.js.map +0 -1
- package/src/schema/getMatchingOption.ts +0 -103
- package/src/schema/mergeValidationData.ts +0 -38
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
StrictRJSFSchema,
|
|
10
10
|
UiSchema,
|
|
11
11
|
ValidatorType,
|
|
12
|
+
Experimental_CustomMergeAllOf,
|
|
12
13
|
} from '../types';
|
|
13
14
|
import isFilesArray from './isFilesArray';
|
|
14
15
|
import isMultiSelect from './isMultiSelect';
|
|
@@ -21,18 +22,20 @@ import isMultiSelect from './isMultiSelect';
|
|
|
21
22
|
* @param [uiSchema={}] - The UI schema from which to derive potentially displayable information
|
|
22
23
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
23
24
|
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
|
|
25
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
24
26
|
* @returns - True if the label should be displayed or false if it should not
|
|
25
27
|
*/
|
|
26
28
|
export default function getDisplayLabel<
|
|
27
29
|
T = any,
|
|
28
30
|
S extends StrictRJSFSchema = RJSFSchema,
|
|
29
|
-
F extends FormContextType = any
|
|
31
|
+
F extends FormContextType = any,
|
|
30
32
|
>(
|
|
31
33
|
validator: ValidatorType<T, S, F>,
|
|
32
34
|
schema: S,
|
|
33
35
|
uiSchema: UiSchema<T, S, F> = {},
|
|
34
36
|
rootSchema?: S,
|
|
35
|
-
globalOptions?: GlobalUISchemaOptions
|
|
37
|
+
globalOptions?: GlobalUISchemaOptions,
|
|
38
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
36
39
|
): boolean {
|
|
37
40
|
const uiOptions = getUiOptions<T, S, F>(uiSchema, globalOptions);
|
|
38
41
|
const { label = true } = uiOptions;
|
|
@@ -41,8 +44,8 @@ export default function getDisplayLabel<
|
|
|
41
44
|
|
|
42
45
|
if (schemaType === 'array') {
|
|
43
46
|
displayLabel =
|
|
44
|
-
isMultiSelect<T, S, F>(validator, schema, rootSchema) ||
|
|
45
|
-
isFilesArray<T, S, F>(validator, schema, uiSchema, rootSchema) ||
|
|
47
|
+
isMultiSelect<T, S, F>(validator, schema, rootSchema, experimental_customMergeAllOf) ||
|
|
48
|
+
isFilesArray<T, S, F>(validator, schema, uiSchema, rootSchema, experimental_customMergeAllOf) ||
|
|
46
49
|
isCustomWidget(uiSchema);
|
|
47
50
|
}
|
|
48
51
|
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import has from 'lodash/has';
|
|
3
|
+
import isNumber from 'lodash/isNumber';
|
|
4
|
+
|
|
5
|
+
import { PROPERTIES_KEY } from '../constants';
|
|
6
|
+
import getOptionMatchingSimpleDiscriminator from '../getOptionMatchingSimpleDiscriminator';
|
|
2
7
|
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
3
8
|
|
|
4
9
|
/** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
|
|
@@ -15,13 +20,83 @@ import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '..
|
|
|
15
20
|
export default function getFirstMatchingOption<
|
|
16
21
|
T = any,
|
|
17
22
|
S extends StrictRJSFSchema = RJSFSchema,
|
|
18
|
-
F extends FormContextType = any
|
|
23
|
+
F extends FormContextType = any,
|
|
19
24
|
>(
|
|
20
25
|
validator: ValidatorType<T, S, F>,
|
|
21
26
|
formData: T | undefined,
|
|
22
27
|
options: S[],
|
|
23
28
|
rootSchema: S,
|
|
24
|
-
discriminatorField?: string
|
|
29
|
+
discriminatorField?: string,
|
|
25
30
|
): number {
|
|
26
|
-
|
|
31
|
+
// For performance, skip validating subschemas if formData is undefined. We just
|
|
32
|
+
// want to get the first option in that case.
|
|
33
|
+
if (formData === undefined) {
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const simpleDiscriminatorMatch = getOptionMatchingSimpleDiscriminator(formData, options, discriminatorField);
|
|
38
|
+
if (isNumber(simpleDiscriminatorMatch)) {
|
|
39
|
+
return simpleDiscriminatorMatch;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < options.length; i++) {
|
|
43
|
+
const option = options[i];
|
|
44
|
+
|
|
45
|
+
// If we have a discriminator field, then we will use this to make the determination
|
|
46
|
+
if (discriminatorField && has(option, [PROPERTIES_KEY, discriminatorField])) {
|
|
47
|
+
const value = get(formData, discriminatorField);
|
|
48
|
+
const discriminator: S = get(option, [PROPERTIES_KEY, discriminatorField], {}) as S;
|
|
49
|
+
if (validator.isValid(discriminator, value, rootSchema)) {
|
|
50
|
+
return i;
|
|
51
|
+
}
|
|
52
|
+
} else if (option[PROPERTIES_KEY]) {
|
|
53
|
+
// If the schema describes an object then we need to add slightly more
|
|
54
|
+
// strict matching to the schema, because unless the schema uses the
|
|
55
|
+
// "requires" keyword, an object will match the schema as long as it
|
|
56
|
+
// doesn't have matching keys with a conflicting type. To do this we use an
|
|
57
|
+
// "anyOf" with an array of requires. This augmentation expresses that the
|
|
58
|
+
// schema should match if any of the keys in the schema are present on the
|
|
59
|
+
// object and pass validation.
|
|
60
|
+
//
|
|
61
|
+
// Create an "anyOf" schema that requires at least one of the keys in the
|
|
62
|
+
// "properties" object
|
|
63
|
+
const requiresAnyOf = {
|
|
64
|
+
anyOf: Object.keys(option[PROPERTIES_KEY]).map((key) => ({
|
|
65
|
+
required: [key],
|
|
66
|
+
})),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
let augmentedSchema;
|
|
70
|
+
|
|
71
|
+
// If the "anyOf" keyword already exists, wrap the augmentation in an "allOf"
|
|
72
|
+
if (option.anyOf) {
|
|
73
|
+
// Create a shallow clone of the option
|
|
74
|
+
const { ...shallowClone } = option;
|
|
75
|
+
|
|
76
|
+
if (!shallowClone.allOf) {
|
|
77
|
+
shallowClone.allOf = [];
|
|
78
|
+
} else {
|
|
79
|
+
// If "allOf" already exists, shallow clone the array
|
|
80
|
+
shallowClone.allOf = shallowClone.allOf.slice();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
shallowClone.allOf.push(requiresAnyOf);
|
|
84
|
+
|
|
85
|
+
augmentedSchema = shallowClone;
|
|
86
|
+
} else {
|
|
87
|
+
augmentedSchema = Object.assign({}, option, requiresAnyOf);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Remove the "required" field as it's likely that not all fields have
|
|
91
|
+
// been filled in yet, which will mean that the schema is not valid
|
|
92
|
+
delete augmentedSchema.required;
|
|
93
|
+
|
|
94
|
+
if (validator.isValid(augmentedSchema, formData, rootSchema)) {
|
|
95
|
+
return i;
|
|
96
|
+
}
|
|
97
|
+
} else if (validator.isValid(option, formData, rootSchema)) {
|
|
98
|
+
return i;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return 0;
|
|
27
102
|
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import has from 'lodash/has';
|
|
3
|
+
import isEmpty from 'lodash/isEmpty';
|
|
4
|
+
|
|
5
|
+
import retrieveSchema from './retrieveSchema';
|
|
6
|
+
import { Experimental_CustomMergeAllOf, FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
7
|
+
import { REF_KEY } from '../constants';
|
|
8
|
+
|
|
9
|
+
/** Internal helper function that acts like lodash's `get` but additionally retrieves `$ref`s as needed to get the path
|
|
10
|
+
* for schemas containing potentially nested `$ref`s.
|
|
11
|
+
*
|
|
12
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
13
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
14
|
+
* @param schema - The current node within the JSON schema recursion
|
|
15
|
+
* @param path - The remaining keys in the path to the desired property
|
|
16
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
17
|
+
* @returns - The internal schema from the `schema` for the given `path` or undefined if not found
|
|
18
|
+
*/
|
|
19
|
+
function getFromSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
20
|
+
validator: ValidatorType<T, S, F>,
|
|
21
|
+
rootSchema: S,
|
|
22
|
+
schema: S,
|
|
23
|
+
path: string | string[],
|
|
24
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
25
|
+
): T | S | undefined {
|
|
26
|
+
let fieldSchema = schema;
|
|
27
|
+
if (has(schema, REF_KEY)) {
|
|
28
|
+
fieldSchema = retrieveSchema<T, S, F>(validator, schema, rootSchema, undefined, experimental_customMergeAllOf);
|
|
29
|
+
}
|
|
30
|
+
if (isEmpty(path)) {
|
|
31
|
+
return fieldSchema;
|
|
32
|
+
}
|
|
33
|
+
const pathList = Array.isArray(path) ? path : path.split('.');
|
|
34
|
+
const [part, ...nestedPath] = pathList;
|
|
35
|
+
if (part && has(fieldSchema, part)) {
|
|
36
|
+
fieldSchema = get(fieldSchema, part) as S;
|
|
37
|
+
return getFromSchemaInternal<T, S, F>(
|
|
38
|
+
validator,
|
|
39
|
+
rootSchema,
|
|
40
|
+
fieldSchema,
|
|
41
|
+
nestedPath,
|
|
42
|
+
experimental_customMergeAllOf,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Helper that acts like lodash's `get` but additionally retrieves `$ref`s as needed to get the path for schemas
|
|
49
|
+
* containing potentially nested `$ref`s.
|
|
50
|
+
*
|
|
51
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
52
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
53
|
+
* @param schema - The current node within the JSON schema recursion
|
|
54
|
+
* @param path - The keys in the path to the desired field
|
|
55
|
+
* @param defaultValue - The value to return if a value is not found for the `pathList` path
|
|
56
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
57
|
+
* @returns - The inner schema from the `schema` for the given `path` or the `defaultValue` if not found
|
|
58
|
+
*/
|
|
59
|
+
export default function getFromSchema<
|
|
60
|
+
T = any,
|
|
61
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
62
|
+
F extends FormContextType = any,
|
|
63
|
+
>(
|
|
64
|
+
validator: ValidatorType<T, S, F>,
|
|
65
|
+
rootSchema: S,
|
|
66
|
+
schema: S,
|
|
67
|
+
path: string | string[],
|
|
68
|
+
defaultValue: T,
|
|
69
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
70
|
+
): T;
|
|
71
|
+
export default function getFromSchema<
|
|
72
|
+
T = any,
|
|
73
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
74
|
+
F extends FormContextType = any,
|
|
75
|
+
>(
|
|
76
|
+
validator: ValidatorType<T, S, F>,
|
|
77
|
+
rootSchema: S,
|
|
78
|
+
schema: S,
|
|
79
|
+
path: string | string[],
|
|
80
|
+
defaultValue: S,
|
|
81
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
82
|
+
): S;
|
|
83
|
+
export default function getFromSchema<
|
|
84
|
+
T = any,
|
|
85
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
86
|
+
F extends FormContextType = any,
|
|
87
|
+
>(
|
|
88
|
+
validator: ValidatorType<T, S, F>,
|
|
89
|
+
rootSchema: S,
|
|
90
|
+
schema: S,
|
|
91
|
+
path: string | string[],
|
|
92
|
+
defaultValue: T | S,
|
|
93
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
94
|
+
): T | S {
|
|
95
|
+
const result = getFromSchemaInternal(validator, rootSchema, schema, path, experimental_customMergeAllOf);
|
|
96
|
+
if (result === undefined) {
|
|
97
|
+
return defaultValue;
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
package/src/schema/index.ts
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
|
+
import findFieldInSchema from './findFieldInSchema';
|
|
2
|
+
import findSelectedOptionInXxxOf from './findSelectedOptionInXxxOf';
|
|
1
3
|
import getDefaultFormState from './getDefaultFormState';
|
|
2
4
|
import getDisplayLabel from './getDisplayLabel';
|
|
3
5
|
import getClosestMatchingOption from './getClosestMatchingOption';
|
|
4
6
|
import getFirstMatchingOption from './getFirstMatchingOption';
|
|
5
|
-
import
|
|
7
|
+
import getFromSchema from './getFromSchema';
|
|
6
8
|
import isFilesArray from './isFilesArray';
|
|
7
9
|
import isMultiSelect from './isMultiSelect';
|
|
8
10
|
import isSelect from './isSelect';
|
|
9
|
-
import mergeValidationData from './mergeValidationData';
|
|
10
11
|
import retrieveSchema from './retrieveSchema';
|
|
11
12
|
import sanitizeDataForNewSchema from './sanitizeDataForNewSchema';
|
|
12
13
|
import toIdSchema from './toIdSchema';
|
|
13
14
|
import toPathSchema from './toPathSchema';
|
|
14
15
|
|
|
15
16
|
export {
|
|
17
|
+
findFieldInSchema,
|
|
18
|
+
findSelectedOptionInXxxOf,
|
|
16
19
|
getDefaultFormState,
|
|
17
20
|
getDisplayLabel,
|
|
18
21
|
getClosestMatchingOption,
|
|
19
22
|
getFirstMatchingOption,
|
|
20
|
-
|
|
23
|
+
getFromSchema,
|
|
21
24
|
isFilesArray,
|
|
22
25
|
isMultiSelect,
|
|
23
26
|
isSelect,
|
|
24
|
-
mergeValidationData,
|
|
25
27
|
retrieveSchema,
|
|
26
28
|
sanitizeDataForNewSchema,
|
|
27
29
|
toIdSchema,
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { UI_WIDGET_KEY } from '../constants';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Experimental_CustomMergeAllOf,
|
|
4
|
+
FormContextType,
|
|
5
|
+
RJSFSchema,
|
|
6
|
+
StrictRJSFSchema,
|
|
7
|
+
UiSchema,
|
|
8
|
+
ValidatorType,
|
|
9
|
+
} from '../types';
|
|
3
10
|
import retrieveSchema from './retrieveSchema';
|
|
4
11
|
|
|
5
12
|
/** Checks to see if the `schema` and `uiSchema` combination represents an array of files
|
|
@@ -8,19 +15,27 @@ import retrieveSchema from './retrieveSchema';
|
|
|
8
15
|
* @param schema - The schema for which check for array of files flag is desired
|
|
9
16
|
* @param [uiSchema={}] - The UI schema from which to check the widget
|
|
10
17
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
18
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
11
19
|
* @returns - True if schema/uiSchema contains an array of files, otherwise false
|
|
12
20
|
*/
|
|
13
21
|
export default function isFilesArray<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
14
22
|
validator: ValidatorType<T, S, F>,
|
|
15
23
|
schema: S,
|
|
16
24
|
uiSchema: UiSchema<T, S, F> = {},
|
|
17
|
-
rootSchema?: S
|
|
25
|
+
rootSchema?: S,
|
|
26
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
18
27
|
) {
|
|
19
28
|
if (uiSchema[UI_WIDGET_KEY] === 'files') {
|
|
20
29
|
return true;
|
|
21
30
|
}
|
|
22
31
|
if (schema.items) {
|
|
23
|
-
const itemsSchema = retrieveSchema<T, S, F>(
|
|
32
|
+
const itemsSchema = retrieveSchema<T, S, F>(
|
|
33
|
+
validator,
|
|
34
|
+
schema.items as S,
|
|
35
|
+
rootSchema,
|
|
36
|
+
undefined,
|
|
37
|
+
experimental_customMergeAllOf,
|
|
38
|
+
);
|
|
24
39
|
return itemsSchema.type === 'string' && itemsSchema.format === 'data-url';
|
|
25
40
|
}
|
|
26
41
|
return false;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
1
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType, Experimental_CustomMergeAllOf } from '../types';
|
|
2
2
|
|
|
3
3
|
import isSelect from './isSelect';
|
|
4
4
|
|
|
@@ -7,15 +7,21 @@ import isSelect from './isSelect';
|
|
|
7
7
|
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
8
|
* @param schema - The schema for which check for a multi-select flag is desired
|
|
9
9
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
10
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
10
11
|
* @returns - True if schema contains a multi-select, otherwise false
|
|
11
12
|
*/
|
|
12
13
|
export default function isMultiSelect<
|
|
13
14
|
T = any,
|
|
14
15
|
S extends StrictRJSFSchema = RJSFSchema,
|
|
15
|
-
F extends FormContextType = any
|
|
16
|
-
>(
|
|
16
|
+
F extends FormContextType = any,
|
|
17
|
+
>(
|
|
18
|
+
validator: ValidatorType<T, S, F>,
|
|
19
|
+
schema: S,
|
|
20
|
+
rootSchema?: S,
|
|
21
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
22
|
+
) {
|
|
17
23
|
if (!schema.uniqueItems || !schema.items || typeof schema.items === 'boolean') {
|
|
18
24
|
return false;
|
|
19
25
|
}
|
|
20
|
-
return isSelect<T, S, F>(validator, schema.items as S, rootSchema);
|
|
26
|
+
return isSelect<T, S, F>(validator, schema.items as S, rootSchema, experimental_customMergeAllOf);
|
|
21
27
|
}
|
package/src/schema/isSelect.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import isConstant from '../isConstant';
|
|
2
|
-
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
2
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType, Experimental_CustomMergeAllOf } from '../types';
|
|
3
3
|
import retrieveSchema from './retrieveSchema';
|
|
4
4
|
|
|
5
5
|
/** Checks to see if the `schema` combination represents a select
|
|
@@ -7,14 +7,16 @@ import retrieveSchema from './retrieveSchema';
|
|
|
7
7
|
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
8
|
* @param theSchema - The schema for which check for a select flag is desired
|
|
9
9
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
10
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
10
11
|
* @returns - True if schema contains a select, otherwise false
|
|
11
12
|
*/
|
|
12
13
|
export default function isSelect<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
13
14
|
validator: ValidatorType<T, S, F>,
|
|
14
15
|
theSchema: S,
|
|
15
|
-
rootSchema: S = {} as S
|
|
16
|
+
rootSchema: S = {} as S,
|
|
17
|
+
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
|
|
16
18
|
) {
|
|
17
|
-
const schema = retrieveSchema<T, S, F>(validator, theSchema, rootSchema, undefined);
|
|
19
|
+
const schema = retrieveSchema<T, S, F>(validator, theSchema, rootSchema, undefined, experimental_customMergeAllOf);
|
|
18
20
|
const altSchemas = schema.oneOf || schema.anyOf;
|
|
19
21
|
if (Array.isArray(schema.enum)) {
|
|
20
22
|
return true;
|