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

Migrate joinProject to zod

GitOrigin-RevId: 8c03002adfdf47a7f83ad3dde32428d29abd9bb9
Eric Mc Sween 1 год назад
Родитель
Сommit
53af983981

+ 14 - 2
services/web/app/src/Features/Editor/EditorHttpController.mjs

@@ -11,6 +11,7 @@ import Errors from '../Errors/Errors.js'
 import { expressify } from '@overleaf/promise-utils'
 import Settings from '@overleaf/settings'
 import CollaboratorsGetter from '../Collaborators/CollaboratorsGetter.js'
+import { z, zz, validateReq } from '../../infrastructure/Validation.js'
 
 const ProjectAccess = CollaboratorsGetter.ProjectAccess
 
@@ -27,9 +28,20 @@ export default {
   _nameIsAcceptableLength,
 }
 
+const joinProjectSchema = z.object({
+  params: z.object({
+    Project_id: zz.objectId(),
+  }),
+  body: z.object({
+    userId: z.string(),
+    anonymousAccessToken: z.string().optional(),
+  }),
+})
+
 async function joinProject(req, res, next) {
-  const projectId = req.params.Project_id
-  let userId = req.body.userId // keep schema in sync with router
+  const { params, body } = validateReq(req, joinProjectSchema)
+  const projectId = params.Project_id
+  let userId = body.userId
   if (userId === 'anonymous-user') {
     userId = null
   }

+ 0 - 7
services/web/app/src/Features/Editor/EditorRouter.mjs

@@ -3,7 +3,6 @@ import AuthenticationController from '../Authentication/AuthenticationController
 import AuthorizationMiddleware from '../Authorization/AuthorizationMiddleware.mjs'
 import { RateLimiter } from '../../infrastructure/RateLimiter.js'
 import RateLimiterMiddleware from '../Security/RateLimiterMiddleware.js'
-import { validate, Joi } from '../../infrastructure/Validation.js'
 
 const rateLimiters = {
   addDocToProject: new RateLimiter('add-doc-to-project', {
@@ -74,12 +73,6 @@ export default {
         // keep schema in sync with controller
         getUserId: req => req.body.userId,
       }),
-      validate({
-        body: Joi.object({
-          userId: Joi.string().required(),
-          anonymousAccessToken: Joi.string().optional(),
-        }),
-      }),
       EditorHttpController.joinProject
     )
   },