Shobhit Ranjan
Shobhit Ranjan

Reputation: 83

Merge properties of one Json schema in another Json schema

I have a schema whose properties I have to merge in another schema. Suppose following is the schema whose properties have to be included:

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable"],
"properties":
{
  "job_name":
  {
    "type":"string",
    "dname": "job name"
  },
  "executable":
  {
    "type":"string",
    "dname":"server",
    "description": "server name"
  }
}}

The schema that includes the above schema is of the form:

{"$schema": "http://json-schema.org/schema#",
"title": "application",
"type": "object",
"required": ["app_name"],
"properties":
{
  "app_name":
  {
    "type":"string",
    "dname": "app name"
  }
}}

I want to merge the properties of first schema in the second one so that the second one looks like :

{"$schema": "http://json-schema.org/schema#",
"title": "native",
"type": "object",
"required": ["executable","app_name"],
"properties":
{
  "job_name":
  {
    "type":"string",
    "dname": "job name"
  },
  "executable":
  {
    "type":"string",
    "dname":"server",
    "description": "server name"
  }
  "app_name":
  {
    "type":"string",
    "dname": "app name"
  }
}}

Also, the "required" field gets added to the second schema. Is there a way it is possible?

Upvotes: 5

Views: 7414

Answers (2)

Shobhit Ranjan
Shobhit Ranjan

Reputation: 83

I could extend one schema in another using 'extends' keyword as follows :

  • Define the first schema in the same file in the definitions (I hope it can be another json schema file as well) and extend it in another schema.

    "definitions" : {
     "argumentBaseType":{
     "required": ["executable"],
     "properties":
     {
      "job_name":
      {
       "type":"string",
       "dname": "job name"
      },
     "executable":
     {
      "type":"string",
      "dname":"server",
      "description": "server name"
     }
    }
    }
    "properties":
    {
    {
     Argument1 : {
      "extends" : {
        "$ref" : "#/definitions/argumentBaseType"
      },
      "app_name":
      {
       "type":"string",
       "dname": "app name"
      }
     }}
    

Upvotes: 1

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

You can't merge schemas, but you can use allOf to require that a document validates against both schemas. It's not the same as merge, but it will work for the case you presented.

{
  "allOf": [
    { ... schema1 ... },
    { ... schema2 ... }
  ]
}

Upvotes: 4

Related Questions