Vijay
Vijay

Reputation: 597

Json schema - oneOf fields needs to be required

I need oneOf value and value_num to be present as a required field but not both. None of these options are working for me.I have tried adding "additionalProperties": false.Tried removing value and value_num field declarations.

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
    "observation": {
        "type": "array",
        "minItems": 1,
        "items": {
            "type": "object",
            "properties": {
                "timestamp": {
                    "type": "string",
                    "format": "date-time"
                },
                "eventType": {
                    "type": "string"
                },
                "value": {
                    "type": "object"
                },
                "value_num": {
                    "type": "number"
                }

            },
            "oneOf": [{
                "properties": {
                    "value": {
                        "type": "object"
                    },
                    "required": ["value"]
                }
            }, {
                "properties": {
                    "value_num": {
                        "type": "number"
                    },
                    "required": ["value_num"]
                }
            }],
            "required": [
                "timestamp",
                "eventType"
            ]
        }
    }
},
"required": [
    "observation"
]

}

I have tried adding the oneOf within the items.properties block. All I really want is this: "oneOf": [{"value", "value_num"}],

Any help is appreciated.

TIA,

Upvotes: 2

Views: 1093

Answers (2)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

Your oneOf should look like this

"oneOf": [
  { "required": ["value"] },
  { "required": ["value_num"] }
]

This says that either "value" or "value_num" must be required, but not both.

Upvotes: 4

Vijay
Vijay

Reputation: 597

Almost fixed the problem like this:

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
    "observation": {
        "type": "array",
        "minItems": 1,
        "items": {
            "type": "object",
            "properties": {
                "timestamp": {
                    "type": "string",
                    "format": "date-time"
                },
                "eventType": {
                    "type": "string"
                },
                "value": {
                    "type": "object"
                },
                "value_num": {
                    "type": "number"
                }

            },
            "oneOf": [{"value": {
                    "type": "object"
                },
                "value_num": {
                    "type": "number"
                }}],
            "required": [
                "timestamp",
                "eventType"
            ]
        }
    }
},
"required": [
    "observation"
]

}

What it doesn't invalidate is payload like this where both value and value_num are present and only 1 should be present:

{
"info": {
    "deviceId": "482038028341324",
    "companionId": "asdfas76df76sdf",
    "userId": "1234"
},
"observation": [{
        "timestamp": "2013-12-31T23:59:59-01:00",
        "eventType": "BOOT_EVENT",
        "value_num": 6,
        "value": {"boot":true}
    }
]

}

Upvotes: 0

Related Questions