|
|
@@ -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: '更新后的备注内容'
|
|
|
})
|
|
|
-});
|
|
|
+});
|