Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sanitizeDataForNewSchema.ts #3443

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/test/oneOf_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ describe("oneOf", () => {
sinon.assert.calledWithMatch(onChange.lastCall, {
formData: {
craftTypes: [
{ keywords: [undefined], title: undefined, daysOfYear: undefined },
{ keywords: [undefined], title: undefined },
],
},
});
Expand Down
7 changes: 6 additions & 1 deletion packages/utils/src/schema/sanitizeDataForNewSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down