Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
La publicación HTTP con el cuerpo de la solicitud falló al usar Regex para validar el parámetro del cuerpo

Estoy probando una API Express usando el método de publicación.

El cuerpo de la solicitud es como

 { "menuId": "id2-test", "menuName": "test menu name 2", "menuUrl": "/admin/menu/test", "desc": "this is test menu 2 description", "menuOrder": 2, "usageStatus": "true" }

Como puede ver, hay una propiedad menuUrl que es una ruta de una URL válida. Entonces, para asegurarse de que el usuario pueda ingresar el valor válido para todas las propiedades proporcionadas. He intentado validar el menuUrl usando Regex

 router.post( "/", validate(paramValidation.createMenu), async (req, res, next) => { const { userInfo } = req; const menu = new Menus({ menuUrl: req.body.menuUrl, ...req.body, }); // Regex validation is here if (!menu?.menuUrl.match(/^[az][a-z0-9+.-]*:/)) { return new ApiError("MENU_URL_NOT_VALID", httpStatus.BAD_REQUEST, true); } menu.createdBy = userInfo.userNm; await Menus.findOne({ menuId: menu.menuId }) .then((_menu) => { if (!_menu) { return menu.save(); } const err = new ApiError("ROLE_ALREADY", httpStatus.BAD_REQUEST, true); return Promise.reject(err); }) .then((_menu) => { res.status(httpStatus.CREATED).json(_menu); return null; }) .catch((e) => next(e)); } );

Sin embargo, cuando se realiza la solicitud, recibí el error que decía que could not get response . Si elimino el paso de validación Regex anterior, todo sigue funcionando bien. ¿Qué pasó con eso?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El patrón de expresiones regulares que parece querer aquí es algo como:

 ^/[az][a-z0-9+.-]*(?:/[az][a-z0-9+.-]*)*$

Código actualizado:

 if (!menu?.menuUrl.match(/^\/[az][a-z0-9+.-]*(?:\/[az][a-z0-9+.-]*)*$/)) { return new ApiError("MENU_URL_NOT_VALID", httpStatus.BAD_REQUEST, true); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!