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!2 Replies
xenial-black•3mo 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(),
})conscious-sapphireOP•3mo ago
thanks, that makes sense! I worked around it for now by just omitting those fields from the update schemas as well, which I was already doing for the create schemas, but it's nice to know that's an option too