3

When I have defined XML Schema I can then reference it from XML this way saying that that XML must correspond to the referenced schema. This way I can force validation of such XML and I can also provide valuable hint for the person who is going to edit this file, because XML editors supporting XML Schema will use such reference to generate auto-complete, this way making editing much easier.

However I can't see such referencing in JSON Schema documentation. For example: https://json-schema.org/learn/getting-started-step-by-step.html

It looks like it is not part of the standard, or I just can't find it.

Here is an example for XSD Schema with reference usage:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/definitions/product">
    <xsd:element name="product">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="id" type="xsd:long" maxOccurs="1" minOccurs="1"/>
                <xsd:element name="name" type="xsd:string" maxOccurs="1" minOccurs="1"/>
                <xsd:element name="description" type="xsd:string" minOccurs="0" maxOccurs="1" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

And here is an XML that uses that Schema by referencing it logical name: http://example.org/definitions/product

<product:product xmlns:product="http://example.org/definitions/product">
    <id>1</id>
    <name>One</name>
    <description>The One</description>
</product:product>

So now anyone can start editing it and, if supported, get auto-complete by their editor based on referenced XSD Schema.

But what about JSON Schema?

If I have JSON schema like this:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.org/definitions/product",
  "title": "product",
  "type": "object",
  "properties": {
    "id": {
      "type": "long"
    },
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    }
  },
  "required": [ "id", "name" ]
}

And actual JSON like this:

{
  "id": 1,
  "name": "One",
  "description": "The one"
}

Then how can I actually link the JSON to Schema I expect it will correspond to?

2
  • http://example.org/definitions/product is not the logical name of the schema. It is the namespace URI that you use in your document, and you also have one schema that happens to have definitions for some elements in that namespace. The namespace can have more than is in the schema, and there can be more than one schema for the namespace. There is no 1:1 mapping. Perhaps you mean xsi:schemaLocation (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="...") Commented Feb 28, 2019 at 11:20
  • Thank for correction! My point was to stress that it is not actual location of schema or anything else but more like unique identifier, and it was not really an essence of the question. Commented Feb 28, 2019 at 12:28

1 Answer 1

4

You are correct, it is not part of the standard.

For JSON returned as an HTTP response, you can note using a header that the response JSON is described by a specific JSON Schema.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-01#section-10.1

It is RECOMMENDED that instances described by a schema provide a link to a downloadable JSON Schema using the link relation "describedby",
as defined by Linked Data Protocol 1.0, section 8.1
[W3C.REC-ldp-20150226].

In HTTP, such links can be attached to any response using the Link header [RFC8288]. An example of such a header would be:

Link: http://example.com/my-hyper-schema#; rel="describedby"

Sign up to request clarification or add additional context in comments.

3 Comments

It should be noted that vocabularies (a feature of the upcoming draft-2019-03, successor to draft-07) will do something like this by allowing authors to add new keywords. I imagine any editors that support the new draft would autocomplete with the vocabulary-defined keywords.
I don't like what the JSON schema draft suggests: by putting the schema link outside of the document it ignores cases when the document is available alone. In such cases information about schema used is not available at hand. I guess it's because including this as part of JSON document would require changing JSON specification? Anyway, my complain is about the draft, not your answer.
I understand. (I am one of the people that work on the drafts too!) We do not control, nor can we, the JSON specification. There have been a number of discussions about suggestions or recommendations on how to allow an instance / JSON data to identify as conforming to a schema, but it's not as clear cut as you might expect. There are complex issues, and StackOverflow is not the place to list or discuss them =] There's a GH issue somewhere. Feel free to join our Slack =]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.