Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coerce and transform not having proper types in output #759

Closed
teaishealthy opened this issue Dec 24, 2022 · 8 comments · Fixed by #1116
Closed

coerce and transform not having proper types in output #759

teaishealthy opened this issue Dec 24, 2022 · 8 comments · Fixed by #1116
Labels
external bug it's a bug, but in a dependency

Comments

@teaishealthy
Copy link

teaishealthy commented Dec 24, 2022

Typescript reports an error when using z.coerce or .transform in output.

Additionally generating an OpenAPI spec fails at runtime when using z.coere.bigint in input

Minimal reproducible example

import { defaultEndpointsFactory, z } from "express-zod-api";

const testEndpoint = defaultEndpointsFactory.build({
  method: "get",
  input: z.object({}),
  output: z.object({
    name: z.coerce.string(),
    id: z.coerce.bigint().
  }),
  handler: async () => {
    return { name: 1, id: "1" };  // <-  Type 'number' is not assignable to type 'string'
  },
});

Edit: Reading a bit through issues and discussions the behavior for output transforms seems intended?
Anyhow generating a OpenAPI spec still fails with z.coerce.bigint in input.

@RobinTail
Copy link
Owner

Well-well-well...
You're assigning a number to the property having schema z.coerce.string().
Typescript validates the handler's returns using z.input< typeof z.coerce.string() >, and it seems to be just string, so you're getting an error.
I'd say, that this is more likely the zod's issue of the way the schemas with coercion being inferred.

But I'll check it more precisely tomorrow.
Thank you for letting me know, @teaishealthy

@teaishealthy
Copy link
Author

teaishealthy commented Dec 26, 2022

I think it's not about bigint, it's about your name property, which is string, but you're assigning a number to it,

I don't think my provided example was directly related to the schema issue, I just found it while tinkering around. I can try to provide an example later this day.

@RobinTail
Copy link
Owner

I don't think my provided example was directly related to the schema issue, I just found it while tinkering around. I can try to provide an example later this day.

I checked the example in details — it's fine and it really reveals the confusion with the new coercion feature in Zod.
The problem is that coerce feature is not reflected on the type level, it only works in runtime.

I posted a more generic issue in Zod repo with a question to Colin. Maybe they would explain should it be fixed or it's somehow intentional.

colinhacks/zod#1760

Anyway, thank you @teaishealthy

@RobinTail RobinTail added the unclear Is it a bug or a feature? label Dec 26, 2022
@RobinTail
Copy link
Owner

The issue confirmed as a bug of zod, @teaishealthy
colinhacks/zod#1760 (comment)

@RobinTail RobinTail added external bug it's a bug, but in a dependency and removed unclear Is it a bug or a feature? labels Jan 5, 2023
@RobinTail
Copy link
Owner

I made a PR that should fix the issue in zod, but I'm still waiting for its approval.
colinhacks/zod#1793

@RobinTail
Copy link
Owner

RobinTail commented Jan 30, 2023

Bad news, @teaishealthy .
The author of zod confirmed the bug, but does not want to fix it (now), since according to the author's vision it requires a complete refactoring of zod to be less messy.
¯\_(ツ)_/¯
Not sure yet what I can do about it...

@teaishealthy
Copy link
Author

That's really unfortunate 😕 although I should be able to get along without .coere for the time being

Anyway thank you for your efforts on this issue 👍

@RobinTail
Copy link
Owner

Regarding z.coerce.bigint() — this is also third party bug:
colinhacks/zod#1911

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external bug it's a bug, but in a dependency
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants