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

后端 未结 12 1575
故里飘歌
故里飘歌 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:30

    You can use .error(new Error('message')), And its work for me

    var schema = Joi.object().keys({
      firstName: Joi.string().min(5).max(10).required().error(new Error('Give your error message here for first name')),
      lastName: Joi.string().min(5).max(10).required().error(new Error('Give your error message here for last name'))
      ..
    });
    
    Joi.validate(req.body, schema, function(err, value) {
      if (err) {
        console.log(err.message)
        return catched(err.message); 
      }
    });
    

提交回复
热议问题