Shape Options - Parser: timestamptz
Hi there, I'm hoping to clarify what the expected behavior is when using a collection's update functionality with trpc, when that collection is using a parser for dates like so:
I have an
onUpdate
handler that calls the corresponding trpc.days.update.mutate
endpoint. Just before that call the day object has Date fields, not strings, but I am getting an error from zod validation complaining about recieving strings, not Dates. The schema it's checking against is generated by running
export const updateDaySchema = createUpdateSchema(daysTable)
which is the pattern the tanstack-db-web-starter
repo follows as well
thanks in advance for any advice!1 Reply
absent-sapphire•1h ago
trpc stringifies dates so they'll get sent via json
if using zod you can do something like:
const dateCoercion = z.preprocess((val) => {
if (typeof val ===
string
|| typeof val === number
) {
return new Date(val)
}
return val
}, z.date())
export const insertIngredientsSchema = createInsertSchema(ingredients, {
expiration_date: dateCoercion,
created_at: dateCoercion.optional(),
updated_at: dateCoercion.optional(),
})