我用来Joi根据模式验证我的输入。
如何访问函数foo内部的值custom?
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 });
});
Run Code Online (Sandbox Code Playgroud)
如果您想访问其他密钥,您需要向对象添加自定义验证:
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") {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2107 次 |
| 最近记录: |