i18n support added to all form validations

This commit is contained in:
2024-02-17 07:28:47 +01:00
parent 1da6479c80
commit 30b3da9c31
4 changed files with 38 additions and 30 deletions

View File

@@ -7,7 +7,8 @@ import { ObjectId } from 'mongodb';
import { withUser } from '@/app/lib/auth';
import { AuthenticatedUser } from '../types/next-auth';
import { gotoHome } from './navigationActions';
import { Formats, TranslationValues, useTranslations } from "next-intl";
import { getTranslations } from "next-intl/server";
import { IntlTemplateFn } from '@/app/i18n';
export type State = {
errors?: {
@@ -19,10 +20,11 @@ export type State = {
message?:string | null;
}
type IntlTemplate = <TargetKey extends any>(key: TargetKey, values?: TranslationValues | undefined, formats?: Partial<Formats> | undefined) => string;
const FormSchema = (t:IntlTemplate) => z.object({
/**
* Schema for validating bill form fields
* @description this is defined as factory function so that it can be used with the next-intl library
*/
const FormSchema = (t:IntlTemplateFn) => z.object({
_id: z.string(),
billName: z.coerce.string().min(1, t("bill-name-required")),
billNotes: z.string(),
@@ -65,10 +67,6 @@ const FormSchema = (t:IntlTemplate) => z.object({
}),
});
parseFloat
const UpdateBill = ;
/**
* converts the file to a format stored in the database
* @param billAttachment
@@ -117,10 +115,10 @@ export const updateOrAddBill = withUser(async (user:AuthenticatedUser, locationI
const { id: userId } = user;
const t = useTranslations("bill-edit-form.validation");
const t = await getTranslations("bill-edit-form.validation");
// FormSchema
const validatedFields = UpdateBill(t)
const validatedFields = FormSchema(t)
.omit({ _id: true })
.safeParse({
billName: formData.get('billName'),