-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
3.20: Input type of a schema with a coercion #1760
Comments
I have confirmed this bug. Not exactly sure how to fix it though. Any ideas? |
Well... in order to preserve the backward compatibility, the following idea comes to my mind. Consider the current declaration: class ZodNumber extends ZodType<number, ZodNumberDef> { }
// input type is equal to the output one by default (number) where interface ZodNumberDef extends ZodTypeDef {
checks: ZodNumberCheck[];
typeName: ZodFirstPartyTypeKind.ZodNumber;
coerce: boolean;
} We need to make interface ZodNumberDef<Coerce extends boolean = false> extends ZodTypeDef {
checks: ZodNumberCheck[];
typeName: ZodFirstPartyTypeKind.ZodNumber;
coerce: Coerce;
}
class ZodNumber<Coerce extends boolean = false>
extends ZodType<
number, // output type
ZodNumberDef<Coerce>,
Coerce extends true ? any : number // conditional input type
> { } It's only the beginning, for demonstrating a possible way of fixing this. |
@JacobWeisenburger , I made a fix #1793 — please review. |
…nt." This reverts commit 031a876.
I rewrote @RobinTail's fix to not be a breaking change by introducing new coerced types (eg: The @colinhacks @RobinTail @JacobWeisenburger Does this sound like an acceptable solution? edit: Well, I guess one could argue it's still a breaking change, since |
@aleclarson I took a quick look and having types with the actual expected coercion inputs seems reasonable to me, FWIW. It forces users to be explicit about disregarding the expected input types for those that need it (bigint and date maybe). I'm not sure what would happen in re: to the coercion types sometimes not being assignable back to the zod "main" types, since I haven't worked in userland code that does that extensively. That would be a concern I think. |
Didn't you forget to cover the case like |
@RobinTail ah yes, I did 😞 |
That's why I went with the approach in my PR. I believe it's backward compatible while having defaults where type params are introduced. |
This was a tradeoff with adding the coercion logic directly into the respective primitive schema types (ZodString, etc.). I could have introduced a I don't like the idea of adding any generics to const r = z.any().transform((val) => String(val));
type In = z.input<typeof r>;
type Out = z.output<typeof r>; |
Perhaps my approach is a good compromise then? …even though |
That seems messy to me and not worth the headache with assignability and whatnot. I was aware of this issue before selecting the current approach and I still think its the best option from a set of imperfect options. The true solution is to rewrite Zod to avoid classes/inheritance but in the meantime, this is the best tradeoff. I'm gonna close, thanks to everyone for the ideas and discussion. |
|
Then why do you close the issue? @colinhacks It is a confirmed bug that remains, right? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I believe there is a confusion with the way how the new
coerce
feature works together with the inferring of the input type.Here is the example with the
ZodString
havingcoerce
enabled:Under the hood, as per my understanding, the schema actually accepts
any
type of the input, making aString
out of them.However, inferring the input type of such schema returns
string
, so thecoerce
feature is not taken in account on the typescript level.I believe, the
z.coerce.string()
should createZodType<string, ZodStringDef, any>
(input type —any
, not equal to the output type —string
).what do you think, @colinhacks ?
The text was updated successfully, but these errors were encountered: