使用 if 子句进行 JSON 模式验证,为同一对象选择不同的属性

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

我喜欢为这种 yml 编写 JSON 模式验证

stages:

  - template: template-a.yml
    parameters:
      a-param1: abc
      a-param2: xy

  - template: template-b.yml
    parameters:
      b-param1: df
      b-param2: yz

因此,根据属性“template”的值,属性对象“parameters”应该具有不同的属性。

我尝试了类似的方法,但是 if/then 不适用于对象属性。 (如果该属性与“模板”处于同一级别,则它正在工作)

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$ref": "#/definitions/azure-templates",
    "definitions": {
        "azure-templates": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "stages": {
                    "type": "array",
                    "items": {
                        "anyOf": [
                            {
                                "$ref": "#/definitions/template-select"
                            }
                        ]
                    }
                }
            },
            "required": [
                "stages"
            ],
            "title": "azure-pipeline"
        },
        "template-params-a": {
            "if": {
                "properties": {
                    "template": {
                        "const": "template-a.yml"
                    }
                },
                "required": [
                    "template"
                ]
            },
            "then": {
                "properties": {
                    "parameters": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "a-param1": {
                                "type": "string"
                            },
                            "a-param2": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        },
        "template-params-b": {
            "if": {
                "properties": {
                    "template": {
                        "const": "template-b.yml"
                    }
                },
                "required": [
                    "template"
                ]
            },
            "then": {
                "properties": {
                    "parameters": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "b-param1": {
                                "type": "string"
                            },
                            "b-param2": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        },
        "template-select": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "template": {
                    "enum": [
                        "template-a.yml",
                        "template-b.yml"
                    ]
                }
            },
            "allOf": [
                {
                    "$ref": "#/definitions/template-params-a",
                    "$ref": "#/definitions/template-params-b"
                }
            ],
            "required": [
                "template"
            ],
            "title": "Template"
        }
    }
}
json if-statement jsonschema
1个回答
0
投票
  • 根部
    $ref
    应包裹在
    allOf
  • 您的
    additionalProperties:false
    模式中的
    template-select
    会阻止使用
    parameters
    ,因为在 Draft-07 中,如果模式中没有一些体操,
    allOf
    additionalProperties:false
    就无法很好地协同工作
  • 我删除了
    anyOf
    模式中的
    items
    ,单个模式引用没有必要。
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "allOf": [
        {
            "$ref": "#/definitions/azure-templates"
        }
    ],
    "definitions": {
        "azure-templates": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "stages": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/template-select"
                    }
                }
            },
            "required": [
                "stages"
            ],
            "title": "azure-pipeline"
        },
        "template-params-a": {
            "if": {
                "properties": {
                    "template": {
                        "const": "template-a.yml"
                    }
                },
                "required": [
                    "template"
                ]
            },
            "then": {
                "properties": {
                    "parameters": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "a-param1": {
                                "type": "string"
                            },
                            "a-param2": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        },
        "template-params-b": {
            "if": {
                "properties": {
                    "template": {
                        "const": "template-b.yml"
                    }
                },
                "required": [
                    "template"
                ]
            },
            "then": {
                "properties": {
                    "parameters": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "b-param1": {
                                "type": "string"
                            },
                            "b-param2": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        },
        "template-select": {
            "type": "object",
            "properties": {
                "template": {
                    "enum": [
                        "template-a.yml",
                        "template-b.yml"
                    ]
                }
            },
            "allOf": [
                {
                    "$ref": "#/definitions/template-params-a",
                    "$ref": "#/definitions/template-params-b"
                }
            ],
            "required": [
                "template"
            ],
            "title": "Template"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.