Newtonsoft.Json.Schema icon indicating copy to clipboard operation
Newtonsoft.Json.Schema copied to clipboard

Add: onlyOneOf behavior, one property allowed.

Open oreleraki opened this issue 4 years ago • 1 comments

I'm trying to create a json scema validation, which only 1 property is allowed. either Tenants or ExcludedTenants but not both. And they both not exists then it's also ok.

Is there a way to accomplish that.

{
  "type": "object",
  "properties": {
    "Id": {
      "type": "integer"
    },
    "Name": {
      "type": "string"
    },
    "Highlights": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "Highlight": {
            "type": "string"
          },
          "Lobs": {
            "type": "string"
          },
          "MimeType": {
            "type": "string"
          },
          "ActionType": {
            "type": "string"
          },
          "Order": {
            "type": "integer"
          },
          "Tenants": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "ExcludedTenants": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        },
        "required": ["Highlight"]
      }
    }
  },
  "required": ["Id", "Name", "Highlights"]
}

oreleraki avatar Feb 22 '21 09:02 oreleraki

I need this as well. I've got a simple generic multiplexer/demultiplexer that turns a list of type List<BaseClass> (with possible subclasses Subclass1, Subclass2, Subclass3) into a list of demuxed objects of the form

public class BaseClassDemuxed
{
   public Subclass1 Subclass1 {get;set;}
   public Subclass2 Subclass2 {get;set;}
   public Subclass3 Subclass3 {get;set;}
}

this creates clean JSON for serialization/deserialization of polymorphic lists, and it creates viable schema. The only hole is that obviously the members of the demuxed form should be mutually-exclusive.

So yeah, I need a way to similarly create a "OneOf" thing... although in my case I'd want exactly one of the properties populated, not zero-or-one, as oreleraki described.

Pxtl avatar Apr 22 '21 21:04 Pxtl