Comments (5)
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 type Input = z.infer<typeof SignupCreateSchema>
.
from zenstack.
Hi @tlancina , thanks for bringing this up. I believe it's a limitation of zod that after .refine()
call, the schema is not a ZodObject anymore. Zod's author confirmed this: colinhacks/zod#2646.
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.
from zenstack.
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 type Input = z.infer<typeof SignupCreateSchema>
. This way, if the schema changes, I don't have to manually update that type.
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 refine()
and ZodEffects, the underlying types are still there:
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 @@validate
:
The underlying types are gone (everything is just any
), so it doesn't work anymore:
from zenstack.
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 refine()
call and export the refine()
function as well, so in case people need to manipulate the object schema further, they can do that and call refine
by themselves. Probably a bit more flexible than it is today.
from zenstack.
Sounds good, thanks!
from zenstack.
Related Issues (20)
- Model inheritance doesn't check for cycles
- LSP VSCode: Linter doesn't complain when using `previewFeatures = ["multiSchema"]` but not including `@@schema` on models / enums
- Polymorphic extends using `enum` for the type when using @@delegate HOT 5
- Update permission buggy when used in @@deny HOT 1
- [ZModel] Insufficient type checking for "in" operator
- Two one-to-many relations on the same model generated wrong model meta data
- Issue using @@index with @@delegate HOT 2
- false error on @relation in vscode extension when using multiple schemas HOT 5
- auth() with import causes linking-error HOT 2
- [Feature Request] Provide Migrations API HOT 1
- [Zod] Field with `auth()` in `@default()` should be generated as optional in zod schemas HOT 1
- Allow accessing all fields through auth() when @@auth is applied on model with @@delegate HOT 3
- Typecasting "Bytes" Fields HOT 2
- [Feature Request] Honojs server adapter support
- Zenstack having problems with upserting relations with compound keys HOT 2
- [Feature Request] Compatibility with Prisma "omit" feature
- Prisma --no-engine CLI option is not available until 5.2.0
- Zod schemas should use `.optional()` instead of `.nullish()` to represent optional fields
- [Feature Request] Disable hooks that generates files outside of node_modules in CLI HOT 1
- Policy compilation errors with deep nested post-update rules
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zenstack.