File size: 955 Bytes
656c481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Request, Response, NextFunction } from "express";
import { z } from "zod";

const pingMessageSchema = z.object({
  message: z
    .string()
    .min(1, "Message is required and must be a non-empty string"),
  numbers: z
    .array(
      z
        .string()
        .min(12, "Each number must be at least 12 characters long")
        .regex(
          /^\d{12}$/,
          "Invalid phone number format. Correct example: 123456789012"
        )
    )
    .max(5, "You can provide a maximum of 5 phone numbers"),
  image: z.string().optional()
});

const validatePingMessage = (
  req: Request,
  res: Response,
  next: NextFunction
): void => {
  try {
    pingMessageSchema.parse(req.body);
    next();
  } catch (error) {
    if (error instanceof z.ZodError) {
      res.status(400).json({ errors: error.errors });
    } else {
      res.status(500).json({ message: "Internal Server Error" });
    }
  }
};

export default validatePingMessage;