Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
azaslavsky opened this issue Oct 10, 2017 · 1 comment
Labels

Comments

@azaslavsky
Copy link

azaslavsky commented Oct 10, 2017

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.

@azaslavsky azaslavsky changed the title Feature Request: Support arrays of objects to be merged in the "with" property Feature Proposal: Support arrays of objects to be merged in the "with" property Oct 10, 2017
@outdooracorn
Copy link

I suggest that the "source" and "with" properties are replaced with an array. These properties are not needed to perform a compound merge and just add unnecessary indentation. For example (based on Alex's example):

{
  "$id": "schemaExtended",
  "$merge": [
    { "$ref": "baseSchema.json#" },
    {
      "properties": {
        "foo": {
          "minimum": 0
         },
        "bar": {
          "type": "string"
        }
      }
    }, 
    {
      "properties": {
        "baz": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants