feat: update
This commit is contained in:
68
schemas/user.ts
Normal file
68
schemas/user.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 用户相关的 Zod 验证 Schema
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* 用户信息 Schema
|
||||
*/
|
||||
export const userSchema = z.object({
|
||||
id: z.string(),
|
||||
username: z.string(),
|
||||
email: z.string().email(),
|
||||
avatar: z.string().url().optional(),
|
||||
nickname: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
createdAt: z.string().optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* 更新用户资料 Schema
|
||||
*/
|
||||
export const updateProfileSchema = z.object({
|
||||
nickname: z.string().min(2, '昵称至少2个字符').max(20, '昵称最多20个字符').optional(),
|
||||
avatar: z.string().url('请输入有效的头像URL').optional(),
|
||||
phone: z
|
||||
.string()
|
||||
.regex(/^1[3-9]\d{9}$/, '请输入有效的手机号')
|
||||
.optional()
|
||||
.or(z.literal('')),
|
||||
bio: z.string().max(200, '个人简介最多200个字符').optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* 绑定手机号 Schema
|
||||
*/
|
||||
export const bindPhoneSchema = z.object({
|
||||
phone: z
|
||||
.string()
|
||||
.min(11, '请输入11位手机号')
|
||||
.max(11, '请输入11位手机号')
|
||||
.regex(/^1[3-9]\d{9}$/, '请输入有效的手机号'),
|
||||
code: z
|
||||
.string()
|
||||
.min(6, '验证码为6位')
|
||||
.max(6, '验证码为6位')
|
||||
.regex(/^\d{6}$/, '验证码必须是6位数字'),
|
||||
});
|
||||
|
||||
/**
|
||||
* 绑定邮箱 Schema
|
||||
*/
|
||||
export const bindEmailSchema = z.object({
|
||||
email: z.string().min(1, '请输入邮箱').email('请输入有效的邮箱地址'),
|
||||
code: z
|
||||
.string()
|
||||
.min(6, '验证码为6位')
|
||||
.max(6, '验证码为6位')
|
||||
.regex(/^\d{6}$/, '验证码必须是6位数字'),
|
||||
});
|
||||
|
||||
/**
|
||||
* TypeScript 类型推断
|
||||
*/
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
export type UpdateProfileFormData = z.infer<typeof updateProfileSchema>;
|
||||
export type BindPhoneFormData = z.infer<typeof bindPhoneSchema>;
|
||||
export type BindEmailFormData = z.infer<typeof bindEmailSchema>;
|
||||
Reference in New Issue
Block a user