colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
31.58k stars 1.09k forks source link

coerce.date() is not respected by safeParse #3502

Open hongquan opened 1 month ago

hongquan commented 1 month ago

We have a schema:

z.object({
  created_at: z.string().datetime().pipe(z.coerce.date())
})

and data:

{ created_at: '2024-05-17T01:08:19.550454Z' }

If call .parse, the created_at field is converted to Date, which is correct, but if call .safeParse, the created_at is still a string.

hongquan commented 1 month ago

Seems to be difference across build.

If I use zod directly in my project, safeParse and parse give the same result. But if I use indirectly via some 3rd-party lib, like https://github.com/muzychenka/vue3-websocket, then safeParse doesn't respect coerce, transform....