如何在json模式中表示sum/union类型?

时间:2020-12-03 18:50:34

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.

我正在尝试使用JSON模式记录JSON的现有使用。系统允许对其中一个对象属性具有以下两种占有权。

Either

要么

{
    "tracking_number" : 123
}

Or

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

How can I express this using json schema?

如何使用json模式表示这一点?

1 个解决方案

#1


7  

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

使用anyOf断言属性必须符合一个或另一个模式。

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

#1


7  

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

使用anyOf断言属性必须符合一个或另一个模式。

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