user.schema.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { z } from '@hono/zod-openapi';
  2. import { DeleteStatus, DisabledStatus } from '@/share/types';
  3. import { RoleSchema } from './role.schema';
  4. // 基础用户 schema(包含所有字段)
  5. export const UserSchema = z.object({
  6. id: z.number().int().positive().openapi({ description: '用户ID' }),
  7. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  8. example: 'admin',
  9. description: '用户名,3-255个字符'
  10. }),
  11. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  12. example: 'password123',
  13. description: '密码,最少6位'
  14. }),
  15. phone: z.string().max(255, '手机号最多255个字符').nullable().openapi({
  16. example: '13800138000',
  17. description: '手机号'
  18. }),
  19. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().openapi({
  20. example: 'user@example.com',
  21. description: '邮箱'
  22. }),
  23. nickname: z.string().max(255, '昵称最多255个字符').nullable().openapi({
  24. example: '昵称',
  25. description: '用户昵称'
  26. }),
  27. name: z.string().max(255, '姓名最多255个字符').nullable().openapi({
  28. example: '张三',
  29. description: '真实姓名'
  30. }),
  31. avatar: z.string().max(255, '头像地址最多255个字符').nullable().openapi({
  32. example: 'https://example.com/avatar.jpg',
  33. description: '用户头像'
  34. }),
  35. isDisabled: z.number().int().min(0).max(1).default(DisabledStatus.ENABLED).openapi({
  36. example: DisabledStatus.ENABLED,
  37. description: '是否禁用(0:启用,1:禁用)'
  38. }),
  39. isDeleted: z.number().int().min(0).max(1).default(DeleteStatus.NOT_DELETED).openapi({
  40. example: DeleteStatus.NOT_DELETED,
  41. description: '是否删除(0:未删除,1:已删除)'
  42. }),
  43. roles: z.array(RoleSchema).optional().openapi({
  44. example: [
  45. {
  46. id: 1,
  47. name: 'admin',
  48. description: '管理员',
  49. permissions: ['user:create'],
  50. createdAt: new Date(),
  51. updatedAt: new Date()
  52. }
  53. ],
  54. description: '用户角色列表'
  55. }),
  56. createdAt: z.date().openapi({ description: '创建时间' }),
  57. updatedAt: z.date().openapi({ description: '更新时间' })
  58. });
  59. // 创建用户请求 schema
  60. export const CreateUserDto = z.object({
  61. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').openapi({
  62. example: 'admin',
  63. description: '用户名,3-255个字符'
  64. }),
  65. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').openapi({
  66. example: 'password123',
  67. description: '密码,最少6位'
  68. }),
  69. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  70. example: '13800138000',
  71. description: '手机号'
  72. }),
  73. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  74. example: 'user@example.com',
  75. description: '邮箱'
  76. }),
  77. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  78. example: '昵称',
  79. description: '用户昵称'
  80. }),
  81. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  82. example: '张三',
  83. description: '真实姓名'
  84. }),
  85. avatar: z.string().max(255, '头像地址最多255个字符').nullable().optional().openapi({
  86. example: 'https://example.com/avatar.jpg',
  87. description: '用户头像'
  88. }),
  89. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').default(DisabledStatus.ENABLED).optional().openapi({
  90. example: DisabledStatus.ENABLED,
  91. description: '是否禁用(0:启用,1:禁用)'
  92. })
  93. });
  94. // 更新用户请求 schema
  95. export const UpdateUserDto = z.object({
  96. username: z.string().min(3, '用户名至少3个字符').max(255, '用户名最多255个字符').optional().openapi({
  97. example: 'admin',
  98. description: '用户名,3-255个字符'
  99. }),
  100. password: z.string().min(6, '密码至少6位').max(255, '密码最多255位').optional().openapi({
  101. example: 'password123',
  102. description: '密码,最少6位'
  103. }),
  104. phone: z.string().max(255, '手机号最多255个字符').nullable().optional().openapi({
  105. example: '13800138000',
  106. description: '手机号'
  107. }),
  108. email: z.email('请输入正确的邮箱格式').max(255, '邮箱最多255个字符').nullable().optional().openapi({
  109. example: 'user@example.com',
  110. description: '邮箱'
  111. }),
  112. nickname: z.string().max(255, '昵称最多255个字符').nullable().optional().openapi({
  113. example: '昵称',
  114. description: '用户昵称'
  115. }),
  116. name: z.string().max(255, '姓名最多255个字符').nullable().optional().openapi({
  117. example: '张三',
  118. description: '真实姓名'
  119. }),
  120. avatar: z.string().max(255, '头像地址最多255个字符').nullable().optional().openapi({
  121. example: 'https://example.com/avatar.jpg',
  122. description: '用户头像'
  123. }),
  124. isDisabled: z.number().int().min(0, '状态值只能是0或1').max(1, '状态值只能是0或1').optional().openapi({
  125. example: DisabledStatus.ENABLED,
  126. description: '是否禁用(0:启用,1:禁用)'
  127. })
  128. });
  129. // 用户列表响应 schema
  130. export const UserListResponse = z.object({
  131. data: z.array(UserSchema),
  132. pagination: z.object({
  133. total: z.number().openapi({
  134. example: 100,
  135. description: '总记录数'
  136. }),
  137. current: z.number().openapi({
  138. example: 1,
  139. description: '当前页码'
  140. }),
  141. pageSize: z.number().openapi({
  142. example: 10,
  143. description: '每页数量'
  144. })
  145. })
  146. });
  147. // 类型导出
  148. export type User = z.infer<typeof UserSchema>;
  149. export type CreateUserRequest = z.infer<typeof CreateUserDto>;
  150. export type UpdateUserRequest = z.infer<typeof UpdateUserDto>;
  151. export type UserListResponseType = z.infer<typeof UserListResponse>;