Alternative required properties

72 views
Skip to first unread message

Roy Walmsley

unread,
Jun 13, 2017, 2:56:47 PM6/13/17
to JSON Schema
Hi,

I want to make one of two properties required. Here is an example instance:

"MetadataBoolean": {
"type": "object",
"properties": {
"@DEF": {
"type": "string"
},
"@USE": {
"type": "string"
},
"IS": {
"$ref": "#/definitions/IS"
},
"@name": {
"type": "string"
},
"@reference": {
"type": "string"
},
"@value": {
"type": "array",
"minItems": 1,
"items": {
"type": "boolean"
}
},
"-metadata": {
"$ref": "#/definitions/-metadata"
},
"-children": {
"$ref": "#/definitions/-commentRoute"
}
},
"required": [
"@name"
],
"additionalProperties": false
},

At the moment, only the "@name" property is required. I want to modify the schema so that either the "@name" or the "@USE" properties are required. I suspect that "oneOf" is the solution, but I don't know how to apply it, other than putting "oneOf" at the top and having the two alternatives for the full schema subset. Any help to arrive at a shorter solution would be appreciated.

Thanks in advance,

Roy

Henry H. Andrews

unread,
Jun 14, 2017, 2:04:39 AM6/14/17
to JSON Schema
You only need to put the different "required" fields in the "oneOf", assuming that you mean that you want EXACTLY one of those two present, but not both.  I think you probably just want AT LEAST one of them present, which would be "anyOf".  Here is an example:

        "anyOf": [
          {
            "required": ["@name"]
          },
          {
            "required": ["@USE"]
          }
        ],
        "additionalProperties": false
    }



Notice that it's very important to keep the full "properties" at the same level as "additionalProperties, as they interact.  However, "required" (like most keywords) is applied independently so you can move it around with the "*Of" keywords all you'd like.

If you really did mean "oneOf", it's exactly the same, just with "oneOf" replacing "anyOf".

thanks,
-henry

Roy Walmsley

unread,
Jun 14, 2017, 2:15:10 PM6/14/17
to json-...@googlegroups.com
Thanks Henry. Yes, I did mean EXACTLY one of those two. Your general comments are much appreciated.

Roy

--
You received this message because you are subscribed to a topic in the Google Groups "JSON Schema" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-schema/Y6brKTJ7rAs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-schema+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages