diff --git a/packages/core/test/oneOf.test.jsx b/packages/core/test/oneOf.test.jsx index 3fa1081c1c..35b3aeb0ee 100644 --- a/packages/core/test/oneOf.test.jsx +++ b/packages/core/test/oneOf.test.jsx @@ -1228,7 +1228,7 @@ describe('oneOf', () => { sinon.assert.calledWithMatch(onChange.lastCall, { formData: { - craftTypes: [{ keywords: [undefined], title: undefined, daysOfYear: undefined }], + craftTypes: [{ keywords: [undefined], title: undefined }], }, }); }); diff --git a/packages/utils/src/schema/sanitizeDataForNewSchema.ts b/packages/utils/src/schema/sanitizeDataForNewSchema.ts index 2ecba355e2..aa22845576 100644 --- a/packages/utils/src/schema/sanitizeDataForNewSchema.ts +++ b/packages/utils/src/schema/sanitizeDataForNewSchema.ts @@ -1,5 +1,7 @@ import get from 'lodash/get'; import has from 'lodash/has'; +import omitBy from 'lodash/omitBy'; +import isUndefined from 'lodash/isUndefined'; import { FormContextType, GenericObjectType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types'; import { PROPERTIES_KEY, REF_KEY } from '../constants'; @@ -137,8 +139,14 @@ export default function sanitizeDataForNewSchema< }); newFormData = { - ...data, - ...removeOldSchemaData, + // remove fields from old schema and then sanitize undefined + ...omitBy( + { + ...data, + ...removeOldSchemaData, + }, + isUndefined + ), ...nestedData, }; // First apply removing the old schema data, then apply the nested data, then apply the old data keys to keep