Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "anyof" and "oneof" in z schema?

Its looking like both works fine with my input validation code. Then what is the exact difference?

Schema with oneof

[{
  "id": "MyAction",
  "oneOf": [{ "$ref": "A1" },
            { "$ref": "A2" }]
 },
 {
  "id": "A1",
  "properties": {
      "class1": { "type": "string"},
      "class2": { "type": "string"}
   }
 },
 {
  "id": "A2",
  "properties": {
      "class2": { "type": "string"},
      "class3": { "type": "string"}
   }
 }
]

Schema with anyof

    [{
  "id": "MyAction",
  "anyOf": [{ "$ref": "A1" },
            { "$ref": "A2" }]
 },
 {
  "id": "A1",
  "properties": {
      "class1": { "type": "string"},
      "class2": { "type": "string"}
   }
 },
 {
  "id": "A2",
  "properties": {
      "class2": { "type": "string"},
      "class3": { "type": "string"}
   }
 }
]
like image 964
Somnath Avatar asked Dec 02 '15 08:12

Somnath


People also ask

What is the difference between oneOf and anyOf?

Difference Between anyOf and oneOfoneOf matches exactly one subschema, and anyOf can match one or more subschemas.

What does anyOf mean?

anyOf means the item must validate against at least one (but possibly more than one) of the schemas. oneOf means it must validate against only one of the schemas.

What is anyOf in JSON schema?

allOf: (AND) Must be valid against all of the subschemas. anyOf: (OR) Must be valid against any of the subschemas. oneOf: (XOR) Must be valid against exactly one of the subschemas.

What is oneOf in JSON schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.


2 Answers

If you look at the JSON Schema documentation, it says:

anyOf:

...

An instance validates successfully against this keyword if it validates successfully against at least one schema defined by this keyword's value. Note that when annotations are being collected, all subschemas MUST be examined so that annotations are collected from each subschema that validates successfully.

oneOf:

...

An instance validates successfully against this keyword if it validates successfully against exactly one schema defined by this keyword's value.

Note my emphasis in the above. anyOf means the item must validate against at least one (but possibly more than one) of the schemas. oneOf means it must validate against only one of the schemas.

like image 168
T.J. Crowder Avatar answered Sep 23 '22 16:09

T.J. Crowder


I am late in the quest but as per my understanding the usage of this keyword depends on the type of the object/parent itself. for example if you are trying to define the type for a single property of the object or the element of an array. take the below example :

{
    "title": "Sample JSON Schema",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "definitions": {
        "propObjectType1" : {
            "name": "string",
            "age": "number"
        },
        "propObjectType2" : {
            "name": "string",
            "dob": {
                "type": "string",
                "pattern": "\\d\\d\/\\d\\d\/\\d\\d\\d\\d"
            }
        }
    },
    "properties": {
        "prop1": {
            "type": "string",
            "maxLength": 64
        },
        "prop2": {
            "anyOf": [
                {
                    "$ref": "#/definitions/propObjectType1"
                },
                {
                    "$ref": "#/definitions/propObjectType2"
                }
            ] 
        },
        "prop3": {
            "oneOf": [
                {
                    "$ref": "#/definitions/propObjectType1"
                },
                {
                    "$ref": "#/definitions/propObjectType2"
                }
            ] 
        },
        "prop4Array": {
            "type": "array",
            "items": {
                "oneOf": [
                    {
                        "$ref": "#/definitions/propObjectType1"
                    },
                    {
                        "$ref": "#/definitions/propObjectType2"
                    }
                ] 
            }       
        },
        "prop5Array": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "$ref": "#/definitions/propObjectType1"
                    },
                    {
                        "$ref": "#/definitions/propObjectType2"
                    }
                ] 
            }       
        }
    }
}

So in the above definition the prop2 and prop3 are the same (you can use interchangeably anyOf or oneOf) and you can define what ever you are comfortable with. but, in case of array:

  1. when you use anyOf for the items type, the elements can be of any type out of those and the array can contain the mixed items. Means you can have one item of type 1 and another item of type 2.
  2. when you use oneOf for the items type, the elements can be of any type out of those and the array can contain only one type of items. Means all the items must be of the same type (either type 1 or type 2).
like image 34
Anant Anand Gupta Avatar answered Sep 23 '22 16:09

Anant Anand Gupta