Skip to content

Commit

Permalink
fix(sanitize): do not delete props on root data object
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jun 14, 2021
1 parent 41d8ee6 commit 60358cd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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]
}
}
Expand All @@ -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
}

0 comments on commit 60358cd

Please # to comment.