platform.schema.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { z } from '@hono/zod-openapi';
  2. // 平台实体Schema
  3. export const PlatformSchema = z.object({
  4. id: z.number().int().positive().openapi({
  5. description: '平台ID',
  6. example: 1
  7. }),
  8. platformName: z.string().max(100).openapi({
  9. description: '平台名称',
  10. example: '微信平台'
  11. }),
  12. contactPerson: z.string().max(50).openapi({
  13. description: '联系人',
  14. example: '张三'
  15. }),
  16. contactPhone: z.string().max(20).openapi({
  17. description: '联系电话',
  18. example: '13800138000'
  19. }),
  20. contactEmail: z.string().email().max(100).nullable().optional().openapi({
  21. description: '联系邮箱',
  22. example: 'zhangsan@example.com'
  23. }),
  24. status: z.number().int().min(0).max(1).default(1).openapi({
  25. description: '状态:1-正常,0-禁用',
  26. example: 1
  27. }),
  28. createTime: z.coerce.date().openapi({
  29. description: '创建时间',
  30. example: '2024-01-01T00:00:00Z'
  31. }),
  32. updateTime: z.coerce.date().openapi({
  33. description: '更新时间',
  34. example: '2024-01-01T00:00:00Z'
  35. })
  36. });
  37. // 创建平台DTO
  38. export const CreatePlatformSchema = z.object({
  39. platformName: z.string({
  40. error: '请输入平台名称'
  41. }).min(1, {
  42. message: '平台名称不能为空'
  43. }).max(100, {
  44. message: '平台名称不能超过100个字符'
  45. }).openapi({
  46. description: '平台名称',
  47. example: '微信平台'
  48. }),
  49. contactPerson: z.string().max(50).optional().openapi({
  50. description: '联系人',
  51. example: '张三'
  52. }),
  53. contactPhone: z.string().max(20).optional().openapi({
  54. description: '联系电话',
  55. example: '13800138000'
  56. }),
  57. contactEmail: z.string({
  58. error: '请输入联系邮箱'
  59. }).email({
  60. message: '请输入有效的邮箱地址'
  61. }).max(100, {
  62. message: '邮箱地址不能超过100个字符'
  63. }).optional()
  64. .or(z.literal('')) // 允许空字符串
  65. .transform(val => val === '' ? undefined : val) // 将空字符串转为undefined
  66. .openapi({
  67. description: '联系邮箱',
  68. example: 'zhangsan@example.com'
  69. })
  70. });
  71. // 更新平台DTO
  72. export const UpdatePlatformSchema = z.object({
  73. id: z.number().int().positive().openapi({
  74. description: '平台ID',
  75. example: 1
  76. }),
  77. platformName: z.string().min(1).max(100).optional().openapi({
  78. description: '平台名称',
  79. example: '微信平台'
  80. }),
  81. contactPerson: z.string().max(50).optional().openapi({
  82. description: '联系人',
  83. example: '张三'
  84. }),
  85. contactPhone: z.string().max(20).optional().openapi({
  86. description: '联系电话',
  87. example: '13800138000'
  88. }),
  89. contactEmail: z.string({
  90. error: '请输入联系邮箱'
  91. }).email({
  92. message: '请输入有效的邮箱地址'
  93. }).max(100, {
  94. message: '邮箱地址不能超过100个字符'
  95. }).optional()
  96. .or(z.literal('')) // 允许空字符串
  97. .transform(val => val === '' ? undefined : val) // 将空字符串转为undefined
  98. .openapi({
  99. description: '联系邮箱',
  100. example: 'zhangsan@example.com'
  101. })
  102. });
  103. // 删除平台DTO
  104. export const DeletePlatformSchema = z.object({
  105. id: z.number().int().positive().openapi({
  106. description: '平台ID',
  107. example: 1
  108. })
  109. });
  110. // 分页查询参数Schema
  111. export const PaginationQuerySchema = z.object({
  112. skip: z.coerce.number().int().min(0).default(0).optional().openapi({
  113. description: '跳过记录数',
  114. example: 0
  115. }),
  116. take: z.coerce.number().int().min(1).max(100).default(10).optional().openapi({
  117. description: '获取记录数',
  118. example: 10
  119. })
  120. });
  121. // 搜索平台参数Schema
  122. export const SearchPlatformQuerySchema = PaginationQuerySchema.extend({
  123. name: z.string().min(1).openapi({
  124. description: '搜索关键词',
  125. example: '微信'
  126. })
  127. });
  128. // 类型定义
  129. export type Platform = z.infer<typeof PlatformSchema>;
  130. export type CreatePlatformDto = z.infer<typeof CreatePlatformSchema>;
  131. export type UpdatePlatformDto = z.infer<typeof UpdatePlatformSchema>;
  132. export type DeletePlatformDto = z.infer<typeof DeletePlatformSchema>;
  133. export type PaginationQuery = z.infer<typeof PaginationQuerySchema>;
  134. export type SearchPlatformQuery = z.infer<typeof SearchPlatformQuerySchema>;