Reputation: 1
I tried sending in postman a form-data with 2 fields, userProData with the Json data of the user, and imageProfile as a file.
I cant find how to send in a single request both body json with user data and a image file. Its possible to manage images and json data in one endpoint or i need a separate for the image and another for the data? i actually solve this but with 2 endpoints. One with multerMdw and the other one a normal saveUser for json data. But i have to update the user with the image of the other endpoint, i cant do it in one controller or endpoint at same time. Express-validator dont let it pass. I tried to fill form-data in postman with each value in a single field as Text Application/Json plus the image field as FILE but doesnt works :(
ROUTE:
const multerMdw = uploadImageMdw.single('imageProfile') // mdw de carga de imagen
// User PRO -> el id es del user básico asociado a su perfil PRO
userRouter.route('/:id')
.post(authenticate, idByParameterValidator, validateUserPro, multerMdw, handleValidationErrors, registerUserPro)
VALIDATOR:
import { body, ValidationChain } from "express-validator";
import { PaymentMethod, Sector, Tool } from "../models/enum/enum";
export const validateUserPro: ValidationChain[] = [
body("firstName")
.isString()
.notEmpty()
.withMessage("El nombre es obligatorio"),
body("lastName")
.isString()
.notEmpty()
.withMessage("El apellido es obligatorio"),
body("description")
.optional()
.isString()
.isLength({ max: 500 })
.withMessage("La descripción no debe exceder los 500 caracteres"),
body("country")
.optional()
.isString()
.withMessage("El país debe ser un string válido")
];
POSTMAN:
in body, form-data i create 2 keys (1- imageProfile: file | 2- userProData: text COntent-type: Application/JSON with the json within)
No field is recognized by validator.....
I tried to change body for check in validator
Also i tried to set the validator this way to consider the userProData field sended in postman as key
import { body, ValidationChain } from "express-validator";
import { PaymentMethod, Sector, Tool } from "../models/enum/enum";
export const validateUserPro: ValidationChain[] = [
body("userProData")
.exists()
.withMessage("El campo userProData es obligatorio")
.bail()
.isJSON()
.withMessage("El campo userProData debe ser un JSON válido")
.bail(),
body("userProData.firstName")
.exists()
.withMessage("El nombre es obligatorio")
.bail()
.isString()
.withMessage("El nombre debe ser un string"),
body("userProData.lastName")
.exists()
.withMessage("El apellido es obligatorio")
.bail()
.isString()
.withMessage("El apellido debe ser un string"),
body("userProData.description")
.optional()
.isString()
.isLength({ max: 500 })
.withMessage("La descripción no debe exceder los 500 caracteres"),
body("userProData.country")
.optional()
.isString()
.withMessage("El país debe ser un string válido"),
];
Upvotes: 0
Views: 11