Is XML Schema xs: an alternative equivalent available in JSON Schema?

Can alternatives be used in JSON schema? In XSD, this is doable with an element xs:alternative

.

For example see How to use alternatives in XML Schema 1.1


UPDATE 1:

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

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

      

Requirements:

  • actions

    can have any number of elements
  • basic

    actions must contain a property param1

  • extended

    actions must contain param1

    and param2

    properties
+3


source to share


1 answer


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

  • oneOf: Force the given item to execute to satisfy only one of the schema list.
  • anyOf: must satisfy at least one of the list of schemas.
  • allOf: must match all provided schemas in the list.
  • not: must not match the given schema.


Assuming you're 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"]
        }
    }
}

      

+3


source







All Articles