What's the recommended way to use useExtracted with Zod schemas?
#2347
-
|
I'm experimenting with With traditional translation keys, I would usually keep the schema independent from i18n and translate validation errors separately. However, For example, I currently see two possible approaches. Option 1: Keep the schema independent from translationsimport * as z from 'zod/mini';
const schema = z.object({
name: z.string('invalidName').check(z.trim(), z.minLength(1, 'requiredName'))
});Then translate errors separately in the UI. The issue is that t(error.message);Option 2: Inject translated messages into the schemainterface SchemaMessages {
invalidName: string;
requiredName: string;
}
const createSchema = (messages: SchemaMessages) =>
z.object({
name: z.string(messages.invalidName).check(
z.trim(),
z.minLength(1, messages.requiredName)
)
});
export function Form() {
const t = useExtracted();
const schema = createSchema({
invalidName: t('Name is invalid'),
requiredName: t('Name is required')
});
// ...
}This works with extraction because the message literal is visible to Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
I can recommend this approach: https://next-intl.dev/docs/environments/actions-metadata-route-handlers#server-actions-zod |
Beta Was this translation helpful? Give feedback.
-
|
@amannn I'd like to use ZOD schemas on the client side without server actions, along with react-hook-form or @tanstack/react-form. Is it possible to use |
Beta Was this translation helpful? Give feedback.
Yep, that looks good to me! 💯