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

910
Views
How to generate error messages with Zod in different Languages

I want to generate validation messages in different languages. I wanted to create my own middleware that adds a lang attribute to the request object. And after setting lang, I will validate my requests with Zod. I am not able to send the lang as another parameter to schema.parse() method. Here is the code :

ValidateResource

const validate =
    (schema: AnyZodObject) =>
        (req: Request, res: Response, next: NextFunction) => {
            try {
                schema.parse({
                    body: req.body,
                    query: req.query,
                    params: req.params,
                }, );
                next();
            } catch (e: any) {
                if(e instanceof  ZodError) return res.status(StatusCodes.BAD_REQUEST).send(e.flatten());
                return next()
            }
        };

CreateUserSchema



export const createUserSchema = z.object({

    body :  object({
        name : z.string({
            required_error : "Name is required"
        }),
        surname : z.string({
            required_error : "last name is required"
        }),
        password : z.string({
            required_error : "password is required"
        }),
        email : z.string({
            required_error: "Email is required"
        }).email({
            message : "Not a valid email"
        }),
        gender : z.enum([Gender.F,Gender.M]),
        profilePicture : z.string().url().default(config.get<string>("defaultProfilePictureURL")).optional(),
        phoneNumber : z.string({
            required_error : "Phone number is required for singing up"
        }),
        dateOfBirth : z.date().optional(),
    })
})

I have a getMessage method that simply returns an object of message list.


export function getMessage (lang : string) : messages {

    if(lang == "tr"){
        return messagesInTurkish
    }

    return messagesInEnglish
}

export interface messages {
    forbidden : string,
    internalServerError : string,

}

const messagesInTurkish : messages = {
    forbidden: "Yasak Kaynak",
    internalServerError: "Server Hatası"
}

const messagesInEnglish : messages = {
    forbidden: "Forbidden",
    internalServerError: "Internal Server Error"

I want to make CreateUserSchema as

const params = {
    params: object({
        _id: z.string({
            required_error:  getMessage(req.body.lang),
        }),
    }),
};

However, the request object is not carried here. How can I make it dynamic?

over 4 years ago · Santiago Trujillo
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!