【问题标题】:json schemas: Is type necessary when const or enum are present?json 模式:存在 const 或 enum 时是否需要类型?
【发布时间】:2022-10-13 12:37:43
【问题描述】:

当一个属性有constenum 时,证明type 有什么好处或坏处?

{
  "type": "object",
  "properties": {
    "has_car": {
      "title": "Do you have a car?",
      "enum": ["yes", "no"],
      "type": "string",
      "$comment": "Do I need type here?"
    },
    "car_brand": {
      "title": "What's your car brand?",
      "type": "string"
    },
    "terms": {
      "title": "I accept my car terms",
      "const": "acknowledged",
      "$comment": "Do I need type here?"
    }
  }
}

【问题讨论】:

    标签: jsonschema json-schema-validator


    【解决方案1】:

    真的没有任何好处。 constenum 验证确切的值,所以 type 没有添加任何内容,正如您所怀疑的那样。

    【讨论】:

    • 只是为了澄清最后一件事:官方文档说:“根据此数据模型解释的 JSON 值称为“实例”。[...] 实例具有六种原始类型中的一种,并且可能值的范围取决于关于类型”。因此,通过阅读此内容,它隐含地表明该类型是必需的。但你是说它没有增加任何东西。所以……你能澄清一下吗?谢谢@gregsdennis!
    【解决方案2】:

    不,它不是必需的,因为关键字是单独应用的。根据使用模式的内容,它可以帮助定义两者。对于代码生成,添加类型会有所帮助,因为可以使用该信息限制允许的输入类型。

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多