synth icon indicating copy to clipboard operation
synth copied to clipboard

Flattening objects in the schema

Open mikeando opened this issue 4 years ago • 3 comments

Required Functionality Creating a schema where result has many fields A1..AN but has two other fields, say B and C and only one can be present requires us to create a schema with a one_of at the root.

{
   "type":"one_of",
   "variants": [
    {
       "type":"object",
       "A1":  {...},
       ...
       "AN": {...},
        "B": {...}
    },
    {
       "type":"object",
       "A1":  {...},
       ...
       "AN": {...},
       "C": {...}
    },
  ]
}

This means a lot of repetition. All the A1..AN fields have to be spelt out again.

Proposed Solution

If we could add an attribute to an object which puts all its entries into the parent (like serde's struct flattening at https://serde.rs/attr-flatten.html) we would be able to write this as something like

{
   "type":"object",
   "A1":  {...},
   ...
   "AN": {...},
    "B_OR_C": {
        "type":"variant",
        "attributes":["flatten"],
        "variants":[
            { "type":"object", "B":{...} }
            { "type":"object", "C":{...} }
        ]
   }
}

with that "B_OR_C" never appearing in the output.

mikeando avatar Nov 11 '21 13:11 mikeando

The example schema you shown should be something like this, right?

{
   "type":"one_of",
   "variants": [
    {
       "type":"object",
       "A1":  {...},
       ...
       "AN": {...},
        "B": {...}
    },
    {
       "type":"object",
       "A1":  {...},
       ...
       "AN": {...},
       "B": {...} // <==== C ?
    },
  ]
}

hbina avatar Nov 12 '21 01:11 hbina

The example schema you shown should be something like this, right?

Absolutely.

mikeando avatar Nov 12 '21 02:11 mikeando

The example schema you shown should be something like this, right?

I've fixed it in the description now. There were a few issues in the proposed syntax too, which I've just corrected too. Again copy-paste errors due to me submitting this in a rush - sorry.

mikeando avatar Nov 12 '21 02:11 mikeando