DEV Community

Kazi Abdur Rakib
Kazi Abdur Rakib

Posted on

Implement validateRequest Middleware

const validateRequest = (Schema: AnyZodObject) => {
  return async (req: Request, res: Response, next: NextFunction) => {
    //validate

    // Validate
    try {
      await Schema.parseAsync({
        body: req.body,
      });
      next();
    } catch (err) {
      next(err);
    }
  };
};
router.post(
  '/create-student',
  validateRequest(studentValidations.studentValidationSchema),
  userControllers.createStudent,
);
Enter fullscreen mode Exit fullscreen mode
const studentValidationSchema = z.object({
  body: z.object({
    password: z.string().min(1),
    student: z.object({
      name: UserNameValidationSchema,
      gender: z.enum(['male', 'female', 'other']),
      dateofBirth: z.string(),
      email: z.string().email(),
      contactNo: z.string().min(1),
      emergenyContractNo: z.string().min(1),
      bloodGroup: z.enum(['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-']),
      presentAddress: z.string().min(1),
      permanentAddress: z.string().min(1),
      guardian: GurdianValidationSchema,
      localguardin: LocalGuardianValidationSchema,
      profileImg: z.string().optional(),
    }),
  }),
});

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay