ajv-validator / ajv-merge-patch

$merge and $patch keywords for Ajv JSON-Schema validator to extend schemas
https://ajv.js.org
MIT License
46 stars 17 forks source link

Is there any way to get the schema that results from applying $merge or $patch definitions? #13

Closed azaslavsky closed 7 years ago

azaslavsky commented 7 years ago

For example, if I have the following two schemas:

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

{
  "$id": "schemaExtended",
  "$merge": {
    "source": { "$ref": "schemaExtended.json#" },
    "with": {
      "properties": {
        "foo": {
          "minimum": 0
        },
        "bar": {
          "type": "string"
        }
      }
    }
  }
}

Is there anyway to output the result of the merge operation? Something like ajv.getMergedSchema(schemaExtended);, which would produce:

{
  "$id": "schemaExtended",
  "type": "object",
  "properties": {
    "foo": {
      "type": "number",
      "minimum": 0
    }
    "bar": {
      "type": "string"
    }
  }
}

The reason I ask is that most other json-schema implementations do not support merge-patch, so it would be nice if there was some method by which JSON schemas that use these keywords could be converted to regular JSON schemas.

epoberezkin commented 7 years ago

This package implements $merge and $patch as custom keywords, it only uses resolved schemas in place, without generating full resolved schema.

It's relatively simple to generate the schema you want, using the same packages that are used here and json-schema-traverse.

It's worth publishing as a separate package I think - it could be used in ajv-cli package to add an additional command.