ソースを参照

♻️ refactor(stock): 分离股票训练代码的Schema定义

- 将StockXunlianCodesSchema从实体文件移至独立的schema文件
- 为所有字段添加更严格的验证规则和错误提示
- 优化Schema的OpenAPI文档描述和示例值
- 移除实体文件中与数据验证无关的代码,提高关注点分离
yourname 6 ヶ月 前
コミット
02d487b533

+ 1 - 32
src/server/modules/stock/stock-xunlian-codes.entity.ts

@@ -1,5 +1,4 @@
 import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
-import { z } from '@hono/zod-openapi';
 
 @Entity('stock_xunlian_codes')
 export class StockXunlianCodes {
@@ -29,34 +28,4 @@ export class StockXunlianCodes {
 
   @Column({ name: 'updated_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
   updatedAt!: Date;
-}
-
-export const StockXunlianCodesSchema = z.object({
-  id: z.number().int().positive().openapi({ description: 'ID', example: 1 }),
-  code: z.string().max(255).openapi({ description: '股票代码', example: '001339' }),
-  stockName: z.string().max(255).openapi({ description: '股票名称', example: 'test01' }),
-  name: z.string().max(255).openapi({ description: '案例名称', example: 'test222' }),
-  type: z.string().max(255).nullable().openapi({ description: '案例类型', example: '技术分析' }),
-  description: z.string().max(255).nullable().openapi({ description: '案例描述', example: '这是一个测试案例' }),
-  tradeDate: z.date().openapi({ description: '交易日期', example: '2025-05-21T08:00:00Z' }),
-  createdAt: z.date().openapi({ description: '创建时间', example: '2025-05-22T16:58:01Z' }),
-  updatedAt: z.date().openapi({ description: '更新时间', example: '2025-05-22T17:19:32Z' })
-});
-
-export const CreateStockXunlianCodesDto = z.object({
-  code: z.string().max(255).openapi({ description: '股票代码', example: '001339' }),
-  stockName: z.string().max(255).openapi({ description: '股票名称', example: 'test01' }),
-  name: z.string().max(255).openapi({ description: '案例名称', example: 'test222' }),
-  type: z.string().max(255).nullable().openapi({ description: '案例类型', example: '技术分析' }),
-  description: z.string().max(255).nullable().optional().openapi({ description: '案例描述', example: '这是一个测试案例' }),
-  tradeDate: z.coerce.date().openapi({ description: '交易日期', example: '2025-05-21T08:00:00Z' })
-});
-
-export const UpdateStockXunlianCodesDto = z.object({
-  code: z.string().max(255).optional().openapi({ description: '股票代码', example: '001339' }),
-  stockName: z.string().max(255).optional().openapi({ description: '股票名称', example: 'test01' }),
-  name: z.string().max(255).optional().openapi({ description: '案例名称', example: 'test222' }),
-  type: z.string().max(255).nullable().optional().openapi({ description: '案例类型', example: '技术分析' }),
-  description: z.string().max(255).nullable().optional().openapi({ description: '案例描述', example: '这是一个测试案例' }),
-  tradeDate: z.coerce.date().optional().openapi({ description: '交易日期', example: '2025-05-21T08:00:00Z' })
-});
+}

+ 178 - 0
src/server/modules/stock/stock-xunlian-codes.schema.ts

@@ -0,0 +1,178 @@
+import { z } from '@hono/zod-openapi';
+
+// 股票训练代码实体Schema定义
+export const StockXunlianCodesSchema = z.object({
+  id: z.number()
+    .int('ID必须是整数')
+    .positive('ID必须是正整数')
+    .openapi({
+      description: '股票训练代码ID',
+      example: 1
+    }),
+    
+  code: z.string()
+    .min(1, '股票代码不能为空')
+    .max(255, '股票代码最多255个字符')
+    .openapi({
+      description: '股票代码',
+      example: '000001'
+    }),
+    
+  stockName: z.string()
+    .min(1, '股票名称不能为空')
+    .max(255, '股票名称最多255个字符')
+    .openapi({
+      description: '股票名称',
+      example: '平安银行'
+    }),
+    
+  name: z.string()
+    .min(1, '案例名称不能为空')
+    .max(255, '案例名称最多255个字符')
+    .openapi({
+      description: '案例名称',
+      example: '平安银行2024年走势分析'
+    }),
+    
+  type: z.string()
+    .max(255, '案例类型最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例类型',
+      example: '技术分析'
+    }),
+    
+  description: z.string()
+    .max(255, '案例描述最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例描述',
+      example: '基于MACD指标的技术分析案例'
+    }),
+    
+  tradeDate: z.coerce.date()
+    .openapi({
+      description: '交易日期',
+      example: '2024-01-15'
+    }),
+    
+  createdAt: z.coerce.date()
+    .openapi({
+      description: '创建时间',
+      example: '2024-01-15T10:30:00Z'
+    }),
+    
+  updatedAt: z.coerce.date()
+    .openapi({
+      description: '更新时间',
+      example: '2024-01-15T10:30:00Z'
+    })
+});
+
+// 创建股票训练代码DTO
+export const CreateStockXunlianCodesDto = z.object({
+  code: z.string()
+    .min(1, '股票代码不能为空')
+    .max(255, '股票代码最多255个字符')
+    .openapi({
+      description: '股票代码',
+      example: '000001'
+    }),
+    
+  stockName: z.string()
+    .min(1, '股票名称不能为空')
+    .max(255, '股票名称最多255个字符')
+    .openapi({
+      description: '股票名称',
+      example: '平安银行'
+    }),
+    
+  name: z.string()
+    .min(1, '案例名称不能为空')
+    .max(255, '案例名称最多255个字符')
+    .openapi({
+      description: '案例名称',
+      example: '平安银行2024年走势分析'
+    }),
+    
+  type: z.string()
+    .max(255, '案例类型最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例类型',
+      example: '技术分析'
+    }),
+    
+  description: z.string()
+    .max(255, '案例描述最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例描述',
+      example: '基于MACD指标的技术分析案例'
+    }),
+    
+  tradeDate: z.coerce.date()
+    .openapi({
+      description: '交易日期',
+      example: '2024-01-15'
+    })
+});
+
+// 更新股票训练代码DTO
+export const UpdateStockXunlianCodesDto = z.object({
+  code: z.string()
+    .min(1, '股票代码不能为空')
+    .max(255, '股票代码最多255个字符')
+    .optional()
+    .openapi({
+      description: '股票代码',
+      example: '000001'
+    }),
+    
+  stockName: z.string()
+    .min(1, '股票名称不能为空')
+    .max(255, '股票名称最多255个字符')
+    .optional()
+    .openapi({
+      description: '股票名称',
+      example: '平安银行'
+    }),
+    
+  name: z.string()
+    .min(1, '案例名称不能为空')
+    .max(255, '案例名称最多255个字符')
+    .optional()
+    .openapi({
+      description: '案例名称',
+      example: '平安银行2024年走势分析'
+    }),
+    
+  type: z.string()
+    .max(255, '案例类型最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例类型',
+      example: '技术分析'
+    }),
+    
+  description: z.string()
+    .max(255, '案例描述最多255个字符')
+    .nullable()
+    .optional()
+    .openapi({
+      description: '案例描述',
+      example: '基于MACD指标的技术分析案例'
+    }),
+    
+  tradeDate: z.coerce.date()
+    .optional()
+    .openapi({
+      description: '交易日期',
+      example: '2024-01-15'
+    })
+});