【问题标题】:NodeJS joi returns error = null, even though error should occurNodeJS joi 返回 error = null,即使应该发生错误
【发布时间】:2019-02-28 21:52:39
【问题描述】:

我正在运行 NodeJSjoi,并且正在尝试验证 IBM Watson IoT 配置对象。 这是我的架构:

const schema = {
  body: {
    config: Joi.object().keys({
      org: Joi.string().required(),
      type: Joi.string().required(),
      id: Joi.string().required(),
      domain: Joi.string().required(),
      'auth-method': Joi.string().required(),
      'auth-token': Joi.string().required()
    }).required()
  }
};

这是我的支票:

Joi.validate(req.body, schema, { allowUnknown: true }, (err, value) => {
  console.log(err);
});

这会将null 作为error 返回,这表明没有错误,但即使我没有POST 向我的主体添加任何参数。

基本上我想确保我的HTTP POSTbody 包含有效的JSON 对象,如下所示:

config = {
    "org" : "organization",
    "id" : "deviceId",
    "domain": "internetofthings.ibmcloud.com",
    "type" : "deviceType",
    "auth-method" : "token",
    "auth-token" : "authToken"
};

【问题讨论】:

  • 您在尝试打印error时在参数中使用err
  • 对不起,这里的错字。有问题已更正。

标签: javascript node.js joi


【解决方案1】:

您没有将 Joi 对象传递给 validate 函数,您应该传递 schema.body.config 而不是 schema。如果 body 和 config 应该是验证的一部分,你应该用 Joi.Object 包装它们,就像你对 config 中的所有键所做的那样。

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2012-12-08
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2018-07-13
    • 2015-12-04
    相关资源
    最近更新 更多