-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Zod 2: nested transforms don't work #199
Comments
I agree this is quite unintuitive. I'm also not sure if this is fixable. The problem seems to be with your implementation of If you "unwrap" the top-level call to const transformArray = <T extends z.ZodTypeAny>(childSchema: T) => {
const schema = z.object({
elements: z.array(childSchema),
});
return z.transformer(schema, z.array(childSchema), val => {
return val.elements;
});
};
const classSchema = z.object({
students: transformArray(
z.object({
id: z.number(),
}),
),
});
const classesSchema = z.transformer(
z.object({ elements: z.array(classSchema) }),
z.array(classSchema),
val => {
return val.elements;
},
);
This is definitely a major issue with how transformers are currently implemented. I'll keep thinking about this one. |
Thanks 👍 |
There's not technically a bug here so I'm gonna close. You've just got to be careful when using generics and transformers together. |
I'm trying to use Zod to parse data that has arrays nested in an object like this (don't blame me, from a third party :D):
I can write a parser like this, that works as expected, but there's a lot of boilerplate:
I wrote a helper function, that builds the array schema and uses transforms to remove it from the parsed data:
However if I try to use this to parse the whole object, it compiles and has the correct return type, but at runtime throws an error:
The text was updated successfully, but these errors were encountered: