ZOD

I want the final response of the API to look something like this:
{
"user": {
"email": string,
"name": string,
},
"parent": { "email": string, "name": string } | null
}
{
"user": {
"email": string,
"name": string,
},
"parent": { "email": string, "name": string } | null
}
So the parent can be nullable, I define my schema like this:
export const getUserByIdResponseSchema = z.object({
user: z.object(userCore),
parent: z.object(userCore).nullable(),
});
export const getUserByIdResponseSchema = z.object({
user: z.object(userCore),
parent: z.object(userCore).nullable(),
});
When the parent is null, it gets returned null like normal, but when there is actually a parent, this is the fastify error I'm getting. Any idea on how to fix this?
3 Replies
Sturlen
Sturlen16mo ago
posting the userCore definition would be helpful
grandpaK420
grandpaK42016mo ago
@sturlen userCore is just the email and name that were mentioned above. Here is the code:
const userCore = {
email: z
.string({
required_error: "Email is required",
invalid_type_error: "Email must be a string",
})
.email(),
name: z.string({
required_error: "Name is required",
invalid_type_error: "Name must be a string",
}),
};
const userCore = {
email: z
.string({
required_error: "Email is required",
invalid_type_error: "Email must be a string",
})
.email(),
name: z.string({
required_error: "Name is required",
invalid_type_error: "Name must be a string",
}),
};
Tom
Tom16mo ago
if you can get the actual ZodError it should tell you exactly what it doesnt like
Want results from more Discord servers?
Add your server