Skip to content

Feature Proposal: Support arrays of objects to be merged in the "with" property #14

Open
@azaslavsky

Description

@azaslavsky

Since the $merge operation does not concatenate or intersect arrays, but rather overwrites them wholesale, it is basically nonsensical for the top-level node under the with property of the $merge schema to be an array. For example, this would not produce a valid schema post merge:

//Valid baseSchema
{
  "$id": "baseSchema",
  "type": "object",
  "properties": {
    "foo": {
      "type": "number"
    }
  }
}

//Invalid merged schema
{
  "$id": "schemaExtended",
  "$merge": {
    "source": { "$ref": "baseSchema.json#" },
    "with": ["foo", "bar"]
    }
  }
}

Because this is the case, would it be possible to have the with property support an array, which would then be interpreted as a series of consecutive merges?

{
  "$id": "baseSchema",
  "type": "object",
  "properties": {
    "foo": {
      "type": "number"
    }
  }
}

//Cumbersome "nested" syntax for handling multiple merges, which is basically impossible to parse
{
  "$id": "schemaExtended",
  "$merge": {
    "source": {
      "$merge": {
        "source": { "$ref": "baseSchema.json#" },
        "with": {
          "properties": {
            "foo": {
              "minimum": 0
            },
            "bar": {
              "type": "string"
            }
          }
        }
      }
    }
    "with": {
      "properties": {
        "baz": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    }
  }
}

//Much cleaner syntax which mirrors the way _.extend and Object.assign work
{
  "$id": "schemaExtended",
  "$merge": {
    "source": { "$ref": "baseSchema.json#" },
    "with": [{
      "properties": {
        "foo": {
          "minimum": 0
        },
        "bar": {
          "type": "string"
        }
      }
    }, {
      "properties": {
        "baz": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    }]
  }
}

Is there interest in implementing this? I doubt it would ever break existing functionality, since top-level arrays under with already always produce invalid schemas, and I think it would be pretty easy to implement. If this feels like something worth doing, I can submit a quick PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions