feat(i18n): localize recipient validation messages
This commit is contained in:
@@ -1,54 +1,80 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const recipientSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
username: z
|
||||
.string()
|
||||
.min(1, {
|
||||
error: "Username is required",
|
||||
})
|
||||
.nonempty("Username is required"),
|
||||
firstName: z.string().min(1, {
|
||||
error: "First name is required",
|
||||
}),
|
||||
lastName: z.string().min(1, {
|
||||
error: "Last name is required",
|
||||
}),
|
||||
department: z.enum(
|
||||
[
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
"DRIVER",
|
||||
"LOGISTICS",
|
||||
"ADMINISTRATION",
|
||||
"SALES",
|
||||
"OTHER",
|
||||
],
|
||||
{
|
||||
error: "Department is required",
|
||||
},
|
||||
),
|
||||
email: z.string().optional().nullable(),
|
||||
phone: z.string().optional().nullable(),
|
||||
})
|
||||
import type { Dictionary } from "@/i18n/dictionaries"
|
||||
|
||||
export const createRecipientSchema = recipientSchema.superRefine(
|
||||
(data, ctx) => {
|
||||
export type RecipientSchemaCopy =
|
||||
Dictionary["inventory"]["recipients"]["schema"]
|
||||
|
||||
const defaultRecipientSchemaCopy: RecipientSchemaCopy = {
|
||||
usernameRequired: "Username is required",
|
||||
firstNameRequired: "First name is required",
|
||||
lastNameRequired: "Last name is required",
|
||||
departmentRequired: "Department is required",
|
||||
emailInvalid: "Email format is invalid",
|
||||
idRequired: "ID is required",
|
||||
}
|
||||
|
||||
const recipientDepartments = [
|
||||
"IT",
|
||||
"ENGINEERING",
|
||||
"TRAFFIC",
|
||||
"DRIVER",
|
||||
"LOGISTICS",
|
||||
"ADMINISTRATION",
|
||||
"SALES",
|
||||
"OTHER",
|
||||
] as const
|
||||
|
||||
function buildRecipientBaseSchema(copy: RecipientSchemaCopy) {
|
||||
return z.object({
|
||||
id: z.string().optional(),
|
||||
username: z.string().min(1, {
|
||||
error: copy.usernameRequired,
|
||||
}),
|
||||
firstName: z.string().min(1, {
|
||||
error: copy.firstNameRequired,
|
||||
}),
|
||||
lastName: z.string().min(1, {
|
||||
error: copy.lastNameRequired,
|
||||
}),
|
||||
department: z.enum(recipientDepartments, {
|
||||
error: copy.departmentRequired,
|
||||
}),
|
||||
email: z.string().optional().nullable(),
|
||||
phone: z.string().optional().nullable(),
|
||||
})
|
||||
}
|
||||
|
||||
export const recipientSchema = buildRecipientBaseSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export function buildCreateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||
return buildRecipientBaseSchema(copy).superRefine((data, ctx) => {
|
||||
if (data.email && !z.string().email().safeParse(data.email).success) {
|
||||
ctx.addIssue({
|
||||
code: "custom",
|
||||
message: "Email format is invalid",
|
||||
message: copy.emailInvalid,
|
||||
path: ["email"],
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const createRecipientSchema = buildCreateRecipientSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export type CreateRecipientFormType = z.infer<typeof createRecipientSchema>
|
||||
|
||||
export const updateRecipientSchema = recipientSchema.extend({
|
||||
id: z.string().nonempty("ID is required"),
|
||||
})
|
||||
export function buildUpdateRecipientSchema(copy: RecipientSchemaCopy) {
|
||||
return buildRecipientBaseSchema(copy).extend({
|
||||
id: z.string().nonempty(copy.idRequired),
|
||||
})
|
||||
}
|
||||
|
||||
export const updateRecipientSchema = buildUpdateRecipientSchema(
|
||||
defaultRecipientSchemaCopy,
|
||||
)
|
||||
|
||||
export type UpdateRecipientFormType = z.infer<typeof updateRecipientSchema>
|
||||
|
||||
Reference in New Issue
Block a user