@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,59 @@
|
|
|
1
|
+
import { UI_FIELD_KEY, UI_WIDGET_KEY } from '../constants';
|
|
2
|
+
import getSchemaType from '../getSchemaType';
|
|
3
|
+
import getUiOptions from '../getUiOptions';
|
|
4
|
+
import isCustomWidget from '../isCustomWidget';
|
|
5
|
+
import {
|
|
6
|
+
FormContextType,
|
|
7
|
+
GlobalUISchemaOptions,
|
|
8
|
+
RJSFSchema,
|
|
9
|
+
StrictRJSFSchema,
|
|
10
|
+
UiSchema,
|
|
11
|
+
ValidatorType,
|
|
12
|
+
} from '../types';
|
|
13
|
+
import isFilesArray from './isFilesArray';
|
|
14
|
+
import isMultiSelect from './isMultiSelect';
|
|
15
|
+
|
|
16
|
+
/** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
|
|
17
|
+
* should be displayed in a UI.
|
|
18
|
+
*
|
|
19
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
20
|
+
* @param schema - The schema for which the display label flag is desired
|
|
21
|
+
* @param [uiSchema={}] - The UI schema from which to derive potentially displayable information
|
|
22
|
+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
23
|
+
* @param [globalOptions={}] - The optional Global UI Schema from which to get any fallback `xxx` options
|
|
24
|
+
* @returns - True if the label should be displayed or false if it should not
|
|
25
|
+
*/
|
|
26
|
+
export default function getDisplayLabel<
|
|
27
|
+
T = any,
|
|
28
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
29
|
+
F extends FormContextType = any
|
|
30
|
+
>(
|
|
31
|
+
validator: ValidatorType<T, S, F>,
|
|
32
|
+
schema: S,
|
|
33
|
+
uiSchema: UiSchema<T, S, F> = {},
|
|
34
|
+
rootSchema?: S,
|
|
35
|
+
globalOptions?: GlobalUISchemaOptions
|
|
36
|
+
): boolean {
|
|
37
|
+
const uiOptions = getUiOptions<T, S, F>(uiSchema, globalOptions);
|
|
38
|
+
const { label = true } = uiOptions;
|
|
39
|
+
let displayLabel = !!label;
|
|
40
|
+
const schemaType = getSchemaType<S>(schema);
|
|
41
|
+
|
|
42
|
+
if (schemaType === 'array') {
|
|
43
|
+
displayLabel =
|
|
44
|
+
isMultiSelect<T, S, F>(validator, schema, rootSchema) ||
|
|
45
|
+
isFilesArray<T, S, F>(validator, schema, uiSchema, rootSchema) ||
|
|
46
|
+
isCustomWidget(uiSchema);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (schemaType === 'object') {
|
|
50
|
+
displayLabel = false;
|
|
51
|
+
}
|
|
52
|
+
if (schemaType === 'boolean' && !uiSchema[UI_WIDGET_KEY]) {
|
|
53
|
+
displayLabel = false;
|
|
54
|
+
}
|
|
55
|
+
if (uiSchema[UI_FIELD_KEY]) {
|
|
56
|
+
displayLabel = false;
|
|
57
|
+
}
|
|
58
|
+
return displayLabel;
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import getMatchingOption from './getMatchingOption';
|
|
2
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
3
|
+
|
|
4
|
+
/** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
|
|
5
|
+
* Always returns the first option if there is nothing that matches.
|
|
6
|
+
*
|
|
7
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
|
+
* @param formData - The current formData, if any, used to figure out a match
|
|
9
|
+
* @param options - The list of options to find a matching options from
|
|
10
|
+
* @param rootSchema - The root schema, used to primarily to look up `$ref`s
|
|
11
|
+
* @param [discriminatorField] - The optional name of the field within the options object whose value is used to
|
|
12
|
+
* determine which option is selected
|
|
13
|
+
* @returns - The index of the first matched option or 0 if none is available
|
|
14
|
+
*/
|
|
15
|
+
export default function getFirstMatchingOption<
|
|
16
|
+
T = any,
|
|
17
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
18
|
+
F extends FormContextType = any
|
|
19
|
+
>(
|
|
20
|
+
validator: ValidatorType<T, S, F>,
|
|
21
|
+
formData: T | undefined,
|
|
22
|
+
options: S[],
|
|
23
|
+
rootSchema: S,
|
|
24
|
+
discriminatorField?: string
|
|
25
|
+
): number {
|
|
26
|
+
return getMatchingOption<T, S, F>(validator, formData, options, rootSchema, discriminatorField);
|
|
27
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import get from 'lodash/get';
|
|
2
|
+
import has from 'lodash/has';
|
|
3
|
+
|
|
4
|
+
import { PROPERTIES_KEY } from '../constants';
|
|
5
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
6
|
+
|
|
7
|
+
/** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
|
|
8
|
+
* Deprecated, use `getFirstMatchingOption()` instead.
|
|
9
|
+
*
|
|
10
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
11
|
+
* @param formData - The current formData, if any, used to figure out a match
|
|
12
|
+
* @param options - The list of options to find a matching options from
|
|
13
|
+
* @param rootSchema - The root schema, used to primarily to look up `$ref`s
|
|
14
|
+
* @param [discriminatorField] - The optional name of the field within the options object whose value is used to
|
|
15
|
+
* determine which option is selected
|
|
16
|
+
* @returns - The index of the matched option or 0 if none is available
|
|
17
|
+
* @deprecated
|
|
18
|
+
*/
|
|
19
|
+
export default function getMatchingOption<
|
|
20
|
+
T = any,
|
|
21
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
22
|
+
F extends FormContextType = any
|
|
23
|
+
>(
|
|
24
|
+
validator: ValidatorType<T, S, F>,
|
|
25
|
+
formData: T | undefined,
|
|
26
|
+
options: S[],
|
|
27
|
+
rootSchema: S,
|
|
28
|
+
discriminatorField?: string
|
|
29
|
+
): number {
|
|
30
|
+
// For performance, skip validating subschemas if formData is undefined. We just
|
|
31
|
+
// want to get the first option in that case.
|
|
32
|
+
if (formData === undefined) {
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
for (let i = 0; i < options.length; i++) {
|
|
36
|
+
const option = options[i];
|
|
37
|
+
|
|
38
|
+
// If we have a discriminator field, then we will use this to make the determination
|
|
39
|
+
if (discriminatorField && has(option, [PROPERTIES_KEY, discriminatorField])) {
|
|
40
|
+
const value = get(formData, discriminatorField);
|
|
41
|
+
const discriminator = get(option, [PROPERTIES_KEY, discriminatorField], {});
|
|
42
|
+
if (validator.isValid(discriminator, value, rootSchema)) {
|
|
43
|
+
return i;
|
|
44
|
+
}
|
|
45
|
+
} else if (option[PROPERTIES_KEY]) {
|
|
46
|
+
// If the schema describes an object then we need to add slightly more
|
|
47
|
+
// strict matching to the schema, because unless the schema uses the
|
|
48
|
+
// "requires" keyword, an object will match the schema as long as it
|
|
49
|
+
// doesn't have matching keys with a conflicting type. To do this we use an
|
|
50
|
+
// "anyOf" with an array of requires. This augmentation expresses that the
|
|
51
|
+
// schema should match if any of the keys in the schema are present on the
|
|
52
|
+
// object and pass validation.
|
|
53
|
+
//
|
|
54
|
+
// Create an "anyOf" schema that requires at least one of the keys in the
|
|
55
|
+
// "properties" object
|
|
56
|
+
const requiresAnyOf = {
|
|
57
|
+
anyOf: Object.keys(option[PROPERTIES_KEY]).map((key) => ({
|
|
58
|
+
required: [key],
|
|
59
|
+
})),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
let augmentedSchema;
|
|
63
|
+
|
|
64
|
+
// If the "anyOf" keyword already exists, wrap the augmentation in an "allOf"
|
|
65
|
+
if (option.anyOf) {
|
|
66
|
+
// Create a shallow clone of the option
|
|
67
|
+
const { ...shallowClone } = option;
|
|
68
|
+
|
|
69
|
+
if (!shallowClone.allOf) {
|
|
70
|
+
shallowClone.allOf = [];
|
|
71
|
+
} else {
|
|
72
|
+
// If "allOf" already exists, shallow clone the array
|
|
73
|
+
shallowClone.allOf = shallowClone.allOf.slice();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
shallowClone.allOf.push(requiresAnyOf);
|
|
77
|
+
|
|
78
|
+
augmentedSchema = shallowClone;
|
|
79
|
+
} else {
|
|
80
|
+
augmentedSchema = Object.assign({}, option, requiresAnyOf);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Remove the "required" field as it's likely that not all fields have
|
|
84
|
+
// been filled in yet, which will mean that the schema is not valid
|
|
85
|
+
delete augmentedSchema.required;
|
|
86
|
+
|
|
87
|
+
if (validator.isValid(augmentedSchema, formData, rootSchema)) {
|
|
88
|
+
return i;
|
|
89
|
+
}
|
|
90
|
+
} else if (validator.isValid(option, formData, rootSchema)) {
|
|
91
|
+
return i;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import getDefaultFormState from './getDefaultFormState';
|
|
2
|
+
import getDisplayLabel from './getDisplayLabel';
|
|
3
|
+
import getClosestMatchingOption from './getClosestMatchingOption';
|
|
4
|
+
import getFirstMatchingOption from './getFirstMatchingOption';
|
|
5
|
+
import getMatchingOption from './getMatchingOption';
|
|
6
|
+
import isFilesArray from './isFilesArray';
|
|
7
|
+
import isMultiSelect from './isMultiSelect';
|
|
8
|
+
import isSelect from './isSelect';
|
|
9
|
+
import mergeValidationData from './mergeValidationData';
|
|
10
|
+
import retrieveSchema from './retrieveSchema';
|
|
11
|
+
import sanitizeDataForNewSchema from './sanitizeDataForNewSchema';
|
|
12
|
+
import toIdSchema from './toIdSchema';
|
|
13
|
+
import toPathSchema from './toPathSchema';
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
getDefaultFormState,
|
|
17
|
+
getDisplayLabel,
|
|
18
|
+
getClosestMatchingOption,
|
|
19
|
+
getFirstMatchingOption,
|
|
20
|
+
getMatchingOption,
|
|
21
|
+
isFilesArray,
|
|
22
|
+
isMultiSelect,
|
|
23
|
+
isSelect,
|
|
24
|
+
mergeValidationData,
|
|
25
|
+
retrieveSchema,
|
|
26
|
+
sanitizeDataForNewSchema,
|
|
27
|
+
toIdSchema,
|
|
28
|
+
toPathSchema,
|
|
29
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { UI_WIDGET_KEY } from '../constants';
|
|
2
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, UiSchema, ValidatorType } from '../types';
|
|
3
|
+
import retrieveSchema from './retrieveSchema';
|
|
4
|
+
|
|
5
|
+
/** Checks to see if the `schema` and `uiSchema` combination represents an array of files
|
|
6
|
+
*
|
|
7
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
|
+
* @param schema - The schema for which check for array of files flag is desired
|
|
9
|
+
* @param [uiSchema={}] - The UI schema from which to check the widget
|
|
10
|
+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
11
|
+
* @returns - True if schema/uiSchema contains an array of files, otherwise false
|
|
12
|
+
*/
|
|
13
|
+
export default function isFilesArray<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
14
|
+
validator: ValidatorType<T, S, F>,
|
|
15
|
+
schema: S,
|
|
16
|
+
uiSchema: UiSchema<T, S, F> = {},
|
|
17
|
+
rootSchema?: S
|
|
18
|
+
) {
|
|
19
|
+
if (uiSchema[UI_WIDGET_KEY] === 'files') {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (schema.items) {
|
|
23
|
+
const itemsSchema = retrieveSchema<T, S, F>(validator, schema.items as S, rootSchema);
|
|
24
|
+
return itemsSchema.type === 'string' && itemsSchema.format === 'data-url';
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
2
|
+
|
|
3
|
+
import isSelect from './isSelect';
|
|
4
|
+
|
|
5
|
+
/** Checks to see if the `schema` combination represents a multi-select
|
|
6
|
+
*
|
|
7
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
|
+
* @param schema - The schema for which check for a multi-select flag is desired
|
|
9
|
+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
10
|
+
* @returns - True if schema contains a multi-select, otherwise false
|
|
11
|
+
*/
|
|
12
|
+
export default function isMultiSelect<
|
|
13
|
+
T = any,
|
|
14
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
15
|
+
F extends FormContextType = any
|
|
16
|
+
>(validator: ValidatorType<T, S, F>, schema: S, rootSchema?: S) {
|
|
17
|
+
if (!schema.uniqueItems || !schema.items || typeof schema.items === 'boolean') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return isSelect<T, S, F>(validator, schema.items as S, rootSchema);
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import isConstant from '../isConstant';
|
|
2
|
+
import { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types';
|
|
3
|
+
import retrieveSchema from './retrieveSchema';
|
|
4
|
+
|
|
5
|
+
/** Checks to see if the `schema` combination represents a select
|
|
6
|
+
*
|
|
7
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
8
|
+
* @param theSchema - The schema for which check for a select flag is desired
|
|
9
|
+
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
|
|
10
|
+
* @returns - True if schema contains a select, otherwise false
|
|
11
|
+
*/
|
|
12
|
+
export default function isSelect<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
|
|
13
|
+
validator: ValidatorType<T, S, F>,
|
|
14
|
+
theSchema: S,
|
|
15
|
+
rootSchema: S = {} as S
|
|
16
|
+
) {
|
|
17
|
+
const schema = retrieveSchema<T, S, F>(validator, theSchema, rootSchema, undefined);
|
|
18
|
+
const altSchemas = schema.oneOf || schema.anyOf;
|
|
19
|
+
if (Array.isArray(schema.enum)) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(altSchemas)) {
|
|
23
|
+
return altSchemas.every((altSchemas) => typeof altSchemas !== 'boolean' && isConstant(altSchemas));
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import isEmpty from 'lodash/isEmpty';
|
|
2
|
+
|
|
3
|
+
import mergeObjects from '../mergeObjects';
|
|
4
|
+
import { ErrorSchema, FormContextType, RJSFSchema, StrictRJSFSchema, ValidationData, ValidatorType } from '../types';
|
|
5
|
+
|
|
6
|
+
/** Merges the errors in `additionalErrorSchema` into the existing `validationData` by combining the hierarchies in the
|
|
7
|
+
* two `ErrorSchema`s and then appending the error list from the `additionalErrorSchema` obtained by calling
|
|
8
|
+
* `validator.toErrorList()` onto the `errors` in the `validationData`. If no `additionalErrorSchema` is passed, then
|
|
9
|
+
* `validationData` is returned.
|
|
10
|
+
*
|
|
11
|
+
* @param validator - The validator used to convert an ErrorSchema to a list of errors
|
|
12
|
+
* @param validationData - The current `ValidationData` into which to merge the additional errors
|
|
13
|
+
* @param [additionalErrorSchema] - The additional set of errors in an `ErrorSchema`
|
|
14
|
+
* @returns - The `validationData` with the additional errors from `additionalErrorSchema` merged into it, if provided.
|
|
15
|
+
* @deprecated - Use the `validationDataMerge()` function exported from `@rjsf/utils` instead. This function will be
|
|
16
|
+
* removed in the next major release.
|
|
17
|
+
*/
|
|
18
|
+
export default function mergeValidationData<
|
|
19
|
+
T = any,
|
|
20
|
+
S extends StrictRJSFSchema = RJSFSchema,
|
|
21
|
+
F extends FormContextType = any
|
|
22
|
+
>(
|
|
23
|
+
validator: ValidatorType<T, S, F>,
|
|
24
|
+
validationData: ValidationData<T>,
|
|
25
|
+
additionalErrorSchema?: ErrorSchema<T>
|
|
26
|
+
): ValidationData<T> {
|
|
27
|
+
if (!additionalErrorSchema) {
|
|
28
|
+
return validationData;
|
|
29
|
+
}
|
|
30
|
+
const { errors: oldErrors, errorSchema: oldErrorSchema } = validationData;
|
|
31
|
+
let errors = validator.toErrorList(additionalErrorSchema);
|
|
32
|
+
let errorSchema = additionalErrorSchema;
|
|
33
|
+
if (!isEmpty(oldErrorSchema)) {
|
|
34
|
+
errorSchema = mergeObjects(oldErrorSchema, additionalErrorSchema, true) as ErrorSchema<T>;
|
|
35
|
+
errors = [...oldErrors].concat(errors);
|
|
36
|
+
}
|
|
37
|
+
return { errorSchema, errors };
|
|
38
|
+
}
|