{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "#myObject",
"description": "The details of my object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
"description": "The ID of my object [Read Only Field]",
"minimum": 1
},
"foo": {
"type": "array",
"items": {
"type": "integer"
},
"description": "List of foo ids","minItems": 1},"bar": {
"type": "array","items": {
"type": "integer"
},
"description": "List of bar ids",
"minItems": 1}
}
}
Is there some magical mixture of anyOf and oneOf that would help me get there? I only want one "foo" and one "bar" property in the resource.Thanks!
{ "$schema": "http://json-schema.org/draft-04/schema#",
"description": "The details of my object",
"anyOf": [ { "type": "object", "properties": { "id": { "$ref": "#/definitions/id" }, "foo": { "$ref": "#/definitions/foo" }, "bar": { "allOf": [ { "$ref": "#/definitions/bar" }, { "minItems": 1 } ] } }, "additionalProperties": false, "required": [ "id", "bar" ] }, { "type": "object", "properties": { "id": { "$ref": "#/definitions/id" }, "foo": { "allOf": [ { "$ref": "#/definitions/foo"
}, { "minItems": 1 } ] }, "bar": {
"$ref": "#/definitions/bar" } }, "additionalProperties": false, "required": [ "id", "foo" ] } ], "definitions": {
"id": { "type": "integer", "description": "The ID of my object [Read Only Field]", "minimum": 1 }, "foo": { "type": "array", "items": { "type": "integer" }, "description": "List of foo ids" },
Hi,I've been digging through the specs for the last few days and I can't seem to find my question either in the docs or here or on StackOverflow.Any of the following json objects are valid:{ "id": 23, "foo": [1,2,3], "bar":[4,5,6] }{ "id": 42, "foo": [1, 2, 3] } or { "id": 42, "foo": [1, 2, 3], "bar": [] }{ "id": 42, "bar": [1] } or { "id": 42, "foo": [], "bar": [1] }However the following are invalid:{ "id": 11 }{ "id": 11, "foo": [] } or { "id": 12, "bar": [] }{ "id": 2, "foo": [], "bar": [] }I would like to set up a json-schema-4 document which catches the invalid cases. I know the following doesn't quite catch it.
Is there some magical mixture of anyOf and oneOf that would help me get there? I only want one "foo" and one "bar" property in the resource.
{ "type": "object", "properties": { "id": { "type": "integer" },
"foo": { "type": "array" }, "bar": { "type": "array" } }, "additionalProperties": false, "anyOf": [
{ "properties": { "foo": { "minItems": 1 } },
"required": [ "foo" ] }, { "properties": { "bar": { "minItems": 1 } }, "required": [ "bar" ] } ]}
{ "type": "object", "properties": { "id": { "type": "integer" },
"foo": { "type": "array" }, "bar": { "type": "array" } },
"additionalProperties": false, "not": { "properties": { "foo": { "maxItems": 0 }, "bar": { "maxItems": 0 } }
}, "minProperties": 2,
"required": ["id"]}