简体   繁体   English

如何在JSON模式中使用真实名称定义对象?

[英]How to define an object with a veriable name in JSON Schema?

I want to create a JSON schema for this JSON "pseudo-code" example: 我想为此JSON“伪代码”示例创建JSON模式:

{
    "xyz": {
        "$something": {
            "property_a": "...",
            "property_b": "...",
            "property_c": "..."
        }
    }
}

$something can be one of the following strings: foo , bar , or buz . $something可以是以下字符串之一: foobarbuz My current schema looks like this: 我当前的架构如下所示:

{
  "xyz": {
    "id": "xyz",
    "type": "object",
    "properties": {
      "foo": {
        "id": "foo",
        "type": "object",
        "additionalProperties": false,
        "required": ["property_a"],
        "properties": {
          "property_a": {
            "id": "property_a",
            "type": "string"
          },
          "property_b": {
            "id": "property_b",
            "type": "string"
          },
          "property_c": {
            "id": "property_a",
            "type": "string"
          }
        }
      },
      "bar": {
        ... copy&paste foo
      },
      "buz": {
        ... copy&paste foo
      }
    }
  }
}

It's working, but it's a lot duplicated code. 它正在工作,但是有很多重复的代码。 So I'm looking for a more elegant way for implementing it. 因此,我正在寻找一种更优雅的实现方式。

How to define a list of values (lie enum ) allowed as name for a property in JSON Schema? 如何定义允许作为JSON模式中的属性名称的值列表(lie enum )?

patternProperties works like properties, except the keys of the object are regular expressions. patternProperties工作方式类似于属性,只是对象的键是正则表达式。

https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5.5 https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5.5

An example from the Understanding JSON Schema site 理解JSON模式站点中的示例

{
  "type": "object",
  "patternProperties": {
    "^S_": { "type": "string" },
    "^I_": { "type": "integer" }
  },
  "additionalProperties": false
}

In this example, any additional properties whose names start with the prefix S_ must be strings, and any with the prefix I_ must be integers. 在此示例中,任何名称以前缀S_开头的其他属性都必须是字符串,而任何以前缀I_开头的属性都必须是整数。 Any properties explicitly defined in the properties keyword are also accepted, and any additional properties that do not match either regular expression are forbidden. 还接受在properties关键字中显式定义的任何属性,并且禁止与两个正则表达式都不匹配的任何其他属性。

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

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