This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
react-component-form/src/utils/handleOptionalEmptyStringToNull.ts

20 lines
445 B
TypeScript

import type { Schema } from "../hooks/useForm"
export const handleOptionalEmptyStringToNull = <K extends Schema>(
object: K,
required: string[] = [],
): K => {
return Object.fromEntries(
Object.entries(object).map(([key, value]) => {
if (
typeof value === "string" &&
value.length === 0 &&
!required.includes(key)
) {
return [key, null]
}
return [key, value]
}),
) as K
}