Node.js + Joi how to display a custom error messages?

后端 未结 12 1554
故里飘歌
故里飘歌 2020-12-01 00:56

It seems pretty straight forward to validate user\'s input in Node.js RESTapi with Joi.

But the problem is that my app is not written in English. That m

12条回答
  •  情歌与酒
    2020-12-01 01:19

    In the latest version use message as.

    var schema = Joi.object().keys({
      firstName: Joi.string().min(5).max(10).required().messages({
        "string.base": `"username" should be a type of 'text'`,
        "string.empty": `"username" cannot be an empty field`,
        "any.required": `"username" is a required.`,
      }),
      lastName: Joi.string().min(5).max(10).required().messages({
        "string.base": `"lastName" should be a type of 'text'`,
        "string.empty": `"lastName" cannot be an empty field`,
        "any.required": `"lastName" is a required.`,
      }),
      [...]
    });
    

提交回复
热议问题