@rjsf/core 5.23.2 → 5.24.1

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/core.umd.js CHANGED
@@ -3063,10 +3063,12 @@
3063
3063
  */
3064
3064
  this.onChange = (formData, newErrorSchema, id) => {
3065
3065
  const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
3066
- const { schemaUtils, schema, retrievedSchema } = this.state;
3066
+ const { schemaUtils, schema } = this.state;
3067
+ let retrievedSchema = this.state.retrievedSchema;
3067
3068
  if (utils.isObject(formData) || Array.isArray(formData)) {
3068
- const newState = this.getStateFromProps(this.props, formData, retrievedSchema);
3069
+ const newState = this.getStateFromProps(this.props, formData);
3069
3070
  formData = newState.formData;
3071
+ retrievedSchema = newState.retrievedSchema;
3070
3072
  }
3071
3073
  const mustValidate = !noValidate && liveValidate;
3072
3074
  let state = { formData, schema };
@@ -3283,8 +3285,9 @@
3283
3285
  */
3284
3286
  getSnapshotBeforeUpdate(prevProps, prevState) {
3285
3287
  if (!utils.deepEquals(this.props, prevProps)) {
3288
+ const formDataChangedFields = utils.getChangedFields(this.props.formData, prevProps.formData);
3286
3289
  const isSchemaChanged = !utils.deepEquals(prevProps.schema, this.props.schema);
3287
- const isFormDataChanged = !utils.deepEquals(prevProps.formData, this.props.formData);
3290
+ const isFormDataChanged = formDataChangedFields.length > 0 || !utils.deepEquals(prevProps.formData, this.props.formData);
3288
3291
  const nextState = this.getStateFromProps(
3289
3292
  this.props,
3290
3293
  this.props.formData,
@@ -3292,7 +3295,8 @@
3292
3295
  // Or if the `formData` changes, for example in the case of a schema with dependencies that need to
3293
3296
  // match one of the subSchemas, the retrieved schema must be updated.
3294
3297
  isSchemaChanged || isFormDataChanged ? void 0 : this.state.retrievedSchema,
3295
- isSchemaChanged
3298
+ isSchemaChanged,
3299
+ formDataChangedFields
3296
3300
  );
3297
3301
  const shouldUpdate = !utils.deepEquals(nextState, prevState);
3298
3302
  return { nextState, shouldUpdate };
@@ -3331,9 +3335,10 @@
3331
3335
  * @param inputFormData - The new or current data for the `Form`
3332
3336
  * @param retrievedSchema - An expanded schema, if not provided, it will be retrieved from the `schema` and `formData`.
3333
3337
  * @param isSchemaChanged - A flag indicating whether the schema has changed.
3338
+ * @param formDataChangedFields - The changed fields of `formData`
3334
3339
  * @returns - The new state for the `Form`
3335
3340
  */
3336
- getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false) {
3341
+ getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false, formDataChangedFields = []) {
3337
3342
  const state = this.state || {};
3338
3343
  const schema = "schema" in props ? props.schema : this.props.schema;
3339
3344
  const uiSchema = ("uiSchema" in props ? props.uiSchema : this.props.uiSchema) || {};
@@ -3358,7 +3363,9 @@
3358
3363
  );
3359
3364
  }
3360
3365
  const formData = schemaUtils.getDefaultFormState(schema, inputFormData);
3361
- const _retrievedSchema = retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData);
3366
+ const _retrievedSchema = this.updateRetrievedSchema(
3367
+ retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData)
3368
+ );
3362
3369
  const getCurrentErrors = () => {
3363
3370
  if (props.noValidate || isSchemaChanged) {
3364
3371
  return { errors: [], errorSchema: {} };
@@ -3395,6 +3402,17 @@
3395
3402
  const currentErrors = getCurrentErrors();
3396
3403
  errors = currentErrors.errors;
3397
3404
  errorSchema = currentErrors.errorSchema;
3405
+ if (formDataChangedFields.length > 0) {
3406
+ const newErrorSchema = formDataChangedFields.reduce((acc, key) => {
3407
+ acc[key] = void 0;
3408
+ return acc;
3409
+ }, {});
3410
+ errorSchema = schemaValidationErrorSchema = utils.mergeObjects(
3411
+ currentErrors.errorSchema,
3412
+ newErrorSchema,
3413
+ "preventDuplicates"
3414
+ );
3415
+ }
3398
3416
  }
3399
3417
  if (props.extraErrors) {
3400
3418
  const merged = utils.validationDataMerge({ errorSchema, errors }, props.extraErrors);
@@ -3488,6 +3506,19 @@
3488
3506
  };
3489
3507
  return filterNilOrEmptyErrors(filteredErrors);
3490
3508
  }
3509
+ /**
3510
+ * If the retrievedSchema has changed the new retrievedSchema is returned.
3511
+ * Otherwise, the old retrievedSchema is returned to persist reference.
3512
+ * - This ensures that AJV retrieves the schema from the cache when it has not changed,
3513
+ * avoiding the performance cost of recompiling the schema.
3514
+ *
3515
+ * @param retrievedSchema The new retrieved schema.
3516
+ * @returns The new retrieved schema if it has changed, else the old retrieved schema.
3517
+ */
3518
+ updateRetrievedSchema(retrievedSchema) {
3519
+ const isTheSame = utils.deepEquals(retrievedSchema, this.state?.retrievedSchema);
3520
+ return isTheSame ? this.state.retrievedSchema : retrievedSchema;
3521
+ }
3491
3522
  /** Returns the registry for the form */
3492
3523
  getRegistry() {
3493
3524
  const { translateString: customTranslateString, uiSchema = {} } = this.props;
package/dist/index.esm.js CHANGED
@@ -3,6 +3,7 @@ import { Component as Component5, createRef } from "react";
3
3
  import {
4
4
  createSchemaUtils,
5
5
  deepEquals as deepEquals3,
6
+ getChangedFields,
6
7
  getTemplate as getTemplate20,
7
8
  getUiOptions as getUiOptions12,
8
9
  isObject as isObject5,
@@ -3414,10 +3415,12 @@ var Form = class extends Component5 {
3414
3415
  */
3415
3416
  this.onChange = (formData, newErrorSchema, id) => {
3416
3417
  const { extraErrors, omitExtraData, liveOmit, noValidate, liveValidate, onChange } = this.props;
3417
- const { schemaUtils, schema, retrievedSchema } = this.state;
3418
+ const { schemaUtils, schema } = this.state;
3419
+ let retrievedSchema = this.state.retrievedSchema;
3418
3420
  if (isObject5(formData) || Array.isArray(formData)) {
3419
- const newState = this.getStateFromProps(this.props, formData, retrievedSchema);
3421
+ const newState = this.getStateFromProps(this.props, formData);
3420
3422
  formData = newState.formData;
3423
+ retrievedSchema = newState.retrievedSchema;
3421
3424
  }
3422
3425
  const mustValidate = !noValidate && liveValidate;
3423
3426
  let state = { formData, schema };
@@ -3634,8 +3637,9 @@ var Form = class extends Component5 {
3634
3637
  */
3635
3638
  getSnapshotBeforeUpdate(prevProps, prevState) {
3636
3639
  if (!deepEquals3(this.props, prevProps)) {
3640
+ const formDataChangedFields = getChangedFields(this.props.formData, prevProps.formData);
3637
3641
  const isSchemaChanged = !deepEquals3(prevProps.schema, this.props.schema);
3638
- const isFormDataChanged = !deepEquals3(prevProps.formData, this.props.formData);
3642
+ const isFormDataChanged = formDataChangedFields.length > 0 || !deepEquals3(prevProps.formData, this.props.formData);
3639
3643
  const nextState = this.getStateFromProps(
3640
3644
  this.props,
3641
3645
  this.props.formData,
@@ -3643,7 +3647,8 @@ var Form = class extends Component5 {
3643
3647
  // Or if the `formData` changes, for example in the case of a schema with dependencies that need to
3644
3648
  // match one of the subSchemas, the retrieved schema must be updated.
3645
3649
  isSchemaChanged || isFormDataChanged ? void 0 : this.state.retrievedSchema,
3646
- isSchemaChanged
3650
+ isSchemaChanged,
3651
+ formDataChangedFields
3647
3652
  );
3648
3653
  const shouldUpdate = !deepEquals3(nextState, prevState);
3649
3654
  return { nextState, shouldUpdate };
@@ -3682,9 +3687,10 @@ var Form = class extends Component5 {
3682
3687
  * @param inputFormData - The new or current data for the `Form`
3683
3688
  * @param retrievedSchema - An expanded schema, if not provided, it will be retrieved from the `schema` and `formData`.
3684
3689
  * @param isSchemaChanged - A flag indicating whether the schema has changed.
3690
+ * @param formDataChangedFields - The changed fields of `formData`
3685
3691
  * @returns - The new state for the `Form`
3686
3692
  */
3687
- getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false) {
3693
+ getStateFromProps(props, inputFormData, retrievedSchema, isSchemaChanged = false, formDataChangedFields = []) {
3688
3694
  const state = this.state || {};
3689
3695
  const schema = "schema" in props ? props.schema : this.props.schema;
3690
3696
  const uiSchema = ("uiSchema" in props ? props.uiSchema : this.props.uiSchema) || {};
@@ -3709,7 +3715,9 @@ var Form = class extends Component5 {
3709
3715
  );
3710
3716
  }
3711
3717
  const formData = schemaUtils.getDefaultFormState(schema, inputFormData);
3712
- const _retrievedSchema = retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData);
3718
+ const _retrievedSchema = this.updateRetrievedSchema(
3719
+ retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData)
3720
+ );
3713
3721
  const getCurrentErrors = () => {
3714
3722
  if (props.noValidate || isSchemaChanged) {
3715
3723
  return { errors: [], errorSchema: {} };
@@ -3746,6 +3754,17 @@ var Form = class extends Component5 {
3746
3754
  const currentErrors = getCurrentErrors();
3747
3755
  errors = currentErrors.errors;
3748
3756
  errorSchema = currentErrors.errorSchema;
3757
+ if (formDataChangedFields.length > 0) {
3758
+ const newErrorSchema = formDataChangedFields.reduce((acc, key) => {
3759
+ acc[key] = void 0;
3760
+ return acc;
3761
+ }, {});
3762
+ errorSchema = schemaValidationErrorSchema = mergeObjects2(
3763
+ currentErrors.errorSchema,
3764
+ newErrorSchema,
3765
+ "preventDuplicates"
3766
+ );
3767
+ }
3749
3768
  }
3750
3769
  if (props.extraErrors) {
3751
3770
  const merged = validationDataMerge({ errorSchema, errors }, props.extraErrors);
@@ -3839,6 +3858,19 @@ var Form = class extends Component5 {
3839
3858
  };
3840
3859
  return filterNilOrEmptyErrors(filteredErrors);
3841
3860
  }
3861
+ /**
3862
+ * If the retrievedSchema has changed the new retrievedSchema is returned.
3863
+ * Otherwise, the old retrievedSchema is returned to persist reference.
3864
+ * - This ensures that AJV retrieves the schema from the cache when it has not changed,
3865
+ * avoiding the performance cost of recompiling the schema.
3866
+ *
3867
+ * @param retrievedSchema The new retrieved schema.
3868
+ * @returns The new retrieved schema if it has changed, else the old retrieved schema.
3869
+ */
3870
+ updateRetrievedSchema(retrievedSchema) {
3871
+ const isTheSame = deepEquals3(retrievedSchema, this.state?.retrievedSchema);
3872
+ return isTheSame ? this.state.retrievedSchema : retrievedSchema;
3873
+ }
3842
3874
  /** Returns the registry for the form */
3843
3875
  getRegistry() {
3844
3876
  const { translateString: customTranslateString, uiSchema = {} } = this.props;