0

In any xml file i can say what namespace I refer to using xmlns-attributes to describe the namespace. It is well descibed here: What does "xmlns" in XML mean?

Then I can use a xml schema having a target namespace so that everyone knows that the schema describes that namespace. One question about that is found here: Why do we need targetNamespace?

Using json-schema we can define schemas for json documents. My mental model is that this is roughly equivalent to having a xsd file.

Now, how do I reference the schema in a json object? I can reference a schema using $schema attribute, but how do I declare the name of the schema i develop myself? I dont understand the equivalent of targetNamespace

LudvigH
  • 3,662
  • 5
  • 31
  • 49

1 Answers1

3

Researching for writing the question I found the answer. The closest thing of a targetNamespace is the $id attribute. The standard states...

The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against. A subschema's "$id" is resolved against the base URI of its parent schema. If no parent sets an explicit base with "$id", the base URI is that of the entire document, as determined per RFC 3986 section 5 [RFC3986].

... which is kind of the mirror image of the leading text for $schema...

The "$schema" keyword is both used as a JSON Schema version identifier and the location of a resource which is itself a JSON Schema, which describes any schema written for this particular version. The value of this keyword MUST be a URI [RFC3986] (containing a scheme) and this URI MUST be normalized. The current schema MUST be valid against the meta-schema identified by this URI.

so it is essentially the same thing. Some things to note, however:

a) you use $schema in a schema to define what schema should be used for defining your own custom schema. It is not stated in the spec that $schema in any kind of object should indicate validation for a schema.

b) You may define in your schema that $schema should be an indication about what schema to use for validation.

c) there are other ways to indicate the schema for data. One such example is using content-type in http headers. Another is to use link http headers.

d) vscode and visual studio both interpret $schema as a reference to a schema for use in validation

The issue has been discussed at the github repo for the spec.

https://github.com/json-schema/json-schema/issues/235

https://github.com/json-schema/json-schema/issues/220#issuecomment-209452992

LudvigH
  • 3,662
  • 5
  • 31
  • 49
  • $id is not equivalent to XML targetNamespace. Actually there is nothing in JSON that is equivalent to targetNamespace. $id is an id, that's all it is; JSON Schema has no concept of namespaces (yet). refer: https://github.com/json-schema-org/json-schema-spec/issues/828 thread. Namespaces can somewhat be realized through means outside of JSON Schema, its not very intuitive. – Ironluca Feb 24 '22 at 07:04
  • I rephreased my answer. – LudvigH Feb 24 '22 at 09:06
  • refer: http://lists.xml.org/archives/xml-dev/201506/msg00024.html $id is the identifier of one schema, it really does not identify a set of declarations like XML. The namespace aspects could be dealt as highlighted in the above link, that's one way. – Ironluca Feb 24 '22 at 09:15
  • I guess it all comes down to how much one wants to elaborate on the "essentially the same thing". sure, they are not the same thing, but for all intents and purposes I had when asking the question, they are the same. xmlns and $id introduce names that the validator needs to know to do its job. please provide a more complete answer as you see fit, and I might accept that one instead of my simplistic answer. these are nuanced and complicated matters, and good succint yet correct and complete explantions are hard to come by. – LudvigH Feb 24 '22 at 10:21