All files / schemas user.schema.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 0/0
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                1x                             1x               1x             1x             1x                 1x                            
import { z } from 'zod';
 
import { petSchema } from './pet.schema';
 
/**
 * 用户信息 Schema(适配 Supabase)
 * 包含用户基础信息、头像和宠物列表
 */
export const userSchema = z.object({
  id: z.string(), // Supabase 使用 UUID
  email: z.string().email().optional(),
  username: z.string(),
  bio: z.string().nullable().optional(), // 用户简介
  avatar_url: z.string().nullable().optional(), // 头像 URL
  is_admin: z.boolean().optional().default(false), // 管理员标识
  created_at: z.string().optional(), // 创建时间
  updated_at: z.string().optional(), // 更新时间
  pets: z.array(petSchema).optional(), // 用户的宠物列表
});
 
/**
 * 头像上传响应 Schema
 */
export const avatarUploadResponseSchema = z.object({
  message: z.string(),
  avatar: z.string(), // 新头像的 URL
});
 
/**
 * 删除响应 Schema
 */
export const deleteResponseSchema = z.object({
  message: z.string(),
});
 
/**
 * 更新用户名请求 Schema
 */
export const updateUsernameSchema = z.object({
  username: z.string().min(3, '用户名至少需要3个字符').max(20, '用户名最多20个字符'),
});
 
/**
 * 修改密码请求 Schema
 */
export const changePasswordSchema = z.object({
  current_password: z.string().min(1, '请输入当前密码'),
  new_password: z.string().min(6, '新密码至少需要6个字符'),
  re_new_password: z.string().min(6, '确认密码至少需要6个字符'),
});
 
/**
 * 通用成功响应 Schema
 */
export const successResponseSchema = z.object({
  message: z.string(),
});
 
// 类型导出
export type User = z.infer<typeof userSchema>;
export type AvatarUploadResponse = z.infer<typeof avatarUploadResponseSchema>;
export type DeleteResponse = z.infer<typeof deleteResponseSchema>;
export type UpdateUsernameInput = z.infer<typeof updateUsernameSchema>;
export type ChangePasswordInput = z.infer<typeof changePasswordSchema>;
export type SuccessResponse = z.infer<typeof successResponseSchema>;
 
// 重新导出宠物相关类型,方便统一导入
export type { Pet, PetInput } from './pet.schema';