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

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 */
@@ -678,7 +678,7 @@ type UiSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormCo
678
678
  /** A `CustomValidator` function takes in a `formData`, `errors` and `uiSchema` objects and returns the given `errors`
679
679
  * object back, while potentially adding additional messages to the `errors`
680
680
  */
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>;
681
+ 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
682
  /** An `ErrorTransformer` function will take in a list of `errors` & a `uiSchema` and potentially return a
683
683
  * transformation of those errors in what ever way it deems necessary
684
684
  */
@@ -720,7 +720,7 @@ interface ValidatorType<T = any, S extends StrictRJSFSchema = RJSFSchema, F exte
720
720
  * @param formData - The form data to validate
721
721
  * @param rootSchema - The root schema used to provide $ref resolutions
722
722
  */
723
- isValid(schema: S, formData: T, rootSchema: S): boolean;
723
+ isValid(schema: S, formData: T | undefined, rootSchema: S): boolean;
724
724
  /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use
725
725
  * by the playground. Returns the `errors` from the validation
726
726
  *
@@ -771,13 +771,35 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
771
771
  * @returns - True if the label should be displayed or false if it should not
772
772
  */
773
773
  getDisplayLabel(schema: S, uiSchema?: UiSchema<T, S, F>): boolean;
774
+ /** Determines which of the given `options` provided most closely matches the `formData`.
775
+ * Returns the index of the option that is valid and is the closest match, or 0 if there is no match.
776
+ *
777
+ * The closest match is determined using the number of matching properties, and more heavily favors options with
778
+ * matching readOnly, default, or const values.
779
+ *
780
+ * @param formData - The form data associated with the schema
781
+ * @param options - The list of options that can be selected from
782
+ * @param [selectedOption] - The index of the currently selected option, defaulted to -1 if not specified
783
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
784
+ */
785
+ getClosestMatchingOption(formData: T | undefined, options: S[], selectedOption?: number): number;
786
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
787
+ * Always returns the first option if there is nothing that matches.
788
+ *
789
+ * @param formData - The current formData, if any, used to figure out a match
790
+ * @param options - The list of options to find a matching options from
791
+ * @returns - The firstindex of the matched option or 0 if none is available
792
+ */
793
+ getFirstMatchingOption(formData: T | undefined, options: S[]): number;
774
794
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
795
+ * Deprecated, use `getFirstMatchingOption()` instead.
775
796
  *
776
797
  * @param formData - The current formData, if any, onto which to provide any missing defaults
777
798
  * @param options - The list of options to find a matching options from
778
799
  * @returns - The index of the matched option or 0 if none is available
800
+ * @deprecated
779
801
  */
780
- getMatchingOption(formData: T, options: S[]): number;
802
+ getMatchingOption(formData: T | undefined, options: S[]): number;
781
803
  /** Checks to see if the `schema` and `uiSchema` combination represents an array of files
782
804
  *
783
805
  * @param schema - The schema for which check for array of files flag is desired
@@ -816,6 +838,18 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
816
838
  * @returns - The schema having its conditions, additional properties, references and dependencies resolved
817
839
  */
818
840
  retrieveSchema(schema: S, formData?: T): S;
841
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
842
+ * new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
843
+ * nature of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the
844
+ * old schema that are non-existent in the new schema are set to `undefined`.
845
+ *
846
+ * @param [newSchema] - The new schema for which the data is being sanitized
847
+ * @param [oldSchema] - The old schema from which the data originated
848
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
849
+ * @returns - The new form data, with all of the fields uniquely associated with the old schema set
850
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
851
+ */
852
+ sanitizeDataForNewSchema(newSchema?: S, oldSchema?: S, data?: any): T;
819
853
  /** Generates an `IdSchema` object for the `schema`, recursively
820
854
  *
821
855
  * @param schema - The schema for which the display label flag is desired
@@ -1157,11 +1191,11 @@ declare function localToUTC(dateString: string): string | undefined;
1157
1191
  * - when the array is not set in form data, the default is copied over
1158
1192
  * - scalars are overwritten/set by form data
1159
1193
  *
1160
- * @param defaults - The defaults to merge
1161
- * @param formData - The form data into which the defaults will be merged
1194
+ * @param [defaults] - The defaults to merge
1195
+ * @param [formData] - The form data into which the defaults will be merged
1162
1196
  * @returns - The resulting merged form data with defaults
1163
1197
  */
1164
- declare function mergeDefaultsWithFormData<T = any>(defaults: T, formData: T): T;
1198
+ declare function mergeDefaultsWithFormData<T = any>(defaults?: T, formData?: T): T | undefined;
1165
1199
 
1166
1200
  /** Recursively merge deeply nested objects.
1167
1201
  *
@@ -1341,13 +1375,48 @@ declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFS
1341
1375
  */
1342
1376
  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
1377
 
1378
+ /** Determines which of the given `options` provided most closely matches the `formData`. Using
1379
+ * `getFirstMatchingOption()` to match two schemas that differ only by the readOnly, default or const value of a field
1380
+ * based on the `formData` and returns 0 when there is no match. Rather than passing in all the `options` at once to
1381
+ * this utility, instead an array of valid option indexes is created by iterating over the list of options, call
1382
+ * `getFirstMatchingOptions` with a list of one junk option and one good option, seeing if the good option is considered
1383
+ * matched.
1384
+ *
1385
+ * Once the list of valid indexes is created, if there is only one valid index, just return it. Otherwise, if there are
1386
+ * no valid indexes, then fill the valid indexes array with the indexes of all the options. Next, the index of the
1387
+ * option with the highest score is determined by iterating over the list of valid options, calling
1388
+ * `calculateIndexScore()` on each, comparing it against the current best score, and returning the index of the one that
1389
+ * eventually has the best score.
1390
+ *
1391
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1392
+ * @param rootSchema - The root JSON schema of the entire form
1393
+ * @param formData - The form data associated with the schema
1394
+ * @param options - The list of options that can be selected from
1395
+ * @param [selectedOption=-1] - The index of the currently selected option, defaulted to -1 if not specified
1396
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
1397
+ */
1398
+ 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;
1399
+
1400
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
1401
+ * Always returns the first option if there is nothing that matches.
1402
+ *
1403
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1404
+ * @param formData - The current formData, if any, used to figure out a match
1405
+ * @param options - The list of options to find a matching options from
1406
+ * @param rootSchema - The root schema, used to primarily to look up `$ref`s
1407
+ * @returns - The index of the first matched option or 0 if none is available
1408
+ */
1409
+ 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;
1410
+
1344
1411
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
1412
+ * Deprecated, use `getFirstMatchingOption()` instead.
1345
1413
  *
1346
1414
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1347
1415
  * @param formData - The current formData, if any, used to figure out a match
1348
1416
  * @param options - The list of options to find a matching options from
1349
1417
  * @param rootSchema - The root schema, used to primarily to look up `$ref`s
1350
1418
  * @returns - The index of the matched option or 0 if none is available
1419
+ * @deprecated
1351
1420
  */
1352
1421
  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
1422
 
@@ -1403,6 +1472,55 @@ declare function mergeValidationData<T = any, S extends StrictRJSFSchema = RJSFS
1403
1472
  */
1404
1473
  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
1474
 
1475
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the new
1476
+ * schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the nature
1477
+ * of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the old schema
1478
+ * that are non-existent in the new schema are set to `undefined`. The data sanitization process has the following flow:
1479
+ *
1480
+ * - If the new schema is an object that contains a `properties` object then:
1481
+ * - Create a `removeOldSchemaData` object, setting each key in the `oldSchema.properties` having `data` to undefined
1482
+ * - Create an empty `nestedData` object for use in the key filtering below:
1483
+ * - Iterate over each key in the `newSchema.properties` as follows:
1484
+ * - Get the `formValue` of the key from the `data`
1485
+ * - Get the `oldKeySchema` and `newKeyedSchema` for the key, defaulting to `{}` when it doesn't exist
1486
+ * - Retrieve the schema for any refs within each `oldKeySchema` and/or `newKeySchema`
1487
+ * - 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:
1488
+ * - If `removeOldSchemaData` has an entry for the key, delete it since the new schema has the same property
1489
+ * - If type of the key in the new schema is `object`:
1490
+ * - Store the value from the recursive `sanitizeDataForNewSchema` call in `nestedData[key]`
1491
+ * - Otherwise, check for default or const values:
1492
+ * - Get the old and new `default` values from the schema and check:
1493
+ * - If the new `default` value does not match the form value:
1494
+ * - If the old `default` value DOES match the form value, then:
1495
+ * - Replace `removeOldSchemaData[key]` with the new `default`
1496
+ * - Otherwise, if the new schema is `readOnly` then replace `removeOldSchemaData[key]` with undefined
1497
+ * - Get the old and new `const` values from the schema and check:
1498
+ * - If the new `const` value does not match the form value:
1499
+ * - If the old `const` value DOES match the form value, then:
1500
+ * - Replace `removeOldSchemaData[key]` with the new `const`
1501
+ * - Otherwise, replace `removeOldSchemaData[key]` with undefined
1502
+ * - Once all keys have been processed, return an object built as follows:
1503
+ * - `{ ...removeOldSchemaData, ...nestedData, ...pick(data, keysToKeep) }`
1504
+ * - If the new and old schema types are array and the `data` is an array then:
1505
+ * - If the type of the old and new schema `items` are a non-array objects:
1506
+ * - Retrieve the schema for any refs within each `oldKeySchema.items` and/or `newKeySchema.items`
1507
+ * - If the `type`s of both items are the same (or the old does not have a type):
1508
+ * - If the type is "object", then:
1509
+ * - For each element in the `data` recursively sanitize the data, stopping at `maxItems` if specified
1510
+ * - Otherwise, just return the `data` removing any values after `maxItems` if it is set
1511
+ * - If the type of the old and new schema `items` are booleans of the same value, return `data` as is
1512
+ * - Otherwise return `undefined`
1513
+ *
1514
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1515
+ * @param rootSchema - The root JSON schema of the entire form
1516
+ * @param [newSchema] - The new schema for which the data is being sanitized
1517
+ * @param [oldSchema] - The old schema from which the data originated
1518
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
1519
+ * @returns - The new form data, with all the fields uniquely associated with the old schema set
1520
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
1521
+ */
1522
+ 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;
1523
+
1406
1524
  /** Generates an `IdSchema` object for the `schema`, recursively
1407
1525
  *
1408
1526
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
@@ -1427,4 +1545,4 @@ declare function toIdSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F
1427
1545
  */
1428
1546
  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
1547
 
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 };
1548
+ 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 };