feat(i18n): localize item validation messages
This commit is contained in:
+48
-21
@@ -1,33 +1,60 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const createItemSchema = z.object({
|
||||
name: z.string().min(1, {
|
||||
error: "Name is required",
|
||||
}),
|
||||
categoryId: z.string().min(1, {
|
||||
error: "Category is required",
|
||||
}),
|
||||
stock: z.coerce.number().int().nonnegative().min(0, {
|
||||
error: "Stock is required",
|
||||
}),
|
||||
})
|
||||
import type { Dictionary } from "@/i18n/dictionaries"
|
||||
|
||||
export type ItemSchemaCopy = Dictionary["inventory"]["items"]["schema"]
|
||||
|
||||
const defaultItemSchemaCopy: ItemSchemaCopy = {
|
||||
nameRequired: "Name is required",
|
||||
categoryRequired: "Category is required",
|
||||
stockRequired: "Stock is required",
|
||||
itemRequired: "Item is required",
|
||||
}
|
||||
|
||||
export function buildCreateItemSchema(copy: ItemSchemaCopy) {
|
||||
return z.object({
|
||||
name: z.string().min(1, {
|
||||
error: copy.nameRequired,
|
||||
}),
|
||||
categoryId: z.string().min(1, {
|
||||
error: copy.categoryRequired,
|
||||
}),
|
||||
stock: z.coerce
|
||||
.number({ error: copy.stockRequired })
|
||||
.int({ error: copy.stockRequired })
|
||||
.nonnegative({ error: copy.stockRequired })
|
||||
.min(0, {
|
||||
error: copy.stockRequired,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
export const createItemSchema = buildCreateItemSchema(defaultItemSchemaCopy)
|
||||
|
||||
export type CreateItemFormType = z.input<typeof createItemSchema>
|
||||
export type CreateItemData = z.output<typeof createItemSchema>
|
||||
|
||||
export const updateItemSchema = createItemSchema.extend({
|
||||
id: z.string().min(1, {
|
||||
error: "Item is required",
|
||||
}),
|
||||
})
|
||||
export function buildUpdateItemSchema(copy: ItemSchemaCopy) {
|
||||
return buildCreateItemSchema(copy).extend({
|
||||
id: z.string().min(1, {
|
||||
error: copy.itemRequired,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
export const updateItemSchema = buildUpdateItemSchema(defaultItemSchemaCopy)
|
||||
|
||||
export type UpdateItemFormType = z.input<typeof updateItemSchema>
|
||||
export type UpdateItemData = z.output<typeof updateItemSchema>
|
||||
|
||||
export const getItemByIdSchema = z.object({
|
||||
id: z.string().min(1, {
|
||||
error: "Item is required",
|
||||
}),
|
||||
})
|
||||
export function buildGetItemByIdSchema(copy: ItemSchemaCopy) {
|
||||
return z.object({
|
||||
id: z.string().min(1, {
|
||||
error: copy.itemRequired,
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
export const getItemByIdSchema = buildGetItemByIdSchema(defaultItemSchemaCopy)
|
||||
|
||||
export type GetItemByIdFormType = z.infer<typeof getItemByIdSchema>
|
||||
|
||||
Reference in New Issue
Block a user