Skip to content

Commit

Permalink
fix: sanitise form data before submission: strip nullish values
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon committed Jan 3, 2021
1 parent 70d7304 commit 17bdb4a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/components/DialogDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ const formSchema = yup.object().shape({
originalFilename: yup.string().required('Filename cannot be empty')
})

// Strip keys with empty strings, undefined or null values
const sanitiseFormData = (formData: FormData) => {
return Object.keys(formData).reduce((acc: Record<string, any>, key) => {
if (formData[key] !== '' && formData[key] != null) {
acc[key] = formData[key]
}

return acc
}, {})
}

const DialogDetails: FC<Props> = (props: Props) => {
const {asset, children, id} = props

Expand Down Expand Up @@ -97,13 +108,16 @@ const DialogDetails: FC<Props> = (props: Props) => {
return
}

// Sanitise form data: strip nullish values
const sanitisedFormData = sanitiseFormData(formData)

dispatch(
assetsUpdate(
asset,
// Form data
{
...formData,
originalFilename: `${formData.originalFilename}.${asset.extension}` // Append extension to filename
...sanitisedFormData,
originalFilename: `${sanitisedFormData.originalFilename}.${asset.extension}` // Append extension to filename
},
// Options
{
Expand Down

0 comments on commit 17bdb4a

Please sign in to comment.