JSON schema properties duplication

Viewed 59

I want to develop a schema where I don't need to duplicate properties. I have an array that can store different objects.

Example of array input:

[ 
  { "name": "someName" },
  { "name": "someName", "version": 1 }
  { "name": "someName", "weight": 15 }
]

As you can see, name is a required property, version and weight is optional property.

I want to develop a scheme where I don't need to duplicate version and weight.

Below schema will be valid:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "array",
    "items": {
      "type": "object",
      "anyOf": [
        { 
          "properties": {
            "name": {
              "type": "string"
            }
          },
          "required": ["name"]
        },
        { 
          "properties": {
            "name": {
              "type": "string"
            },
            "version": {
              "type": "number"
            }
          },
          "required": ["name"]
        },
        { 
          "properties": {
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "number"
            }
          },
          "required": ["name"]
        }
      ]
    },
   "additionalProperties": false
}

I don't want to repeat name property. How can I achieve that?

Important: Besides name I also have several other required properties

1 Answers
Related