繁体   English   中英

如何在 joi 中访问自定义 function 中的另一个字段?

[英]how to access another field inside custom function in joi?

我使用Joi针对架构验证我的输入。

如何访问custom function 中的foo值?

代码框.io

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM