下面是我的实际模式的简化/净化后的视图.是否可以使用fieldA的值实现对fieldD的约束(比方说对数组大小的约束)?


{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "fieldA": {
            "type": "boolean"
        },
        "fieldB": {
            "type": "object",
            "$ref": "#/$defs/fieldB"
        }
    },
    "$defs": {
        "fieldB": {
            "type": "object",
            "properties": {
                "fieldC": {
                    "type": "string"
                },
                "fieldD": {
                    "$ref": "#/$defs/fieldD"
                }
            }
        },
        "fieldD": {
            "type": "array"
        }
    }
}

推荐答案

您可以使用if, then语法判断fieldA上的条件并对fieldD应用约束

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "fieldA": {
            "type": "boolean"
        },
        "fieldB": {
            "type": "object",
            "$ref": "#/$defs/fieldB"
        }
    },
    "$defs": {
        "fieldB": {
            "type": "object",
            "properties": {
                "fieldC": {
                    "type": "string"
                },
                "fieldD": {
                    "$ref": "#/$defs/fieldD"
                }
            }
        },
        "fieldD": {
            "type": "array"
        }
    },
    "if": {
        "required": ["fieldA"],
        "properties": {
            "fieldA": {
                "const": true
            }
        }
    },
    "then": {
        "properties": {
            "fieldB": {
                "properties": {
                    "fieldD": {
                        "minItems": 1
                    }
                }
            }
        }
    },
    "else": {
        "properties": {
            "fieldB": {
                "properties": {
                    "fieldD": {
                       <some other constraint or omit `else` completely for a default behavior>                    }
                }
            }
        }
    }
}

Json相关问答推荐

无法根据vega规范中的条件设置文本 colored颜色

手动解开没有可编码的SON- Swift

JSON:将项';S键/名称移动到属性中,并使用JQ将其转换为数组

如何在PowerShell中访问嵌套的JSON字段

需要有关在Ffltter应用程序中解码JSON的帮助;未处理的异常:类型不是类型转换中类型的子类型

在Zig中解析JSON失败

使用Kotlin限制序列化类属性的允许整数值

JSON 的自定义编组器,可以是字符串或 map[string]string / map[string]bool

在 JOLT 中重新排列值

判断golang中解析的json响应中是否存在所需的json键(不是值)

Powershell中等效的JSONPath通配符以 Select 对象中的所有数组

在 postgresql 中将行转换为 json 对象

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

C#扁平化json struct

json_encode() 返回 false

使用 Retrofit 解析动态密钥 Json 字符串

将 json 转换为 C# 数组?

如何对 jq 中的 map 数组中的值求和?

FastAPI:如何将正文读取为任何有效的 json?

是否有一个 PHP 函数只在双引号而不是单引号中添加斜杠