【问题标题】:Can 'if, then and else' keywords be applied within properties of an object or is it strictly kept outside?'if, then and else' 关键字可以应用在对象的属性中还是严格保留在对象的属性之外?
【发布时间】:2019-03-30 06:44:08
【问题描述】:

https://json-schema.org/understanding-json-schema/reference/conditionals.html URL 中提到的所有示例都具有对象属性之外的“if、then 和 else”条件。我只是想知道它是否可以在属性中设置并仍然遵循标准?

{
  "type": "object",
  "properties": {
    "street_address": {
      "type": "string"
    },
    "country": {
      "enum": ["United States of America", "Canada"]
    }
  },
  "if": {
    "properties": { "country": { "const": "United States of America" } }
  },
  "then": {
    "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
  },
  "else": {
    "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
  }
}

这样可以吗?

{
  "type": "object",
  "properties": {
    "street_address": {
      "type": "string"
    },
    "country": {
      "enum": ["United States of America", "Canada"]
    },
    "if": {
      "properties": { "country": { "const": "United States of America" } }
    },
    "then": {
      "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
    },
    "else": {
     "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
    }
  }
}

我在网上尝试了 JSON-schema lint,它没有引发任何错误,但是我不知道它的实现,因此不太确定语法是否与建议的 draft-7 匹配。

在对象的属性中这样做的动机是: 尤其是在大型模式文件中,如果我可以定义一个属性,其值有条件地依赖于它旁边的一些其他属性,那感觉会更好。而不是先完成所有属性,然后在属性之外单独应用条件。

对编程非常陌生 - 如果这是一个非常不正确的问题,请原谅我。 谢谢。

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    所有关键字,包括if/then/else 都可以出现在任何架构中。 properties 关键字的值被定义为一个object,它的值是模式。因此,关键字不能用作properties 的子项,但可以用于其中一个值。

    如果你这样做......

    {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "country": { "enum": ["United States of America", "Canada"] },
        "if": {
          "properties": { "country": { "const": "United States of America" } }
        },
        "then": {
          "properties": { "postal_code": { "pattern": "[0-9]{5}(-[0-9]{4})?" } }
        },
        "else": {
         "properties": { "postal_code": { "pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]" } }
        }
      }
    }
    

    它描述了一个像...这样的对象

    {
      "street_address": "123 Fake St",
      "country": "United States of America",
      "if": { "country": "United States of America" },
      "then": { "postal_code": "90000" },
      "else": { "postal_code": "A0A 0A0" }
    }
    

    请注意,“if”、“then”、“else”被理解为属性名称,而不是关键字。同样,这是因为 properties 的值不是模式,它是一个对象,其值是模式。该架构恰好是一个有效的架构,它只是没有按照您的预期执行。

    因此你可以做这样的事情......

    {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "country": { "enum": ["United States of America", "Canada"] },
        "postal_code": {
          "if": { ... },
          "then": { ... },
          "else": { ... }
        }
      }
    }
    

    在这种情况下,它会将if/then/else 识别为关键字,但是无法编写此条件来执行您正在尝试执行的操作。你必须把它放在顶层,就像你看到的例子所做的那样。

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 2014-04-11
      • 2023-01-19
      • 2019-10-12
      • 2013-03-10
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      相关资源
      最近更新 更多