XML Schema xs:alternative是否在JSON Schema中可用? [英] Is XML Schema xs:alternative equivalent available in JSON Schema?

查看:123
本文介绍了XML Schema xs:alternative是否在JSON Schema中可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JSON模式中使用替代项?在XSD中,可以使用xs:alternative元素来实现.

Is it possible to use alternatives in JSON Schema? In XSD this is doable using xs:alternative element.

例如,请参见:如何在XML模式中使用替代项1.1

更新1:

这是一个示例JSON,我想使用JSON模式进行描述:

This is a sample JSON I would like to describe using JSON schema:

{
  "actions": [
    {
      "type": "basic",
      "param1": "value"
    },
    {
      "type": "extended",
      "param1": "value",
      "param2": "blah"
    }
  ]
}

要求:

  • actions可以有任意数量的项目
  • basic操作必须包含param1属性
  • extended操作必须包含param1param2属性
  • actions may have any number of items
  • basic actions must contain param1 property
  • extended actions must contain param1 and param2 properties

推荐答案

Draft04以来存在类似的机制,具有更好的语义:oneOf,anyOf,allOf和not,关键字.

There is a similar mechanism since Draft04 with nicer semantics: oneOf, anyOf, allOf and not, keywords.

  • oneOf:强制给定元素仅满足模式列表中的一个.
  • anyOf:必须满足至少一个模式列表.
  • allOf:必须满足列表中所有提供的模式.
  • 不:不能满足给定的模式.

假设您正在寻找排他的替代",这将是使用oneOf的json模式的示例:

Assuming you are looking for an exclusive "alternative", this would be an example of json-schema using oneOf:

{
    "actions" : {
        "type" : "array",
        "items" : {
            "oneOf" : [{
                    " $ref " : "#/definitions/type1 "
                }, {
                    " $ref " : "#/definitions/type2 "
                }
            ]

        }

    },
    " definitions " : {
        " type1 " : {
            " type " : " object ",
                        "properties": {
                                  "param1":{"type":"string"}
                        },
                        "required":["param1"]
        },
        " type2 " : {
            " type " : " object ",
                         "properties": {
                                  "param2":{"type":"string"},
                                  "param3":{"type":"string"}
                        },
                         "required":["param2","param3"]
        }
    }
}

这篇关于XML Schema xs:alternative是否在JSON Schema中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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