Json Schema example for oneOf objects

前端 未结 2 1189
清歌不尽
清歌不尽 2020-12-10 00:49

I am trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (ty

相关标签:
2条回答
  • 2020-12-10 00:57

    Try this:

    {
        "description" : "schema validating people and vehicles",
        "type" : "object",
        "oneOf" : [{
            "properties" : {
                "firstName" : {
                    "type" : "string"
                },
                "lastName" : {
                    "type" : "string"
                },
                "sport" : {
                    "type" : "string"
                }
            },
            "required" : ["firstName"]
        }, {
            "properties" : {
                "vehicle" : {
                    "type" : "string"
                },
                "price" : {
                    "type" : "integer"
                }
            },
            "additionalProperties":false
        }
    ]
    }
    
    0 讨论(0)
  • 2020-12-10 01:10

    oneOf need to be used inside a schema to work.

    Inside properties, it's like another property called "oneOf" without the effect you want.

    0 讨论(0)
提交回复
热议问题