简体   繁体   English

为什么会抛出“循环依赖”?

[英]Why does yup throw 'cyclic dependency' at all?

I don't get why yup throws cyclic dependency at all.我不明白为什么 yup 会抛出循环依赖。 When referring to other fields, we are referring to value, not the schema itself aren't we ?当提到其他字段时,我们指的是值,而不是模式本身,不是吗?

More than just something that I don't get, I found myself blocked while requiring a conditional schema validation.不仅仅是我没有得到的东西,我发现自己在需要条件模式验证时被阻止了。

const yup = require("yup");

it("should pass", async () => {
  const schema = yup.object({
    name: yup.string().when("name", {
      is: name => name !== "dummy",
      otherwise: yup.string().email("Email or dummy.")
    })
  });

  const obj = {
    name: "dummy"
  };

  await expect(schema.isValid(obj)).resolves.toBe(true);
});

codesandbox 代码沙盒

Also, is there nothing like or or either into the DSL ?此外,DSL 中是否有类似oreither东西?

The question is really about, why does yup throw cyclic dependency, not about this specific case.问题实际上是关于,为什么会抛出循环依赖,而不是关于这个特定情况。

Getting around the problem with:解决问题:

const schema = yup.object({
    name: yup.string().when([], (v, { originalValue }) => 
      originalValue.name === "dummy" ? yup.string() : yup.email("Email or dummy")
    )
);

But it doesn't look like the natural way, + I still don't get how a schema can generate cyclic dependency.但它看起来不像自然的方式,+我仍然不明白模式如何生成循环依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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