13

I'm using JSON Schema for validating data.

I think that I may have a mistake on my schema by using the reserved keywords $id. The intention of this field was to designate what the REMOTE ID of the property on another platform was. So it was the "origin ID".

Can you please advise what $id is and if I have made a critical mistake and this value needs changing. Because in the documentation I have found this definition:

If present, the value for this keyword MUST be a string, and MUST represent a valid URI-reference [RFC3986]. This value SHOULD be normalized, and SHOULD NOT be an empty fragment <#> or an empty string <>.

sevenwithawp
  • 131
  • 1
  • 1
  • 3

2 Answers2

11

$id is a reserved keyword.

It serves for:

  • Declaring an identifier for the schema or subschema
  • Declaring a base URL against which $ref URLs are resolved

You can identify a schema, or a part of your schema (a subschema) by using $id, and then you can reuse it somewhere else by using the $ref keyword. The most simple way of seeing this, is that the $ref would be replaced by the schema with the corresponding id.

Community
  • 1
  • 1
Pedro
  • 1,875
  • 12
  • 15
10

Since $id changes the base URI of your schema, any $ref values in that same schema or any of its subschemas will be resolved differently.

For instance, if your base URI was "https://example.com/thing" and you had this schema

{
    "allOf": [
        {"$ref": "foo"},
        {
            "$id": "stuff/and/nonsense",
            "allOf": {"$ref": "bar"}
        }
    ]
}

then the "$ref" to "foo" resolves to "https://example.com/foo". But the "$ref" to "bar" resolves to "https://example.com/stuff/and/bar"

So whatever you put in "$id" for another purpose, it is likely to cause problems, particularly with "$ref" resolution.

Henry Andrews
  • 663
  • 3
  • 6
  • 1
    Is this still the case. I remember reading the behavior of id was changed to avoid confusion of overriding base uri. – RBz Dec 19 '21 at 10:25