如何在json模式中表示求和/联合类型 [英] How to represent sum/union types in in json schema

查看:244
本文介绍了如何在json模式中表示求和/联合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用json-schema记录JSON的现有用法.系统允许对象属性之一具有以下两种可能性.

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.

任何一个

{
    "tracking_number" : 123
}

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

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

How can I express this using json schema?

推荐答案

使用anyOf断言该属性必须符合一个或另一个架构.

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" }
    }
}

这篇关于如何在json模式中表示求和/联合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆