gpt4 book ai didi

Json Schema - 如何使任意两个或多个属性成为必需

转载 作者:行者123 更新时间:2023-12-05 00:50:25 24 4
gpt4 key购买 nike

我有这个父架构:

{
"definitions": {
"parcel": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"accountNumber": {
"type": "string"
},
"parcelNumber": {
"type": "string"
},
"propertyType": {
"type": "string"
},
"address": {
"$ref": "address.json#/definitions/address"
},
"coordinates": {
"$ref": "coordinates.json#/definitions/coordinates"
}
},
"required": ["accountNumber", "parcelNumber"]
}
}
}

以下是引用的子模式:
{
"definitions": {
"address": {
"type": "object",
"properties": {
"addressString": {
"type": "string",
"addressType": {
"enum": ["residential", "business"]
}
},
"required": ["addressString"]
}
}
}
}

{
"definitions": {
"coordinates": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"projection": {
"type": "string"
}
},
"required": ["latitude ", "longitude", " projection"]
}
}
}

我想将以下条件应用于父架构。
  • 提供地址或坐标或两者都提供。
  • 如果既没有提供地址也没有提供坐标,则验证失败。
  • 最佳答案

    您的 anyOf解决方案有效。您可以通过从 anyOf 中分离出固定的必需属性(accountNumber 和parcelNumber)来使其更简洁一些。属性组:

    {
    "type": "object",
    "required": [
    "accountNumber",
    "parcelNumber"
    ],
    "anyOf": [
    {"required" : ["address"]},
    {"required" : ["coordinates"]}
    ],
    "properties": {
    "id": {
    "type": "string"
    },
    "accountNumber": {
    "type": "string"
    },
    "parcelNumber": {
    "type": "string"
    },
    "propertyType": {
    "type": "string"
    },
    "address": {
    "type": "object",
    "properties": {
    "addressString": {
    "type": "string"
    },
    "addressType": {
    "enum": [
    "residential",
    "business"
    ]
    }
    },
    "required": [
    "addressString"
    ]
    },
    "coordinates": {
    "type": "object",
    "properties": {
    "latitude": {
    "type": "number"
    },
    "longitude": {
    "type": "number"
    },
    "projection": {
    "type": "string"
    }
    },
    "required": [
    "latitude",
    "longitude",
    "projection"
    ]
    }
    }
    }

    这里有一个要点供引用:

    http://jsonschemalint.com/#/version/draft-05/markup/json?gist=f36d9a7e080c4d25dbbf09b7dd03137e

    关于Json Schema - 如何使任意两个或多个属性成为必需,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41172240/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com