【发布时间】:2019-06-29 10:28:07
【问题描述】:
我想在 Node.js REST api 中进行用户输入验证。
例如,我有这个架构:
let schema = Joi.object().keys({
client: {
type: Joi.string().valid(["private", "business"]).required().error(JoiCustomErrors)
},
});
现在,如果用户使用 type 作为 private 填写表单,我想将 a 添加到架构中,因此它会查找像这样:
let schema = Joi.object().keys({
client: {
type: Joi.string().valid(["private", "business"]).required().error(JoiCustomErrors),
a: Joi.string().required().error(JoiCustomErrors)
},
});
如果用户在 type 字段中填写 business 我想附加 b 而不是 a(有些示例的选项)。
我试过了:
let b = Joi.string().required().error(JoiCustomErrors);
schema.client.append({b: b}); // 1
schema.client.append(b); // 2
schema.client.b = b; // 3
但是没有任何效果我收到一个未定义的错误:TypeError: Cannot read property 'append' of undefined
【问题讨论】:
标签: javascript node.js schema joi