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