How to represent sum/union types in in json schema

只谈情不闲聊 提交于 2019-12-04 16:46:51

问题


I am trying to document an existing use of JSON using json-schema. The system permits the following two possabilities for one of the object attributes.

Either

{
    "tracking_number" : 123
}

Or

{
    "tracking_number" : [ 123, 124, 125 ]
}

How can I express this using json schema?


回答1:


Use anyOf to assert that the property must conform to one or another schema.

{
    "type": "object",
    "properties": {
        "tracking_number": {
            "anyOf": [
                { "$ref": "#/definitions/tracking_number" },
                { "type": "array", "items": { "$ref": "#/definitions/tracking_number" }
            ]
    },
    "definitions": {
        "tracking_number": { "type": "integer" }
    }
}


来源:https://stackoverflow.com/questions/35450405/how-to-represent-sum-union-types-in-in-json-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!