Skip to content
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

Nullable Zod Object Errors Out #44

Open
KarlGNassar opened this issue Jul 19, 2023 · 0 comments
Open

Nullable Zod Object Errors Out #44

KarlGNassar opened this issue Jul 19, 2023 · 0 comments

Comments

@KarlGNassar
Copy link

KarlGNassar commented Jul 19, 2023

I want the final response of the API to look something like this:

{
  "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(),
});

When the parent is actually null, the response is like expected:

{
    "user": {
        "id": 3,
        "email": "[email protected]",
        "name": "karl nassar"
    },
    "parent": null
}

But when there is an actual parent, the error message states The values of <schema> does not match schema definition

{
    "statusCode": 500,
    "error": "Internal Server Error",
    "message": "The value of '#/definitions/getUserByIdResponseSchema/properties/parent' does not match schema definition."
}

Is there any way to explicitly tell fastify-zod to safeParse the object?

@KarlGNassar KarlGNassar changed the title Nullable Zod Object errors out Nullable Zod Object Errors Out Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant