delivery-address.schema.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { z } from '@hono/zod-openapi';
  2. import { UserSchema } from '@d8d/user-module/schemas';
  3. import { getAreaSchema } from '@d8d/geo-areas/schemas';
  4. // 状态枚举
  5. export const DeliveryAddressStatusEnum = {
  6. NORMAL: 1,
  7. DISABLED: 2,
  8. DELETED: 3
  9. } as const;
  10. export const IsDefaultEnum = {
  11. NOT_DEFAULT: 0,
  12. IS_DEFAULT: 1
  13. } as const;
  14. // 基础实体Schema
  15. export const DeliveryAddressSchema = z.object({
  16. id: z.number()
  17. .int('ID必须是整数')
  18. .positive('ID必须是正整数')
  19. .openapi({
  20. description: '收货地址ID',
  21. example: 1
  22. }),
  23. userId: z.number()
  24. .int('用户ID必须是整数')
  25. .positive('用户ID必须是正整数')
  26. .openapi({
  27. description: '用户ID',
  28. example: 1
  29. }),
  30. name: z.string()
  31. .min(1, '姓名不能为空')
  32. .max(255, '姓名最多255个字符')
  33. .openapi({
  34. description: '收货人姓名',
  35. example: '张三'
  36. }),
  37. phone: z.string()
  38. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  39. .openapi({
  40. description: '收货人手机号',
  41. example: '13800138000'
  42. }),
  43. address: z.string()
  44. .min(1, '详细地址不能为空')
  45. .max(255, '详细地址最多255个字符')
  46. .openapi({
  47. description: '详细地址',
  48. example: '北京市朝阳区建国门外大街1号'
  49. }),
  50. receiverProvince: z.coerce.number<number>()
  51. .int('省份ID必须是整数')
  52. .positive('省份ID必须是正整数')
  53. .default(0)
  54. .openapi({
  55. description: '收货省份ID',
  56. example: 110000
  57. }),
  58. receiverCity: z.coerce.number<number>()
  59. .int('城市ID必须是整数')
  60. .positive('城市ID必须是正整数')
  61. .default(0)
  62. .openapi({
  63. description: '收货城市ID',
  64. example: 110100
  65. }),
  66. receiverDistrict: z.coerce.number<number>()
  67. .int('区县ID必须是整数')
  68. .positive('区县ID必须是正整数')
  69. .default(0)
  70. .openapi({
  71. description: '收货区县ID',
  72. example: 110105
  73. }),
  74. receiverTown: z.coerce.number<number>()
  75. .int('街道ID必须是整数')
  76. .positive('街道ID必须是正整数')
  77. .default(0)
  78. .openapi({
  79. description: '收货街道ID',
  80. example: 110105001
  81. }),
  82. state: z.coerce.number<number>()
  83. .int('状态必须是整数')
  84. .min(1, '状态最小值为1')
  85. .max(3, '状态最大值为3')
  86. .default(1)
  87. .openapi({
  88. description: '状态:1正常,2禁用,3删除',
  89. example: 1
  90. }),
  91. isDefault: z.coerce.number<number>()
  92. .int('是否默认必须是整数')
  93. .min(0, '最小值为0')
  94. .max(1, '最大值为1')
  95. .default(0)
  96. .openapi({
  97. description: '是否默认地址:0否,1是',
  98. example: 0
  99. }),
  100. createdBy: z.number()
  101. .int('创建人ID必须是整数')
  102. .positive('创建人ID必须是正整数')
  103. .nullable()
  104. .openapi({
  105. description: '创建用户ID',
  106. example: 1
  107. }),
  108. updatedBy: z.number()
  109. .int('更新人ID必须是整数')
  110. .positive('更新人ID必须是正整数')
  111. .nullable()
  112. .openapi({
  113. description: '更新用户ID',
  114. example: 1
  115. }),
  116. createdAt: z.coerce.date('创建时间格式不正确')
  117. .openapi({
  118. description: '创建时间',
  119. example: '2024-01-01T12:00:00Z'
  120. }),
  121. updatedAt: z.coerce.date('更新时间格式不正确')
  122. .openapi({
  123. description: '更新时间',
  124. example: '2024-01-01T12:00:00Z'
  125. }),
  126. user: UserSchema.optional().openapi({
  127. description: '关联用户信息'
  128. }),
  129. province: getAreaSchema.optional().openapi({
  130. description: '关联省份信息'
  131. }),
  132. city: getAreaSchema.optional().openapi({
  133. description: '关联城市信息'
  134. }),
  135. district: getAreaSchema.optional().openapi({
  136. description: '关联区县信息'
  137. }),
  138. town: getAreaSchema.optional().openapi({
  139. description: '关联街道信息'
  140. })
  141. });
  142. // 创建DTO
  143. export const CreateDeliveryAddressDto = z.object({
  144. userId: z.number()
  145. .int('用户ID必须是整数')
  146. .positive('用户ID必须是正整数')
  147. .openapi({
  148. description: '用户ID',
  149. example: 1
  150. }),
  151. name: z.string()
  152. .min(1, '收货人姓名不能为空')
  153. .max(255, '收货人姓名最多255个字符')
  154. .openapi({
  155. description: '收货人姓名',
  156. example: '张三'
  157. }),
  158. phone: z.string()
  159. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  160. .openapi({
  161. description: '收货人手机号',
  162. example: '13800138000'
  163. }),
  164. address: z.string()
  165. .min(1, '详细地址不能为空')
  166. .max(255, '详细地址最多255个字符')
  167. .openapi({
  168. description: '详细地址',
  169. example: '北京市朝阳区建国门外大街1号'
  170. }),
  171. receiverProvince: z.coerce.number<number>()
  172. .int('省份ID必须是整数')
  173. .positive('省份ID必须是正整数')
  174. .default(0)
  175. .openapi({
  176. description: '收货省份ID',
  177. example: 110000
  178. }),
  179. receiverCity: z.coerce.number<number>()
  180. .int('城市ID必须是整数')
  181. .positive('城市ID必须是正整数')
  182. .default(0)
  183. .openapi({
  184. description: '收货城市ID',
  185. example: 110100
  186. }),
  187. receiverDistrict: z.coerce.number<number>()
  188. .int('区县ID必须是整数')
  189. .positive('区县ID必须是正整数')
  190. .default(0)
  191. .openapi({
  192. description: '收货区县ID',
  193. example: 110105
  194. }),
  195. receiverTown: z.coerce.number<number>()
  196. .int('街道ID必须是整数')
  197. .positive('街道ID必须是正整数')
  198. .default(0)
  199. .openapi({
  200. description: '收货街道ID',
  201. example: 110105001
  202. }),
  203. isDefault: z.coerce.number<number>()
  204. .int('是否默认必须是整数')
  205. .min(0, '最小值为0')
  206. .max(1, '最大值为1')
  207. .default(0)
  208. .openapi({
  209. description: '是否默认地址:0否,1是',
  210. example: 0
  211. })
  212. });
  213. // 更新DTO
  214. export const UpdateDeliveryAddressDto = z.object({
  215. name: z.string()
  216. .min(1, '收货人姓名不能为空')
  217. .max(255, '收货人姓名最多255个字符')
  218. .optional()
  219. .openapi({
  220. description: '收货人姓名',
  221. example: '张三'
  222. }),
  223. phone: z.string()
  224. .regex(/^1[3-9]\d{9}$/, '请输入正确的手机号')
  225. .optional()
  226. .openapi({
  227. description: '收货人手机号',
  228. example: '13800138000'
  229. }),
  230. address: z.string()
  231. .min(1, '详细地址不能为空')
  232. .max(255, '详细地址最多255个字符')
  233. .optional()
  234. .openapi({
  235. description: '详细地址',
  236. example: '北京市朝阳区建国门外大街1号'
  237. }),
  238. receiverProvince: z.coerce.number<number>()
  239. .int('省份ID必须是整数')
  240. .positive('省份ID必须是正整数')
  241. .optional()
  242. .openapi({
  243. description: '收货省份ID',
  244. example: 110000
  245. }),
  246. receiverCity: z.coerce.number<number>()
  247. .int('城市ID必须是整数')
  248. .positive('城市ID必须是正整数')
  249. .optional()
  250. .openapi({
  251. description: '收货城市ID',
  252. example: 110100
  253. }),
  254. receiverDistrict: z.coerce.number<number>()
  255. .int('区县ID必须是整数')
  256. .positive('区县ID必须是正整数')
  257. .optional()
  258. .openapi({
  259. description: '收货区县ID',
  260. example: 110105
  261. }),
  262. receiverTown: z.coerce.number<number>()
  263. .int('街道ID必须是整数')
  264. .positive('街道ID必须是正整数')
  265. .optional()
  266. .openapi({
  267. description: '收货街道ID',
  268. example: 110105001
  269. }),
  270. state: z.coerce.number<number>()
  271. .int('状态必须是整数')
  272. .min(1, '状态最小值为1')
  273. .max(3, '状态最大值为3')
  274. .optional()
  275. .openapi({
  276. description: '状态:1正常,2禁用,3删除',
  277. example: 1
  278. }),
  279. isDefault: z.coerce.number<number>()
  280. .int('是否默认必须是整数')
  281. .min(0, '最小值为0')
  282. .max(1, '最大值为1')
  283. .optional()
  284. .openapi({
  285. description: '是否默认地址:0否,1是',
  286. example: 0
  287. })
  288. });