-
-
Notifications
You must be signed in to change notification settings - Fork 105
Generated Zod types are lost with @@validate #676
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
Comments
I see from colinhacks/zod#2474 that ZodEffects is an expected change, but the loss of type information I believe is the bug, since ideally instead of having to redefine the interface we would be able to use |
Hi @tlancina , thanks for bringing this up. I believe it's a limitation of zod that after Do you want to manipulate the generated object schema further and then use it for validation? Could you share the ideal way how you want to do it? Maybe we can come up with a workaround here. |
Hey thanks for the quick response. And apologies, the issue is a little unclear. Here you have to redefine the type for react-hook-form: https://github.com/ymc9/zenstack-form-validation/blob/5ceea4423e13541e4ddeb49d157d7f6a41432a96/src/app/page.tsx#L33-L38. What I'd like to do is replace those lines with The issue is not the ZodEffects, it's that all type information gets lost during the zod plugin generation. If we copy and paste the generated schema, we can see that even with const baseSchema = z.object({
name: z.string(),
email: z.string().email().endsWith("@zenstack.dev", { message: "Must be a @zenstack.dev email" }),
adult: z.boolean(),
beverage: z.string(),
});
function refine(schema: typeof baseSchema) {
return schema.refine((value) => { var _a, _b; return ((_b = (_a = ['SODA', 'COFFEE', 'BEER', 'COCKTAIL']) === null || _a === void 0 ? void 0 : _a.includes(value === null || value === void 0 ? void 0 : value.beverage)) !== null && _b !== void 0 ? _b : false); }, { message: "Please choose a valid beverage" })
.refine((value) => { var _a, _b; return ((value === null || value === void 0 ? void 0 : value.adult) || ((_b = (_a = ['SODA', 'COFFEE']) === null || _a === void 0 ? void 0 : _a.includes(value === null || value === void 0 ? void 0 : value.beverage)) !== null && _b !== void 0 ? _b : false)); }, { message: "You must be an adult to drink alcohol" });
}
const refinedSchema = refine(baseSchema)
type Input = z.infer<typeof refinedSchema> ![]() Compare this to the generated schema type once we use The underlying types are gone (everything is just |
Thanks a lot for the detailed explanation and for making the PR @tlancina ! It makes very good sense, and the code changes look great! I'll merge it when CI passes and include it in the next release. I'll probably make a further change after merging your PR to export the schemas before the |
Sounds good, thanks! |
Hello! Generated model types for Zod change to ZodEffects and lose most of their helpful information when using a validate rule. This came up because I'd like to enforce some string literals and can't use enum because some of them have disallowed chars (I'd like to enforce that the field/form is one of
"a" | "b" | "c'd"
- if there's another way to do this please let me know.)Using https://github.com/ymc9/zenstack-form-validation.
The lines in question are
.zenstack/zod/models/Signup.schema.d.ts
with@@validate
:Without validate:
Environment (please complete the following information):
Using https://github.com/ymc9/zenstack-form-validation. I removed package-lock because there's a reference to localhost, and updated to latest zenstack (beta 21).
Happy to contribute if you can point me in the right direction.
The text was updated successfully, but these errors were encountered: