-
-
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
Cannot get error message from safeParse #2883
Comments
@colinhacks pls fix |
@colinhacks @Creative-Difficulty, I'm facing the same issue and the return type for safeParse() is also incorrect. |
@amany9000 There's a quite easy workaround |
when run
with: zod@^3.22.4 The issue is resolved already? |
No it isn't |
I'm seeing the same issue. Here's my code where I'm seeing it. I changed the variable names for security reasons but, it doesn't change anything. const envSchema = z.object({
NODE_ENV: z.string().default("development"),
// FOO
FOO_API_KEY: z.string(),
// BAR
BAR_KEY: z.string(),
BAR_REGION: z.string(),
})
const parsed = envSchema.safeParse(process.env)
if (!parsed.success) {
console.error(
`Error #%d: Failed Parsing .env file.
Please ensure that it exists and is valid: `,
JSON.stringify(parsed.error.format(), null, 4)
// ^^^^^
);
process.exit(1);
} else {
...
} VSCode Error for the underlined object:
|
Do you have |
This has nothing to do with strict mode. |
This is still an issue |
This is still an issue in 2024. Considering the above works, it seems that it's just the type definitions that are broken. |
Same, currenty facing this issue |
It works if you do |
Thanks for the solution my friend |
You need to enable |
I am still encountering this error. I have set Tried |
It works when:
|
So I believe this issue is still present and may have been misunderstood. I am not having any issues with the types, however the return type for [
{
"code": "invalid_type",
"expected": "string",
"received": "number",
"path": [],
"message": "Expected string, received number"
}
] So, if you follow the documentation and provide export function getZodErrorMessage(error: z.ZodError): string {
const message = error.message;
const parsedMessage = JSON.parse(message) as Array<{ message: string }>;
const zodError = parsedMessage[0];
if (!zodError) throw Error("Zod is broken");
return zodError.message;
} However, I don't think this should be necessary. It would be nice if this was fixed at some point in the future! Thanks 😄 |
Per today's code I think the type is actually technically correct (the best kind of correct): Lines 286 to 288 in f7ad261
i.e. the output of the I've opened #3937 to start a discussion about possible resolutions (starting with just a comment clarifying what the field actually returns). |
The Other people on here are describing an unrelated issue: |
Makes a lot of sense and is good information -- it'd be extra-useful if it was straightforward to find in documentation or inline in the code (per #3937, which I have updated to include this explanation specifically). |
This should work:
but it doesn't.
Instead i have to do these shenanigans:
The text was updated successfully, but these errors were encountered: