Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONSchema validation failure with $ref (Draft v3)

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.

like image 209
Stormbringer Avatar asked Nov 10 '17 09:11

Stormbringer


2 Answers

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
}
like image 136
damorin Avatar answered Oct 19 '22 03:10

damorin


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

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

like image 32
esp Avatar answered Oct 19 '22 04:10

esp