64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { z } from 'zod'
|
|
|
|
import { uploadedFileSchema } from '@/schemas'
|
|
|
|
export const postSchema = z.object({
|
|
id: z.number().int(),
|
|
title: z.string(),
|
|
cover_id: z.number().int(),
|
|
cover: z.string(),
|
|
slug: z.string(),
|
|
summary: z.string(),
|
|
status: z.number().int(),
|
|
view_count: z.number().int(),
|
|
sort: z.number().int(),
|
|
category_name: z.string(),
|
|
category_id: z.number().int().optional(),
|
|
published_at: z.string(),
|
|
tags: z.array(z.number().int()),
|
|
cover_size: z.number(),
|
|
created_at: z.string(),
|
|
updated_at: z.string(),
|
|
})
|
|
|
|
export const postDetailSchema = postSchema.extend({
|
|
content: z.string(),
|
|
})
|
|
|
|
export type Post = z.infer<typeof postSchema>
|
|
|
|
export type PostDetail = z.infer<typeof postDetailSchema>
|
|
|
|
export const postFormSchema = z
|
|
.object({
|
|
id: z.number().int().optional(),
|
|
title: z.string().min(1, '请输入文章标题!'),
|
|
slug: z
|
|
.string()
|
|
.min(1, '请输入 URL 别名!')
|
|
.regex(/^[A-Za-z0-9-]+$/, '只允许字母、数字和短横线(-)'),
|
|
summary: z.string(),
|
|
status: z.number().int(),
|
|
published_at: z.string().min(1, '请选择发布日期!'),
|
|
cover: uploadedFileSchema.optional(),
|
|
sort: z.coerce.number<number>().int().min(0, '排序值不能小于0'),
|
|
content: z.string().optional(),
|
|
category_id: z
|
|
.number()
|
|
.int()
|
|
.optional()
|
|
.refine((v) => v !== undefined, '请选择文章分类'),
|
|
tags: z.array(z.number().int()).optional(),
|
|
})
|
|
.transform((data) => {
|
|
const { cover, ...rest } = data
|
|
return {
|
|
...rest,
|
|
cover_id: cover?.id || null,
|
|
}
|
|
})
|
|
|
|
export type PostFormInput = z.input<typeof postFormSchema>
|
|
|
|
export type PostFormOutput = z.output<typeof postFormSchema>
|