Validation Hapijs Joi引用模式键以在其他模型或管线中重用
我有一个这样构造的示例模型:Validation Hapijs Joi引用模式键以在其他模型或管线中重用,validation,routes,schema,joi,hapi,Validation,Routes,Schema,Joi,Hapi,我有一个这样构造的示例模型: const MyModelResponse = Joi.object().keys({ id: Joi.number().integer().required() .description('ID of the example model'), description: Joi.string() .description('Description of the example model'), }) .description('examp
const MyModelResponse = Joi.object().keys({
id: Joi.number().integer().required()
.description('ID of the example model'),
description: Joi.string()
.description('Description of the example model'),
})
.description('example instance of MyModel with the unique ID present')
.label('MyModelResponse');
在我的路线中,我希望确保我的输入参数根据MyModeResponse的id属性进行验证,如下所示:
validate: {
params: {
id: MyModelResponse.id,
},
options: { presence: 'required' },
failAction: failAction('request'),
}
这将导致服务器启动时出现架构验证错误:
AssertionError [ERR_ASSERTION]: Invalid schema content: (id)
是否有方法引用架构的键?目前,我不得不采取以下措施之一:
根本不引用MyModelResponse架构:
validate: {
params: {
id: Joi.number().integer().description('id of MyModel instance to get'),
},
options: { presence: 'required' },
failAction: failAction('request'),
}
不使用Joi.object.keys()构造函数来定义我的模型,如下所示:
const MyModelResponse = {
id: Joi.number().integer().required()
.description('ID of the example model'),
description: Joi.string()
.description('Description of the example model'),
}
底部方法允许我引用路由中的id属性,但不允许我向模式中添加描述和标签。我尝试在路由验证中使用MyModel.descripe().children.id,并尝试将id对象反序列化为schema对象,但没有成功
如果您有任何建议,我们将不胜感激。删除密钥()并按如下方式使用
const MyModelResponse = Joi.object({
id: Joi.number().integer().required()
.description('ID of the example model'),
description: Joi.string()
.description('Description of the example model'),
})
.description('example instance of MyModel with the unique ID present')
.label('MyModelResponse');