Node.js Module.exports将两种不同的Joi验证与Hapi.js混合
不确定标题是否准确地解释了问题所在,希望这能让你更好地了解我的感受 我有一个路由,它使用的代码与另一个路由几乎完全相同,只有管理员可以访问它进行更新。所以路由“A”:路由/更新费用有一组Joi验证,我想在路由“B”:路由/更新费用下使用。因此,我创建了一个validations文件夹,并从路由“a”导出了验证。这是下面的文件:Node.js Module.exports将两种不同的Joi验证与Hapi.js混合,node.js,validation,hapijs,joi,Node.js,Validation,Hapijs,Joi,不确定标题是否准确地解释了问题所在,希望这能让你更好地了解我的感受 我有一个路由,它使用的代码与另一个路由几乎完全相同,只有管理员可以访问它进行更新。所以路由“A”:路由/更新费用有一组Joi验证,我想在路由“B”:路由/更新费用下使用。因此,我创建了一个validations文件夹,并从路由“a”导出了验证。这是下面的文件: "use strict"; const Joi = require("joi"); module.exports = { payload: {
"use strict";
const Joi = require("joi");
module.exports = {
payload: {
fees: Joi.object().keys({
default: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees to be charge in any shipment status").min(1),
fees_status: Joi.object().keys({
101: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees for 101 shipment status").min(1),
102: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees for 102 shipment status").min(1),
}).description("Fees customizable for 101 and 102 status").min(1)
}).xor("default", "fees_status")
},
headers: Joi.object({
"csrf-decorator": Joi.string().required(),
"accept-language": Joi.string().required(),
"user-agent": Joi.string().required()
}).options({
allowUnknown: true
})
}
在第二个文件中,我需要路由A,并添加和修改了路由B工作所需的唯一更改:
"use strict";
const Joi = require("joi");
const set_agent_fee_rates_validation = require("./set_agent_fee_rates");
let admin_set_agent_fee_rates_validation = set_agent_fee_rates_validation;
admin_set_agent_fee_rates_validation.payload.agent_id = Joi.string().required();
module.exports = admin_set_agent_fee_rates_validation;
问题是,当我大摇大摆时,我可以看到在两个api端点中都需要agent\u id
,而在路由B中应该只需要它
如果您能帮助我们理解这个问题,我们将不胜感激。谢谢 与其导出一个对象,然后为B修改它,为什么不导出一个函数呢?该函数可以返回一个验证对象,一个用于a,然后一个用于B 另一种选择是在修改验证对象之前对其进行深度克隆 下面是如何使用函数,而不是原始对象文字
const Joi = require("joi");
const make_validator = (optional) => {
const payload = {
fees: Joi.object().keys({
default: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees to be charge in any shipment status").min(1),
fees_status: Joi.object().keys({
101: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees for 101 shipment status").min(1),
102: Joi.array().items(
Joi.object({
period: Joi.number().integer().required().description("Days to consider the fee"),
fee: Joi.number().integer().required().description("Fee's to be charged")
})
).description("Array of fees for 102 shipment status").min(1),
}).description("Fees customizable for 101 and 102 status").min(1)
}).xor("default", "fees_status")
};
if (optional && optional.payload) {
const new_payload = Object.assign({}, payload, optional.payload);
} else {
const new_payload = payload;
}
return {
new_payload,
headers: Joi.object({
"csrf-decorator": Joi.string().required(),
"accept-language": Joi.string().required(),
"user-agent": Joi.string().required()
}).options({
allowUnknown: true
})}
}
module.exports = make_validator
然后,第二个片段变成
const Joi = require("joi");
const make_validator = require("./set_agent_fee_rates");
const set_agent_fee_rates_validation = make_validator();
const admin_set_agent_fee_rates_validation = make_validator({
payload: {
agent_id: Joi.string().required()
}
});
module.exports = admin_set_agent_fee_rates_validation;
我知道你要去哪里,我正要给你一个最好的答案。在阅读了module.exports是如何缓存的之后,我现在已经在代码中这样做了。我觉得选择的答案应该有代码来显示事情是如何实现的。但是如果没有其他人回答这个问题,我会回来选择你作为最好的答案。谢谢你的提示!好的,我会在笔记本电脑前加上。谢谢!