@rjsf/utils 5.0.0-beta.17 → 5.0.0-beta.19

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.d.ts CHANGED
@@ -242,11 +242,11 @@ interface FieldProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends
242
242
  /** The tree of unique ids for every child field */
243
243
  idSchema: IdSchema<T>;
244
244
  /** The data for this field */
245
- formData: T;
245
+ formData?: T;
246
246
  /** The tree of errors for this field and its children */
247
247
  errorSchema?: ErrorSchema<T>;
248
248
  /** The field change event handler; called with the updated form data and an optional `ErrorSchema` */
249
- onChange: (newFormData: T, es?: ErrorSchema<T>, id?: string) => any;
249
+ onChange: (newFormData: T | undefined, es?: ErrorSchema<T>, id?: string) => any;
250
250
  /** The input blur event handler; call it with the field id and value */
251
251
  onBlur: (id: string, value: any) => void;
252
252
  /** The input focus event handler; call it with the field id and value */
@@ -319,7 +319,7 @@ type FieldTemplateProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F exte
319
319
  /** The `formContext` object that was passed to `Form` */
320
320
  formContext?: F;
321
321
  /** The formData for this field */
322
- formData: T;
322
+ formData?: T;
323
323
  /** The value change event handler; Can be called with a new value to change the value for this field */
324
324
  onChange: FieldProps["onChange"];
325
325
  /** The key change event handler; Called when the key associated with a field is changed for an additionalProperty */
@@ -450,7 +450,7 @@ type ArrayFieldTemplateProps<T = any, S extends StrictRJSFSchema = RJSFSchema, F
450
450
  /** The `formContext` object that was passed to Form */
451
451
  formContext?: F;
452
452
  /** The formData for this array */
453
- formData: T;
453
+ formData?: T;
454
454
  /** An array of strings listing all generated error messages from encountered errors for this widget */
455
455
  rawErrors?: string[];
456
456
  /** The `registry` object */
@@ -494,7 +494,7 @@ type ObjectFieldTemplateProps<T = any, S extends StrictRJSFSchema = RJSFSchema,
494
494
  /** An object containing the id for this object & ids for its properties */
495
495
  idSchema: IdSchema<T>;
496
496
  /** The form data for the object */
497
- formData: T;
497
+ formData?: T;
498
498
  /** The `formContext` object that was passed to Form */
499
499
  formContext?: F;
500
500
  /** The `registry` object */
@@ -672,13 +672,21 @@ type UiSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormCo
672
672
  * to look up an implementation from the `fields` list or an actual one-off `Field` component implementation itself
673
673
  */
674
674
  "ui:field"?: Field<T, S, F> | string;
675
+ /** By default, any field that is rendered for an `anyOf`/`oneOf` schema will be wrapped inside the `AnyOfField` or
676
+ * `OneOfField` component. This default behavior may be undesirable if your custom field already handles behavior
677
+ * related to choosing one or more subschemas contained in the `anyOf`/`oneOf` schema.
678
+ * By providing a `true` value for this flag in association with a custom `ui:field`, the wrapped components will be
679
+ * omitted, so just one instance of the custom field will be rendered. If the flag is omitted or set to `false`,
680
+ * your custom field will be wrapped by `AnyOfField`/`OneOfField`.
681
+ */
682
+ "ui:fieldReplacesAnyOrOneOf"?: boolean;
675
683
  /** An object that contains all the potential UI options in a single object */
676
684
  "ui:options"?: UIOptionsType<T, S, F>;
677
685
  };
678
686
  /** A `CustomValidator` function takes in a `formData`, `errors` and `uiSchema` objects and returns the given `errors`
679
687
  * object back, while potentially adding additional messages to the `errors`
680
688
  */
681
- type CustomValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> = (formData: T, errors: FormValidation<T>, uiSchema?: UiSchema<T, S, F>) => FormValidation<T>;
689
+ type CustomValidator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> = (formData: T | undefined, errors: FormValidation<T>, uiSchema?: UiSchema<T, S, F>) => FormValidation<T>;
682
690
  /** An `ErrorTransformer` function will take in a list of `errors` & a `uiSchema` and potentially return a
683
691
  * transformation of those errors in what ever way it deems necessary
684
692
  */
@@ -720,7 +728,7 @@ interface ValidatorType<T = any, S extends StrictRJSFSchema = RJSFSchema, F exte
720
728
  * @param formData - The form data to validate
721
729
  * @param rootSchema - The root schema used to provide $ref resolutions
722
730
  */
723
- isValid(schema: S, formData: T, rootSchema: S): boolean;
731
+ isValid(schema: S, formData: T | undefined, rootSchema: S): boolean;
724
732
  /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
725
733
  * by the playground. Returns the `errors` from the validation
726
734
  *
@@ -771,13 +779,35 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
771
779
  * @returns - True if the label should be displayed or false if it should not
772
780
  */
773
781
  getDisplayLabel(schema: S, uiSchema?: UiSchema<T, S, F>): boolean;
782
+ /** Determines which of the given `options` provided most closely matches the `formData`.
783
+ * Returns the index of the option that is valid and is the closest match, or 0 if there is no match.
784
+ *
785
+ * The closest match is determined using the number of matching properties, and more heavily favors options with
786
+ * matching readOnly, default, or const values.
787
+ *
788
+ * @param formData - The form data associated with the schema
789
+ * @param options - The list of options that can be selected from
790
+ * @param [selectedOption] - The index of the currently selected option, defaulted to -1 if not specified
791
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
792
+ */
793
+ getClosestMatchingOption(formData: T | undefined, options: S[], selectedOption?: number): number;
794
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
795
+ * Always returns the first option if there is nothing that matches.
796
+ *
797
+ * @param formData - The current formData, if any, used to figure out a match
798
+ * @param options - The list of options to find a matching options from
799
+ * @returns - The firstindex of the matched option or 0 if none is available
800
+ */
801
+ getFirstMatchingOption(formData: T | undefined, options: S[]): number;
774
802
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
803
+ * Deprecated, use `getFirstMatchingOption()` instead.
775
804
  *
776
805
  * @param formData - The current formData, if any, onto which to provide any missing defaults
777
806
  * @param options - The list of options to find a matching options from
778
807
  * @returns - The index of the matched option or 0 if none is available
808
+ * @deprecated
779
809
  */
780
- getMatchingOption(formData: T, options: S[]): number;
810
+ getMatchingOption(formData: T | undefined, options: S[]): number;
781
811
  /** Checks to see if the `schema` and `uiSchema` combination represents an array of files
782
812
  *
783
813
  * @param schema - The schema for which check for array of files flag is desired
@@ -816,6 +846,18 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
816
846
  * @returns - The schema having its conditions, additional properties, references and dependencies resolved
817
847
  */
818
848
  retrieveSchema(schema: S, formData?: T): S;
849
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
850
+ * new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
851
+ * nature of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the
852
+ * old schema that are non-existent in the new schema are set to `undefined`.
853
+ *
854
+ * @param [newSchema] - The new schema for which the data is being sanitized
855
+ * @param [oldSchema] - The old schema from which the data originated
856
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
857
+ * @returns - The new form data, with all of the fields uniquely associated with the old schema set
858
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
859
+ */
860
+ sanitizeDataForNewSchema(newSchema?: S, oldSchema?: S, data?: any): T;
819
861
  /** Generates an `IdSchema` object for the `schema`, recursively
820
862
  *
821
863
  * @param schema - The schema for which the display label flag is desired
@@ -1157,11 +1199,11 @@ declare function localToUTC(dateString: string): string | undefined;
1157
1199
  * - when the array is not set in form data, the default is copied over
1158
1200
  * - scalars are overwritten/set by form data
1159
1201
  *
1160
- * @param defaults - The defaults to merge
1161
- * @param formData - The form data into which the defaults will be merged
1202
+ * @param [defaults] - The defaults to merge
1203
+ * @param [formData] - The form data into which the defaults will be merged
1162
1204
  * @returns - The resulting merged form data with defaults
1163
1205
  */
1164
- declare function mergeDefaultsWithFormData<T = any>(defaults: T, formData: T): T;
1206
+ declare function mergeDefaultsWithFormData<T = any>(defaults?: T, formData?: T): T | undefined;
1165
1207
 
1166
1208
  /** Recursively merge deeply nested objects.
1167
1209
  *
@@ -1341,13 +1383,48 @@ declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFS
1341
1383
  */
1342
1384
  declare function getDisplayLabel<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, uiSchema?: UiSchema<T, S, F>, rootSchema?: S): boolean;
1343
1385
 
1386
+ /** Determines which of the given `options` provided most closely matches the `formData`. Using
1387
+ * `getFirstMatchingOption()` to match two schemas that differ only by the readOnly, default or const value of a field
1388
+ * based on the `formData` and returns 0 when there is no match. Rather than passing in all the `options` at once to
1389
+ * this utility, instead an array of valid option indexes is created by iterating over the list of options, call
1390
+ * `getFirstMatchingOptions` with a list of one junk option and one good option, seeing if the good option is considered
1391
+ * matched.
1392
+ *
1393
+ * Once the list of valid indexes is created, if there is only one valid index, just return it. Otherwise, if there are
1394
+ * no valid indexes, then fill the valid indexes array with the indexes of all the options. Next, the index of the
1395
+ * option with the highest score is determined by iterating over the list of valid options, calling
1396
+ * `calculateIndexScore()` on each, comparing it against the current best score, and returning the index of the one that
1397
+ * eventually has the best score.
1398
+ *
1399
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1400
+ * @param rootSchema - The root JSON schema of the entire form
1401
+ * @param formData - The form data associated with the schema
1402
+ * @param options - The list of options that can be selected from
1403
+ * @param [selectedOption=-1] - The index of the currently selected option, defaulted to -1 if not specified
1404
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
1405
+ */
1406
+ declare function getClosestMatchingOption<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rootSchema: S, formData: T | undefined, options: S[], selectedOption?: number): number;
1407
+
1408
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
1409
+ * Always returns the first option if there is nothing that matches.
1410
+ *
1411
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1412
+ * @param formData - The current formData, if any, used to figure out a match
1413
+ * @param options - The list of options to find a matching options from
1414
+ * @param rootSchema - The root schema, used to primarily to look up `$ref`s
1415
+ * @returns - The index of the first matched option or 0 if none is available
1416
+ */
1417
+ declare function getFirstMatchingOption<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, formData: T | undefined, options: S[], rootSchema: S): number;
1418
+
1344
1419
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
1420
+ * Deprecated, use `getFirstMatchingOption()` instead.
1345
1421
  *
1346
1422
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1347
1423
  * @param formData - The current formData, if any, used to figure out a match
1348
1424
  * @param options - The list of options to find a matching options from
1349
1425
  * @param rootSchema - The root schema, used to primarily to look up `$ref`s
1350
1426
  * @returns - The index of the matched option or 0 if none is available
1427
+ * @deprecated
1351
1428
  */
1352
1429
  declare function getMatchingOption<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, formData: T | undefined, options: S[], rootSchema: S): number;
1353
1430
 
@@ -1403,6 +1480,55 @@ declare function mergeValidationData<T = any, S extends StrictRJSFSchema = RJSFS
1403
1480
  */
1404
1481
  declare function retrieveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema?: S, rawFormData?: T): S;
1405
1482
 
1483
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the new
1484
+ * schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the nature
1485
+ * of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the old schema
1486
+ * that are non-existent in the new schema are set to `undefined`. The data sanitization process has the following flow:
1487
+ *
1488
+ * - If the new schema is an object that contains a `properties` object then:
1489
+ * - Create a `removeOldSchemaData` object, setting each key in the `oldSchema.properties` having `data` to undefined
1490
+ * - Create an empty `nestedData` object for use in the key filtering below:
1491
+ * - Iterate over each key in the `newSchema.properties` as follows:
1492
+ * - Get the `formValue` of the key from the `data`
1493
+ * - Get the `oldKeySchema` and `newKeyedSchema` for the key, defaulting to `{}` when it doesn't exist
1494
+ * - Retrieve the schema for any refs within each `oldKeySchema` and/or `newKeySchema`
1495
+ * - Get the types of the old and new keyed schemas and if the old doesn't exist or the old & new are the same then:
1496
+ * - If `removeOldSchemaData` has an entry for the key, delete it since the new schema has the same property
1497
+ * - If type of the key in the new schema is `object`:
1498
+ * - Store the value from the recursive `sanitizeDataForNewSchema` call in `nestedData[key]`
1499
+ * - Otherwise, check for default or const values:
1500
+ * - Get the old and new `default` values from the schema and check:
1501
+ * - If the new `default` value does not match the form value:
1502
+ * - If the old `default` value DOES match the form value, then:
1503
+ * - Replace `removeOldSchemaData[key]` with the new `default`
1504
+ * - Otherwise, if the new schema is `readOnly` then replace `removeOldSchemaData[key]` with undefined
1505
+ * - Get the old and new `const` values from the schema and check:
1506
+ * - If the new `const` value does not match the form value:
1507
+ * - If the old `const` value DOES match the form value, then:
1508
+ * - Replace `removeOldSchemaData[key]` with the new `const`
1509
+ * - Otherwise, replace `removeOldSchemaData[key]` with undefined
1510
+ * - Once all keys have been processed, return an object built as follows:
1511
+ * - `{ ...removeOldSchemaData, ...nestedData, ...pick(data, keysToKeep) }`
1512
+ * - If the new and old schema types are array and the `data` is an array then:
1513
+ * - If the type of the old and new schema `items` are a non-array objects:
1514
+ * - Retrieve the schema for any refs within each `oldKeySchema.items` and/or `newKeySchema.items`
1515
+ * - If the `type`s of both items are the same (or the old does not have a type):
1516
+ * - If the type is "object", then:
1517
+ * - For each element in the `data` recursively sanitize the data, stopping at `maxItems` if specified
1518
+ * - Otherwise, just return the `data` removing any values after `maxItems` if it is set
1519
+ * - If the type of the old and new schema `items` are booleans of the same value, return `data` as is
1520
+ * - Otherwise return `undefined`
1521
+ *
1522
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1523
+ * @param rootSchema - The root JSON schema of the entire form
1524
+ * @param [newSchema] - The new schema for which the data is being sanitized
1525
+ * @param [oldSchema] - The old schema from which the data originated
1526
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
1527
+ * @returns - The new form data, with all the fields uniquely associated with the old schema set
1528
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
1529
+ */
1530
+ declare function sanitizeDataForNewSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rootSchema: S, newSchema?: S, oldSchema?: S, data?: any): T;
1531
+
1406
1532
  /** Generates an `IdSchema` object for the `schema`, recursively
1407
1533
  *
1408
1534
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
@@ -1427,4 +1553,4 @@ declare function toIdSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F
1427
1553
  */
1428
1554
  declare function toPathSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, name?: string, rootSchema?: S, formData?: T): PathSchema<T>;
1429
1555
 
1430
- export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, ArrayFieldDescriptionProps, ArrayFieldTemplateItemType, ArrayFieldTemplateProps, ArrayFieldTitleProps, CONST_KEY, CustomValidator, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, DateObject, DescriptionFieldProps, ENUM_KEY, ERRORS_KEY, EnumOptionsType, ErrorListProps, ErrorSchema, ErrorSchemaBuilder, ErrorTransformer, Field, FieldError, FieldErrorProps, FieldErrors, FieldHelpProps, FieldId, FieldPath, FieldProps, FieldTemplateProps, FieldValidation, FormContextType, FormValidation, GenericObjectType, ID_KEY, ITEMS_KEY, IconButtonProps, IdSchema, InputPropsType, NAME_KEY, ONE_OF_KEY, ObjectFieldTemplatePropertyType, ObjectFieldTemplateProps, PROPERTIES_KEY, PathSchema, REF_KEY, REQUIRED_KEY, RJSFSchema, RJSFValidationError, RJSF_ADDITONAL_PROPERTIES_FLAG, RangeSpecType, Registry, RegistryFieldsType, RegistryWidgetsType, SUBMIT_BTN_OPTIONS_KEY, SchemaUtilsType, StrictRJSFSchema, SubmitButtonProps, TemplatesType, TitleFieldProps, UIOptionsType, UISchemaSubmitButtonOptions, UI_FIELD_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, UiSchema, UnsupportedFieldProps, ValidationData, ValidatorType, Widget, WidgetProps, WrapIfAdditionalTemplateProps, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, enumOptionsDeselectValue, enumOptionsSelectValue, errorId, examplesId, findSchemaDefinition, getDefaultFormState, getDisplayLabel, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, processSelectValue, rangeSpec, retrieveSchema, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toIdSchema, toPathSchema, utcToLocal };
1556
+ export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, ArrayFieldDescriptionProps, ArrayFieldTemplateItemType, ArrayFieldTemplateProps, ArrayFieldTitleProps, CONST_KEY, CustomValidator, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, DateObject, DescriptionFieldProps, ENUM_KEY, ERRORS_KEY, EnumOptionsType, ErrorListProps, ErrorSchema, ErrorSchemaBuilder, ErrorTransformer, Field, FieldError, FieldErrorProps, FieldErrors, FieldHelpProps, FieldId, FieldPath, FieldProps, FieldTemplateProps, FieldValidation, FormContextType, FormValidation, GenericObjectType, ID_KEY, ITEMS_KEY, IconButtonProps, IdSchema, InputPropsType, NAME_KEY, ONE_OF_KEY, ObjectFieldTemplatePropertyType, ObjectFieldTemplateProps, PROPERTIES_KEY, PathSchema, REF_KEY, REQUIRED_KEY, RJSFSchema, RJSFValidationError, RJSF_ADDITONAL_PROPERTIES_FLAG, RangeSpecType, Registry, RegistryFieldsType, RegistryWidgetsType, SUBMIT_BTN_OPTIONS_KEY, SchemaUtilsType, StrictRJSFSchema, SubmitButtonProps, TemplatesType, TitleFieldProps, UIOptionsType, UISchemaSubmitButtonOptions, UI_FIELD_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, UiSchema, UnsupportedFieldProps, ValidationData, ValidatorType, Widget, WidgetProps, WrapIfAdditionalTemplateProps, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, enumOptionsDeselectValue, enumOptionsSelectValue, errorId, examplesId, findSchemaDefinition, getClosestMatchingOption, getDefaultFormState, getDisplayLabel, getFirstMatchingOption, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, processSelectValue, rangeSpec, retrieveSchema, sanitizeDataForNewSchema, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toIdSchema, toPathSchema, utcToLocal };