node.jstypescriptexpresszod

Typescript: Zod expected string, received undefined


zod is throwing the following error 3 times when i try to submit my req.body data to prisma orm (im using Insomnia):

ZodError: [
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "undefined",
    "path": [
      "name"
    ],
    "message": "Required"
  },

this is the prisma client model:

model Client {
  id         String          @id @default(uuid())
  email      String          @unique
  name       String
  password   String
  created_at DateTime        @default(now())
  updated_at DateTime        @updatedAt
  reviews    ProductReview[] @relation("client")
  adm        Boolean         @default(false)

  @@map("clients")
}

this is the route:

const authController = new AuthController();

router.route('/register').post(authController.registerClient)

this is the controller with the registerClient function, that is the one that is throwing the zod error:

export class AuthController {
    async registerClient(req: Request, res: Response) {
        const createClientBody = z.object({
            name: z.string(),
            email: z.string(),
            password: z.string(),
        });
        const { name } = createClientBody.parse(req.body); // this lines
        const { email } = createClientBody.parse(req.body); // this lines
        const { password } = createClientBody.parse(req.body); // this lines

        const response = await prisma.client.create({
            data: {
                name,
                email,
                password
            },
        });

        return res.status(201).send({ response });
    };
}

Solution

  • name: z.string().nullish(),

    Allows: string (any length) or null or undefined.

    https://zod.dev/?id=nullish