Explorar o código

♻️ refactor(users): add schema validation for user API responses

- import parseWithAwait utility for schema validation
- apply schema validation to createUser API response using UserSchema
- apply schema validation to getUserById API response using UserSchema
yourname hai 2 meses
pai
achega
65ce9c7839
Modificáronse 1 ficheiros con 3 adicións e 2 borrados
  1. 3 2
      src/server/api/users/custom.ts

+ 3 - 2
src/server/api/users/custom.ts

@@ -6,6 +6,7 @@ import { ErrorSchema } from '../../utils/errorHandler';
 import { AppDataSource } from '../../data-source';
 import { AuthContext } from '../../types/context';
 import { CreateUserDto, UpdateUserDto, UserSchema } from '../../modules/users/user.schema';
+import { parseWithAwait } from '../../utils/parseWithAwait';
 
 // 创建用户路由 - 自定义业务逻辑(密码加密等)
 const createUserRoute = createRoute({
@@ -112,7 +113,7 @@ const app = new OpenAPIHono<AuthContext>()
       const userService = new UserService(AppDataSource);
       const result = await userService.createUser(data);
 
-      return c.json(result, 201);
+      return c.json(await parseWithAwait(UserSchema, result), 201);
     } catch (error) {
       if (error instanceof z.ZodError) {
         return c.json({
@@ -138,7 +139,7 @@ const app = new OpenAPIHono<AuthContext>()
         return c.json({ code: 404, message: '资源不存在' }, 404);
       }
 
-      return c.json(result, 200);
+      return c.json(await parseWithAwait(UserSchema, result), 200);
     } catch (error) {
       if (error instanceof z.ZodError) {
         return c.json({