如何创建一个功能,该功能可以根据功能动态设置对象的键
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
return Joi.object({
key: Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required(),
});
};
如何使键成为对象的键
答案 0 :(得分:1)
也许您可以尝试以下方法:
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
let object: any = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
}
欢呼
答案 1 :(得分:1)
我已经可以使用下面的代码片段找到出路
export const validateObjectId = (key: string = 'id'): ObjectSchema => {
interface Obj {
[key: string]: Object;
}
const object: Obj = {};
object[key] = Joi.string()
.regex(/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/i)
.required();
return Joi.object(object);
};