[英]how to access another field inside custom function in joi?
我使用Joi
针对架构验证我的输入。
如何访问custom
function 中的foo
值?
import * as Joi from "joi";
console.clear();
const schema = Joi.object({
bar: Joi.string().custom((x) => {
// how to access foo value?
console.log({ x });
}),
foo: Joi.string()
});
schema
.validateAsync({ foo: "myfoo", bar: "mybar" })
.then((x) => {
console.log({ x });
})
.catch((err) => {
console.log({ err });
});
如果您想访问其他一些密钥,您需要将自定义验证添加到 object:
const schema = Joi.object({
bar: Joi.string(),
foo: Joi.string()
}).custom((obj, helpers) => {
// you have access to the full object above.
const { foo, bar } = obj;
if (foo === "myfoo") {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.