【问题标题】:Dependency on the existence of a nested value依赖嵌套值的存在
【发布时间】:2021-05-15 06:45:44
【问题描述】:

我可以让dependencies 使用非嵌套值(架构中的顶级),但我希望依赖项依赖于嵌套在object 定义中的属性。

const schema = {
  type: "object",
  properties: {
    start: {
      type: "object",
      properties: {
        time: {
          type: "string"
        }
      }
    }
  },

  dependencies: {
    start: {
      properties: {
        end: {
          type: "object",
          properties: {
            time: {
              type: "string"
            }
          }
        }
      }
    }
  }
};

基本上,我想要一种方法让依赖项依赖于start 属性,而是依赖于start.time 属性。

我查看了this 问题,答案指出架构无法“向上”查找树,尽管我不完全确定“向上”和“向下”在这种情况下的含义。

在我看来,如果依赖项可以依赖于start,并且timestart 中的一个属性,那么time 是“向下”树,而不是“向上”(?)

【问题讨论】:

    标签: jsonschema jsonforms


    【解决方案1】:

    dependencies 关键字的存在是为了简化特定用例的架构。它为更简单的语法牺牲了一些灵活性和表现力。尽管您的用例非常相似,但它超出了dependencies 的能力范围。好消息是有办法做到这一点。

    这个,

    {
      "dependencies": {
        "some-propery-name": { ... some-schema ... }
      }
    }
    

    等价于,

    {
      "if": { "required": ["some-property-name"] },
      "then": { ... some-schema ... }
    }
    

    您可以从那里推断来描述您的用例。

    {
      "if": {
        "properties": {
          "start": { "required": ["time"] }
        },
        "required": ["start"]
      },
      "then": { ... some-schema ... }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多