Z
Zod•5mo ago
doug4794

doug4794 - I have this login schema and want to...

I have this login schema and want to display my own error message.
export const loginSchema = z.object({
password: z.string().min(8, { message: i18n("errors.password-too-short") }),
});
export const loginSchema = z.object({
password: z.string().min(8, { message: i18n("errors.password-too-short") }),
});
Problem is that the schema is initialized before i18n . That's why the translation is wrong. Is there a way to asynchronously fetch the message string (with a function or so)? I know it's possible to do with refinement but I want to have it only on the min() validator and not on the whole thing.
4 Replies
janglad
janglad•5mo ago
don't think it's possible to pass a function as a message nope so you'd indeed need to make sure it's initialised after i18..
Svish
Svish•5mo ago
I feel like this question has been asked here before? 🤔
Svish
Svish•5mo ago
But no, the current way to change error messages is to use an error map, which you can set up globally, per parse, or per schema. https://zod.dev/ERROR_HANDLING
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
doug4794
doug4794•5mo ago
Ok thank you, maybe I'll create a GitHub issue then.
Want results from more Discord servers?
Add your server