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

Interpolate custom messages without refine/params method #212

Open
kluplau opened this issue Aug 16, 2024 · 1 comment
Open

Interpolate custom messages without refine/params method #212

kluplau opened this issue Aug 16, 2024 · 1 comment

Comments

@kluplau
Copy link

kluplau commented Aug 16, 2024

Hi,

I wanted to display some custom errors from my API.

I made a name space called formErrors that contains these generic errors, and when an error occurs I then check if formErrors has an translation for it and if so, I translate it with i18next.

However, some erorrs needs more context (parms), like my custom TOO_SHORT_PASSWORD that is a Zod type like string().min(8, "TOO_SHORT_PASSWORD"), which in my formErrors hans a translated string `The password should be at least {{ minimum }} characters long".

This works, I think I have misunderstood something, or done it wrong. With my implementation I miss out of some of the functionality of this package like exact, inclusive and not_inclusive.

Is there a way I can have custom error messages, with interpolation, that doesn't require the refine/params method?

@qbtf
Copy link

qbtf commented Nov 12, 2024

I discovered that the error mapping function is not invoked when you already have message set, i.e. the message field, if provided, is guaranteed to be untouched. This behavior is at zod's side, so I do not think it is possible for this package to intercept your custom message in any way.

That said, I wonder if adding an extra layer after form validations fits into your use case; to check each issue for a code field from zod and call i18n at this moment.

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

2 participants