Files
rn-app/src/schemas/user.ts

77 lines
1.8 KiB
TypeScript
Raw Normal View History

2025-11-04 13:27:19 +08:00
/**
* 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>;