Explorar el Código

✨ feat(routes): 为车型和出行方式添加参数验证

- 为vehicleType参数添加refine验证,确保只能包含bus/minibus/car/business类型
- 为travelMode参数添加refine验证,确保只能包含carpool/charter类型
- 增强API参数合法性校验,提供明确的错误提示信息
yourname hace 3 meses
padre
commit
6bfa204c70
Se han modificado 1 ficheros con 18 adiciones y 8 borrados
  1. 18 8
      src/server/api/routes/index.ts

+ 18 - 8
src/server/api/routes/index.ts

@@ -38,14 +38,24 @@ const searchRoutesSchema = z.object({
     example: 500,
     description: '最高价格'
   }),
-  vehicleType: z.string().optional().openapi({
-    example: 'bus',
-    description: '车型,支持逗号分隔的多个值,如:bus,business'
-  }),
-  travelMode: z.string().optional().openapi({
-    example: 'carpool',
-    description: '出行方式,支持逗号分隔的多个值,如:carpool,charter'
-  }),
+  vehicleType: z.string().optional()
+    .refine(
+      (val) => !val || val.split(',').every(type => Object.values(VehicleType).includes(type as VehicleType)),
+      { message: '车型必须是有效的类型(bus/minibus/car/business)' }
+    )
+    .openapi({
+      example: 'bus',
+      description: '车型,支持逗号分隔的多个值,如:bus,business'
+    }),
+  travelMode: z.string().optional()
+    .refine(
+      (val) => !val || val.split(',').every(mode => Object.values(TravelMode).includes(mode as TravelMode)),
+      { message: '出行方式必须是有效的类型(carpool/charter)' }
+    )
+    .openapi({
+      example: 'carpool',
+      description: '出行方式,支持逗号分隔的多个值,如:carpool,charter'
+    }),
   sortBy: z.enum(['price', 'departureTime']).default('departureTime').openapi({
     example: 'departureTime',
     description: '排序字段'