如何通过 Joi.validate 验证数据库中的唯一性?
How to validate uniqueness in DB via Joi.validate?
是否可以将函数插入 Joi 验证模式,如果值是唯一的,该模式将检查数据库?
(检查将由我在 Joi 函数方法中创建的函数完成)。
也许是这样的:
phone: Joi.customCheck(function(v) {
let bool;
// check in DB if v is already exists, then assign the value to bool...
return bool;
}).required()
我在 Google 和 Joi api 参考资料中进行了搜索,但一无所获。
您可以使用新函数 any().external()
(自版本 16.0.0
起)。这可用于进行数据库查找。
const lookup = async (id) => {
const user = await db.get('user', id);
if (!user) {
throw new Error('Invalid user id');
}
};
const schema = Joi.string().external(lookup);
await schema.validateAsync('1234abcd');
是否可以将函数插入 Joi 验证模式,如果值是唯一的,该模式将检查数据库?
(检查将由我在 Joi 函数方法中创建的函数完成)。
也许是这样的:
phone: Joi.customCheck(function(v) {
let bool;
// check in DB if v is already exists, then assign the value to bool...
return bool;
}).required()
我在 Google 和 Joi api 参考资料中进行了搜索,但一无所获。
您可以使用新函数 any().external()
(自版本 16.0.0
起)。这可用于进行数据库查找。
const lookup = async (id) => {
const user = await db.get('user', id);
if (!user) {
throw new Error('Invalid user id');
}
};
const schema = Joi.string().external(lookup);
await schema.validateAsync('1234abcd');