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

Unable to override translation with discriminated union #30

Open
zAlweNy26 opened this issue Jun 12, 2024 · 5 comments
Open

Unable to override translation with discriminated union #30

zAlweNy26 opened this issue Jun 12, 2024 · 5 comments
Labels
question Further information is requested

Comments

@zAlweNy26
Copy link
Contributor

Description

Hi, I'm trying to override an error translation but my schema is a z.discriminatedUnion and I don't understand what I'm doing wrong, I'm still having the default error from the translated errors of this library.
That's the schema:

const triggerSchema = z.discriminatedUnion('type', [
	likeTriggerSchema,
	dislikeTriggerSchema,
]).refine(data => ['dislike', 'like'].includes(data.type), {
	path: ['type'],
	params: { i18n: 'errors.trigger.unknownType' },
})

I'm using Nuxt UI, so the schema is used by the Form component.

@zAlweNy26 zAlweNy26 added the question Further information is requested label Jun 12, 2024
@xibman
Copy link
Owner

xibman commented Jun 13, 2024

Hi @zAlweNy26 this only solution i found is next one:

But you will not have the text translated.

const triggerSchema = z.discriminatedUnion('type', [
  likeTriggerSchema,
  dislikeTriggerSchema,
], {
  errorMap: (issue, ctx) => {
    console.log(issue, ctx)
    if (issue.code === z.ZodIssueCode.invalid_union_discriminator) {
      return { message: 'errors.trigger.unknownType' }
    }

    return { message: ctx.defaultError }
  },
})

@xibman
Copy link
Owner

xibman commented Jun 13, 2024

@zAlweNy26 check also this message #29 (comment)

@zAlweNy26
Copy link
Contributor Author

@zAlweNy26 check also this message #29 (comment)

I saw that comment, but I think it's a bit overkill to solve that way. Is there any other way, maybe by updating this module?

@xibman
Copy link
Owner

xibman commented Jun 13, 2024

The problem is really on zod side. The author of the lib now it and as i see he work on it for the next version :
To improve error reporting, I'll be simplifying Zod's error map system. The new system will also be more amenable to internationalization (RFC forthcoming).
https://zod.dev/blog/clerk-fellowship

Other solution wrap the schema to pass the translation composer at runtime

@zAlweNy26
Copy link
Contributor Author

Ok thanks for the clarification :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants