7

I have created a JSON schema following the draft v3 specifications. Schema looks like this:

{
"$schema": "http://json-schema.org/draft-03/schema#",
"additionalProperties": false,
"type": "object",
"properties": {
    "ExecutionPlanList": {
        "type": "array",
        "items": [{
            "type": "object",
            "properties": {
                "model": {
                    "required": true,
                    "properties": {
                        "featureList": {
                            "required": true,
                            "items": {
                                "properties": {
                                    "featureName": {
                                        "type": ["string", "null"]
                                    },
                                    "featureType": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "type": "array"
                        },
                        "modelId": {
                            "required": true,
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "cascadeSteps": {
                    "required": false,
                    "items": {
                        "properties": {
                            "binaryModel": {
                                "$ref": "#/properties/ExecutionPlanList/items/properties/model",
                                "required": true
                            },
                            "threshold": {
                                "required": true,
                                "default": "0.0",
                                "maximum": 100.0,
                                "type": "number"
                            },
                            "backupModel": {
                                "$ref": "#/properties/ExecutionPlanList/items/properties/model",
                                "required": true
                            }
                        }
                    },
                    "type": "array"
                },
                "marketplaceId": {
                    "required": true,
                    "type": "integer"
                }
            }
        }]
    }
},
"required": true
}

Essentially, executionPlanList contains list of model and cascadeStep, and each cascadeStep contains two models with a number. So I'm trying to re-use the schema for model in cascadeStep, but validation (https://www.jsonschemavalidator.net/) is failing with Could not resolve schema reference '#/properties/ExecutionPlanList/items/properties/model'.

Would appreciate any pointers on what's wrong with this schema.

YetAnother
  • 1,045
  • 1
  • 9
  • 27

2 Answers2

5

It should be '#/properties/ExecutionPlanList/items/0/properties/model'

The schema validates only the first item, by the way.

esp
  • 7,314
  • 6
  • 49
  • 79
  • Thanks. Can you elaborate on the second part "validates only the first item" – YetAnother Nov 11 '17 at 14:03
  • As “items” value is an array, the schema there applies only to the first item in the array. That’s ok if it’s intended, but it’s also a common mistake... – esp Nov 11 '17 at 21:19
  • Ohh..that's definitely not my intention. Since v3 doesn't support dictionary, what's the recommended way to reuse schemas here ? – YetAnother Nov 13 '17 at 06:35
5

what about adding a 'definitions' and refer like this:

{
"$schema": "http://json-schema.org/draft-03/schema#",
"additionalProperties": false,
"type": "object",
"definitions": {
      "model": {
          "required": true,
          "properties": {
              "featureList": {
                  "required": true,
                  "items": {
                      "properties": {
                          "featureName": {
                              "type": ["string", "null"]
                          },
                          "featureType": {
                              "type": "string"
                          }
                      },
                      "type": "object"
                  },
                  "type": "array"
              },
              "modelId": {
                  "required": true,
                  "type": "string"
              }
          },
          "type": "object"
      }
},
"properties": {
    "ExecutionPlanList": {
        "type": "array",
        "items": [{
            "type": "object",
            "properties": {
                "model": {
                  "$ref" : "#/definitions/model"
                },
                "cascadeSteps": {
                    "required": false,
                    "items": {
                        "properties": {
                            "binaryModel": {
                                "$ref" : "#/definitions/model",
                                "required": true
                            },
                            "threshold": {
                                "required": true,
                                "default": "0.0",
                                "maximum": 100.0,
                                "type": "number"
                            },
                            "backupModel": {
                                "$ref" : "#/definitions/model",
                                "required": true
                            }
                        }
                    },
                    "type": "array"
                },
                "marketplaceId": {
                    "required": true,
                    "type": "integer"
                }
            }
        }]
    }
},
"required": true
}
damorin
  • 1,389
  • 1
  • 12
  • 15
  • This is helpful, thanks, spent a long time fighting with a similar problem and this was the solutions. "definitions" is not a special or reserved word, but for some reason jsonschema does not like "$ref" within the same section, not sure of the exact rule, but it seems to prefer it if the "$ref" you point to is in another object in the structure with a different top level key. – cardamom Nov 12 '18 at 12:26