Trim strings before storing in db

This commit is contained in:
Sam Becker 2025-02-03 21:29:51 -06:00
parent 428f7ead0b
commit 2673c0dbb0

View File

@ -242,14 +242,10 @@ export const convertFormDataToPhotoDbInsert = (
? Object.fromEntries(formData) as PhotoFormData
: formData;
const tags = convertStringToArray(photoForm.tags) ?? [];
if (photoForm.favorite === 'true') {
tags.push(TAG_FAVS);
}
// Parse FormData:
// - remove server action ID
// - remove empty strings
// - trim strings
Object.keys(photoForm).forEach(key => {
const meta = FORM_METADATA()[key as keyof PhotoFormData];
if (
@ -258,13 +254,20 @@ export const convertFormDataToPhotoDbInsert = (
meta?.excludeFromInsert
) {
delete (photoForm as any)[key];
} else if (typeof (photoForm as any)[key] === 'string') {
(photoForm as any)[key] = (photoForm as any)[key].trim();
}
});
const tags = convertStringToArray(photoForm.tags) ?? [];
if (photoForm.favorite === 'true') {
tags.push(TAG_FAVS);
}
return {
...(photoForm as PhotoFormData & { filmSimulation?: FilmSimulation }),
...!photoForm.id && { id: generateNanoid() },
// Convert form strings to arrays
// Delete array field when empty
tags: tags.length > 0 ? tags : undefined,
// Convert form strings to numbers
aspectRatio: photoForm.aspectRatio