fix: clean undefined values before posting form

This commit is contained in:
Nicolas Meienberger
2025-10-17 21:01:04 +02:00
parent 8af0bac63b
commit 65a7f436fe
7 changed files with 47 additions and 29 deletions

View File

@@ -0,0 +1,14 @@
export function deepClean<T>(obj: T): T {
if (Array.isArray(obj)) {
return obj.map(deepClean).filter((v) => v !== undefined && v !== null) as T;
}
if (obj && typeof obj === "object") {
return Object.entries(obj).reduce((acc, [key, value]) => {
const cleaned = deepClean(value);
if (cleaned !== undefined) acc[key as keyof T] = cleaned;
return acc;
}, {} as T);
}
return obj;
}