delivery-address.mt.schema.ts 8.1 KB

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