How do I create a function where a key of an object is dynamically set from the function
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(),
});
};
how do I make the key be the key of the object
Maybe you can try the following:
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);
}
Cheers
I've been able to find a way out using the code snippet below
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);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.