What does "$ref": "#" mean?

Viewed 181

Here is sample JSON schema code: What is "$ref": "#" referring to? Is it here? or do they mean the whole document? You can find this entire schema on https://www.jsonschemavalidator.net Select Schema Draft V7.

{
"$schema": "http://json-schema.org/draft-07/schema#",
 "definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
    },
    "nonNegativeInteger": {
        "type": "integer",
        "minimum": 0
    },
    "nonNegativeIntegerDefault0": {
        "allOf": [
            { "$ref": "#/definitions/nonNegativeInteger" },
            { "default": 0 }
        ]
    },....
1 Answers

I'm not expert, but I had same question and I think I found answer

... each $ref is resolved into a full URI. Once that is done, all your questions are answered by asking the question: What schema would I end up with, if I simply fetched that URI? Where the $ref is, how it was loaded, all of that is irrelevant - it's entirely dependent on the resolved URI.

The library might take some shortcuts (like caching documents so they are only fetched once, or trusting one schema to "speak for" another), but those are all implementation details.

# is not special: all values of $ref are resolved as URIs relative to the current document (or the closest value of "id", if there is one).

Therefore, if you haven't used "id", then # will point to the root of the schema document. If you fetched your schema from http://example.com/schema, then a {"$ref": "#"} anywhere inside that will resolve to http://example.com/schema#, which is the document itself.

Link: https://stackoverflow.com/a/17723151/989369

So basically it points to main scheme, i.e.

{
"$schema": "http://json-schema.org/draft-07/schema#",
 "definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
    }

will convert to:

{
"$schema": "http://json-schema.org/draft-07/schema#",
 "definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": {
          "$schema": "http://json-schema.org/draft-07/schema#",
          "definitions": {
          ...
        }
    }
Related