如何为具有不同类型值的一系列字典创建JSON模式

问题描述 投票:0回答:1
现在使用此模式验证:

{ "type": "object", "uniqueItems": true, "properties": { "factor": { "type": "number" }, "partitions": { "type": "number", "minimum": 3, "maximum": 15 }, "configuration": { "type": "array", "uniqueItems": true, "prefixItems": [ { "type": "object", "maxProperties": 2, "properties": { "name": {"type": "string"}, "value": {"type": "number"} } }, { "type": "object", "properties": { "name": {"type": "string"}, "value": {"type": "string"} } }, { "type": "object", "properties": { "name": {"type": "string"}, "value": {"type": "string"} } }, ] } }, "required": [ "partitions_count", "replication_factor", "configs", "topic_name" ] }
我目前面临的问题是订购是固定的。我想将我的配置键中的每个项目验证到其他类型。保留应该是整数,清理应为字符串。我不知道该怎么做。我使用“项目”关键字尝试了一下,但是尽管每个“名称”键都应该具有“字符串”值。 “值”密钥可以具有不同的类要求,该要求取决于名称。有什么想法吗?

json模式并非旨在根据实例数据有条件地验证。但是在某种程度上,通常使用

if-then-else

arrays dictionary jsonschema json-schema-validator
1个回答
1
投票
)或

oneOf/anyOf/allOf

通常与
constpattern
Tutorial
)结合使用:
{
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
  "properties": {
    "configuration": {
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "if": {
              "properties": {
                "name": {
                  "pattern": "cleanup"
                }
              }
            },
            "then": {
              "properties": {
                "value": {
                  "type": "string"
                }
              }            
            },
            "else": false
          },
          {
            "type": "object",
            "if": {
              "properties": {
                "name": {
                  "pattern": "delete.retention"
                }
              }
            },
            "then": {
              "properties": {
                "value": {
                  "type": "null"
                }
              }            
            },
            "else": false
          },
          {
            "type": "object",
            "if": {
              "properties": {
                "name": {
                  "pattern": "retention.ms"
                }
              }
            },
            "then": {
              "properties": {
                "value": {
                  "type": "integer"
                }
              }            
            },
            "else": false
          }
        ]
      }
    }
  }
}
请注意,您可以使用pattern

关键字的任何正则表达式。
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.