feng
feng4mo ago

How to use output validation for dates? (Zod, SuperJSON)

I am trying to add output validation with zod, and my defined zod schema. I have currently set up superJson to transform my objects to and from, so that it can also do date transformation. If I add .output(ZodSchemaObject) it throws Expected date, received string This is becuse the ZodSchemaObject has a date in it. However I'm not sure how to make it so that it does the validation BEFORE the SuperJSON has had a chance to transform to string (is this correct?) to send to the client. Has anyone else had any success with this? Only solution now is to remove output validation and risk returning too much data
2 Replies
Nick
Nick4mo ago
Validators DO run before your transformer runs Your data is legitimately wrong
feng
feng4mo ago
Haha thanks, i thought I was going crazy. You were right, the culprit was a json_agg query that wasnt parsing a date data properly into string. Knowing that the validator ran before the transformer helped a lot thanks