From 60358cddda15fe771eb0046f5fd682e6bb87d39f Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Mon, 14 Jun 2021 19:03:16 -0300 Subject: [PATCH] fix(sanitize): do not delete props on root data object --- src/lib/sanitize.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/sanitize.js b/src/lib/sanitize.js index b71eb8a0..5261be02 100644 --- a/src/lib/sanitize.js +++ b/src/lib/sanitize.js @@ -19,12 +19,14 @@ const sanitizeArray = items => { } } -const sanitize = (obj, prop) => { +const sanitize = (obj, prop, isRootObject = false) => { if (typeof obj === 'object' && obj !== null) { switch (obj[prop]) { case null: case '': - delete obj[prop] + if (!isRootObject) { + delete obj[prop] + } break default: if (hasNextObject(obj[prop])) { @@ -36,7 +38,7 @@ const sanitize = (obj, prop) => { sanitizeArray(obj[prop]) obj[prop] = obj[prop].filter(item => isEmptyObject(item) === false) } - if (isEmptyObject(obj[prop])) { + if (!isRootObject && isEmptyObject(obj[prop])) { delete obj[prop] } } @@ -46,7 +48,7 @@ const sanitize = (obj, prop) => { export default formData => { const newFormData = cloneDeep(formData) for (const key of Object.keys(newFormData)) { - sanitize(newFormData, key) + sanitize(newFormData, key, true) } return newFormData }