All files / schemas pet.schema.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

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          2x                               2x     1x                    
import { z } from 'zod';
 
/**
 * 宠物 Schema
 */
export const petSchema = z.object({
  id: z.number(),
  name: z.string(),
  species: z.string(),
  species_display: z.string().optional(),
  breed: z.string().optional().nullable(),
  age: z.number().nullable().optional(),
  photo_url: z.string().nullable().optional(), // 后端返回 photo_url
  description: z.string().optional().nullable(),
  created_at: z.string(),
  updated_at: z.string(),
});
 
/**
 * 创建/更新宠物的输入 Schema
 */
export const petInputSchema = z.object({
  name: z.string().min(1, '请输入宠物名称').max(100, '宠物名称最多100个字符'),
  species: z.enum(['dog', 'cat', 'bird', 'other'], {
    errorMap: () => ({ message: '请选择宠物种类' }),
  }),
  breed: z.string().max(100, '品种最多100个字符').optional(),
  age: z.number().int().min(0, '年龄不能为负数').max(100, '年龄过大').nullable().optional(),
  description: z.string().max(500, '描述最多500个字符').optional(),
});
 
// 类型导出
export type Pet = z.infer<typeof petSchema>;
export type PetInput = z.infer<typeof petInputSchema>;