json 架构来验证所需标签

问题描述 投票:0回答:2

我正在使用 json 模式草案 4,并且我正在尝试有条件地添加对所需标签的要求。对于 env 标签中的任何值,都需要 1/2/3,但如果该值为 sat 或 uat,则还必须需要另一个标签、日期。

    "date": {
      "type": [
        "string"
      ]
    }
  },
  "anyOf": [
    {
      "not": {
        "properties": { "env": { "enum": ["sit", "uat"] } },
        "required": ["label1","label2","label3]
        }
    },
    { "required": ["date"] }
  ],

我基于此处记录的含义:https://json-schema.org/understanding-json-schema/reference/conditionals

json jsonschema json-schema-validator
2个回答
0
投票

这将满足您的要求。

如果

date
只允许与
sit
uat
一起使用,那么您可以稍微修改一下,以将
date
限制在第二个
required
模式中的
oneOf
数组中。

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "required": [
        "env",
        "label1",
        "label2",
        "label3"
    ],
    "type": "object",
    "properties": {
        "env": {
            "type": "string"
        },
        "label1": {
            "type": "string"
        },
        "label2": {
            "type": "string"
        },
        "label3": {
            "type": "string"
        }
    },
    "oneOf": [
        {
            "required": [
                "date",
                "env"
            ],
            "type": "object",
            "properties": {
                "env": {
                    "type": "string",
                    "enum": [
                        "sit",
                        "uat"
                    ]
                },
                "date": {
                    "type": "string"
                }
            }
        },
        {
            "not": {  <-- optional depending on your `date` requirement
                "required": [
                    "date"
                ]
            },
            "type": "object",
            "properties": {
                "env": {
                    "type": "string",
                    "enum": [
                        "dev",
                        "prod"
                    ]
                }
            }
        }
    ]
}

0
投票

另一种方法是在

oneOf

中重新定义整个模式
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "oneOf": [
        {
            "required": [
                "date",
                "env",
                "label1",
                "label2",
                "label3"
            ],
            "properties": {
                "date": {
                    "type": "string"
                },
                "env": {
                    "enum": [
                        "sit",
                        "uat"
                    ]
                },
                "label1": {
                    "type": "string"
                },
                "label2": {
                    "type": "string"
                },
                "label3": {
                    "type": "string"
                }
            }
        },
        {
            "required": [
                "env",
                "label1",
                "label2",
                "label3"
            ],
            "properties": {
                "env": {
                    "enum": [
                        "prod",
                        "dev"
                    ]
                },
                "label1": {
                    "type": "string"
                },
                "label2": {
                    "type": "string"
                },
                "label3": {
                    "type": "string"
                }
            }
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.