0
votes

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

{
  "type": "object",

  "properties": {

        "songs": {
            "type": "array",
            "items": {
                "type": "object",      
                "properties": {

                    "composition": {
                        "type": "object",

                        "properties": {
                            "title": {
                                "type": "string"
                            },
                            "publishing": {
                                "type": "array",

                                "items": {
                                    "type": "object",
                                    "required": ["publisherId","territory"],

                                    "definitions": {
                                        "categoryList": {
                                            "type": "object",
                                            "properties": {
                                                "BR": {
                                                "type": "number"
                                                }
                                            }
                                        }
                                    },
                                    "properties": {
                                        "publisherId": {
                                            "type": "integer"
                                        },
                                        "territory": {
                                            "$ref": "#/definitions/categoryList"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "recordings": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "songVersion": {
                                    "type": "string"
                                },
                                "album": {
                                    "type": "object",
                                    "properties": {
                                        "title": {
                                        "type": "string"
                                        }                    
                                    }
                                }                  
                            }              
                        }         
                    }

                }

            }
        }
    }
}

but got error Could not resolve schema reference '#/definitions/categoryList'. Path 'properties.songs.items.properties.composition.properties.publishing.items.properties.territory', line 40, position 24. If I omit the definitionpart it works perfectly

1
It's expecting to see definitions at the root level based on the reference you have provided. Your reference URI is relative to the document root, not the current object. - Relequestual
"URI is relative to the document root, not the current object "- could you please explain? - naqwe

1 Answers

0
votes

The easiest way to explain why the reference resolution doesn't work as you expect is to talk about a modified example from the draft-07 core specification itself.

   {
       "definitions": {
           "A": { },
           "B": {
               "definitions": {
                   "X": { },
                   "Y": { }
               }
           }
       }
   }

The document root is an object which has a definitions property.

To access #/definitions/A, you can use a reference of #/definitions/A.

To access #/definitions/B/definitions/X, you can use a reference of #/definitions/B/definitions/X.

The reference in your schema needs to know the FULL path to the subschema from the document root.

I would guess you've assumed the URI is relative to the closest subschema or the subschema which it's used in, but that is not the case.

Reference: https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8.2.4

The example includes a more exhaustive set of examples. Think of URI referencing like you would an anchor tag in HTML. When a domain part of the URI isn't included, one is "imagined" in it's place in order to do URI resolution.

Let me know if you have any follow up questions.