diff --git a/packages/core/test/oneOf_test.js b/packages/core/test/oneOf_test.js index b79cd510eb..edf1d7a701 100644 --- a/packages/core/test/oneOf_test.js +++ b/packages/core/test/oneOf_test.js @@ -1243,7 +1243,7 @@ describe("oneOf", () => { sinon.assert.calledWithMatch(onChange.lastCall, { formData: { craftTypes: [ - { keywords: [undefined], title: undefined, daysOfYear: undefined }, + { keywords: [undefined], title: undefined }, ], }, }); diff --git a/packages/utils/src/schema/sanitizeDataForNewSchema.ts b/packages/utils/src/schema/sanitizeDataForNewSchema.ts index 5c9c292faf..66aea8ab6f 100644 --- a/packages/utils/src/schema/sanitizeDataForNewSchema.ts +++ b/packages/utils/src/schema/sanitizeDataForNewSchema.ts @@ -16,7 +16,7 @@ const NO_VALUE = Symbol("no Value"); /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the new * schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the nature * of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the old schema - * that are non-existent in the new schema are set to `undefined`. The data sanitization process has the following flow: + * that are non-existent in the new schema are removed. The data sanitization process has the following flow: * * - If the new schema is an object that contains a `properties` object then: * - Create a `removeOldSchemaData` object, setting each key in the `oldSchema.properties` having `data` to undefined @@ -80,6 +80,11 @@ export default function sanitizeDataForNewSchema< if (has(oldSchema, PROPERTIES_KEY)) { const properties = get(oldSchema, PROPERTIES_KEY, {}); Object.keys(properties).forEach((key) => { + // if new schema does not have removeOldSchemaData[key] + if (!has(newSchema, [PROPERTIES_KEY, key])) { + delete removeOldSchemaData[key]; + delete data[key]; + } if (has(data, key)) { removeOldSchemaData[key] = undefined; }