2
0
Просмотр исходного кода

✨ feat(stock): 优化日期备注模块的日期格式处理

- 引入dayjs库处理日期格式化
- 添加formatDateToString函数统一日期格式为YYYY-MM-DD
- 修改DateNotesSchema、CreateDateNotesDto和UpdateDateNotesDto中的noteDate字段
  - 使用transform应用日期格式化
  - 更新openapi示例为YYYY-MM-DD格式
  - 移除时间戳格式示例
- 优化代码结构,添加注释说明DTO同样适用日期格式化
yourname 6 месяцев назад
Родитель
Сommit
d0d47e16f7
1 измененных файлов с 27 добавлено и 13 удалено
  1. 27 13
      src/server/modules/stock/date-notes.schema.ts

+ 27 - 13
src/server/modules/stock/date-notes.schema.ts

@@ -1,4 +1,10 @@
 import { z } from '@hono/zod-openapi';
+import dayjs from 'dayjs';
+
+// 使用 dayjs 格式化日期为 YYYY-MM-DD
+const formatDateToString = (date: Date): string => {
+  return dayjs(date).format('YYYY-MM-DD');
+};
 
 export const DateNotesSchema = z.object({
   id: z.number().int('ID必须是整数').positive('ID必须是正整数').openapi({
@@ -9,10 +15,12 @@ export const DateNotesSchema = z.object({
     description: '股票代码',
     example: '600000'
   }),
-  noteDate: z.coerce.date<Date>('备注日期格式不正确').openapi({
-    description: '备注日期',
-    example: '2024-01-15T00:00:00Z'
-  }),
+  noteDate: z.coerce.date<Date>('备注日期格式不正确')
+    .transform(formatDateToString) // 使用 dayjs 转换为 YYYY-MM-DD 格式
+    .openapi({
+      description: '备注日期',
+      example: '2024-01-15'
+    }),
   note: z.string().min(1, '备注内容不能为空').max(255, '备注内容最多255个字符').openapi({
     description: '备注内容',
     example: '重点关注该股票走势'
@@ -27,15 +35,18 @@ export const DateNotesSchema = z.object({
   })
 });
 
+// 对于 DTOs 同样适用
 export const CreateDateNotesDto = z.object({
   code: z.string().min(1, '股票代码不能为空').max(255, '股票代码最多255个字符').openapi({
     description: '股票代码',
     example: '600000'
   }),
-  noteDate: z.coerce.date<Date>('备注日期格式不正确').openapi({
-    description: '备注日期',
-    example: '2024-01-15T00:00:00Z'
-  }),
+  noteDate: z.coerce.date<Date>('备注日期格式不正确')
+    .transform(formatDateToString)
+    .openapi({
+      description: '备注日期',
+      example: '2024-01-15'
+    }),
   note: z.string().min(1, '备注内容不能为空').max(255, '备注内容最多255个字符').openapi({
     description: '备注内容',
     example: '重点关注该股票走势'
@@ -47,12 +58,15 @@ export const UpdateDateNotesDto = z.object({
     description: '股票代码',
     example: '600000'
   }),
-  noteDate: z.coerce.date<Date>('备注日期格式不正确').optional().openapi({
-    description: '备注日期',
-    example: '2024-01-15T00:00:00Z'
-  }),
+  noteDate: z.coerce.date<Date>('备注日期格式不正确')
+    .transform(formatDateToString)
+    .optional()
+    .openapi({
+      description: '备注日期',
+      example: '2024-01-15'
+    }),
   note: z.string().min(1, '备注内容不能为空').max(255, '备注内容最多255个字符').optional().openapi({
     description: '备注内容',
     example: '更新后的备注内容'
   })
-});
+});