Skip to content

Commit

Permalink
refactor: use zod
Browse files Browse the repository at this point in the history
  • Loading branch information
TinsFox committed Nov 3, 2024
1 parent cccd959 commit 0e10336
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/components/nav-sidebar/nav-breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@/components/ui/breadcrumb"
import { useNavMenu } from "@/hooks/query/user-memu"
import { cn } from "@/lib/utils"
import type { IMenu } from "@/models/menu"
import type { IMenu } from "@/schema/menu"

interface Breadcrumb {
title: I18nKeys
Expand Down
2 changes: 1 addition & 1 deletion src/components/nav-sidebar/nav-main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
SidebarMenuSubButton,
SidebarMenuSubItem,
} from "@/components/ui/sidebar"
import type { IMenu } from "@/models/menu"
import type { IMenu } from "@/schema/menu"

import { Button } from "../ui/button"

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/query/use-album.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery } from "@tanstack/react-query"

import { apiFetch } from "@/lib/api-fetch"
import type { IAlbum } from "@/models/album"
import type { IAlbum } from "@/schema/album"

export function useAlbums() {
return useQuery({
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/query/use-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { PaginationState } from "@tanstack/react-table"
import { useNavigate } from "react-router-dom"

import { apiFetch } from "@/lib/api-fetch"
import type { ILoginForm, IUserProfile, IUsers } from "@/models/user"
import type { ILoginForm, IUserProfile, IUsers } from "@/schema/user"

export const queryUser = () => queryOptions({
queryKey: ["userInfo"],
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/query/user-memu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
Trash2,
} from "lucide-react"

import type { IMenu } from "@/models/menu"
import type { IMenu } from "@/schema/menu"

export const queryNavMenu = () => queryOptions({
queryKey: ["nav-menu"],
Expand Down
2 changes: 1 addition & 1 deletion src/lib/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IMenu } from "@/models/menu"
import type { IMenu } from "@/schema/menu"

export function findMenuTitleByPathname(
menus: IMenu[],
Expand Down
9 changes: 0 additions & 9 deletions src/models/album.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/models/menu.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/pages/(admin)/(with-layout)/list/basic-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
TableRow,
} from "@/components/ui/table"
import { useUsers } from "@/hooks/query/use-user"
import type { IUsers } from "@/models/user"
import type { IUsers } from "@/schema/user"

import { ViewUser } from "./components/view-user"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
import { Separator } from "@/components/ui/separator"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"
import { cn } from "@/lib/utils"
import type { IAlbum } from "@/models/album"
import type { IAlbum } from "@/schema/album"

export interface AlbumCardProps extends React.HTMLAttributes<HTMLDivElement> {
album: IAlbum
Expand Down
4 changes: 2 additions & 2 deletions src/pages/(admin)/(with-layout)/list/components/view-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import {
SheetTrigger,
} from "@/components/ui/sheet"
import { toast } from "@/components/ui/use-toast"
import type { IUsers } from "@/models/user"
import { userRoles, userSchema } from "@/models/user"
import type { IUsers } from "@/schema/user"
import { userRoles, userSchema } from "@/schema/user"

export function ViewUser({ user }: { user: IUsers }) {
const form = useForm<IUsers>({
Expand Down
4 changes: 2 additions & 2 deletions src/pages/(external)/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { Input } from "@/components/ui/input"
import { useUserLoginMutation } from "@/hooks/query/use-user"
import { getFetchErrorMessage } from "@/lib/api-fetch"
import { cn } from "@/lib/utils"
import type { ILoginForm } from "@/models/user"
import { loginFormSchema } from "@/models/user"
import type { ILoginForm } from "@/schema/user"
import { loginFormSchema } from "@/schema/user"

export function Component() {
const { t } = useTranslation("auth")
Expand Down
13 changes: 13 additions & 0 deletions src/schema/album.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from "zod"

export const albumSchema = z.object({
id: z.string(),
title: z.string(),
cover: z.string(),
url: z.string(),
slogan: z.string(),
updatedAt: z.date(),
digitalDownloads: z.number(),
})

export type IAlbum = z.infer<typeof albumSchema>
37 changes: 37 additions & 0 deletions src/schema/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { LucideIcon } from "lucide-react"
import { z } from "zod"

// 由于 LucideIcon 是一个类型,我们需要使用 custom 方法来验证
const LucideIconSchema = z.custom<LucideIcon>(
(data) => {
return typeof data === "function"
},
{
message: "Invalid Lucide icon",
},
)

// 子菜单项 Schema
export const ChildrenMenuItemSchema = z.object({
title: z.string(),
label: z.string().optional(),
icon: LucideIconSchema,
to: z.string().url(),
})

// 菜单项 Schema
export const MenuItemSchema = z.object({
title: z.string(),
label: z.string().optional(),
icon: LucideIconSchema,
to: z.string().url(),
children: z.array(ChildrenMenuItemSchema).optional(),
})

// 导出类型
export type IChildrenMenuItem = z.infer<typeof ChildrenMenuItemSchema>
export type MenuItem = z.infer<typeof MenuItemSchema>
export type IMenu = MenuItem

// 如果需要验证菜单数组
export const MenuArraySchema = z.array(MenuItemSchema)
File renamed without changes.

0 comments on commit 0e10336

Please sign in to comment.