T
TanStack3mo ago
conscious-sapphire

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:
shapeOptions: {
parser: {
timestamptz: (date: string) => new Date(date),
},
shapeOptions: {
parser: {
timestamptz: (date: string) => new Date(date),
},
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
xenial-black3mo 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-sapphire
conscious-sapphireOP3mo 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

Did you find this page helpful?