5

My question is how can I know which JSON schema to use to validate a particular JSON document? I specified the URL to the schema in the id field from it. Is this enough? Should I put the id in the JSON? I am not sure I understand how to connect a JSON to a specific JSON Schema.

Here is my schema

{
 "$schema": "http://json-schema.org/draft-04/schema#",
 "id": "url/schema.json",
 "title": "title",
 "definitions": {
    "emailObject": {
        "type": "object",
        "properties":{
            "name": {
                "description": "The name of the customer",
                "type": "string",
                "maxLength": 200
            },
            "email": {
                "description": "The email of the customer",
                "type": "string",
                "format": "email",
                "maxLength": 100
            }
        }
    }
 }
Robert P. Goldman
  • 860
  • 1
  • 8
  • 16
Maria
  • 313
  • 1
  • 4
  • 16

2 Answers2

3

To add to and clarify Tom's answer, here is an example of how you can link a JSON document to a JSON Schema. There is no standard way of doing this outside the context of an HTTP response. If that is something you need, you will have to come up with your own strategy.

GET /data/my-data HTTP/1.1

HTTP/1.1 200 OK
Content-Type: application/json
Link: </schema/my-schema> rel=describedby

{ "name": "Fake Fakerson", "email": "fake@fakerson.com" }

GET /schema/my-schema HTTP/1.1

HTTP/1.1 200 OK
Content-Type: application/schema+json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "url/schema.json",
  "title": "title",
  "definitions": {
    "emailObject": {
      "type": "object",
      "properties":{
        "name": {
          "description": "The name of the customer",
          "type": "string",
          "maxLength": 200
        },
        "email": {
          "description": "The email of the customer",
          "type": "string",
          "format": "email",
          "maxLength": 100
        }
      }
    }
  }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
1

According to 10.1 of the specification:

It is RECOMMENDED that instances described by a schema/profile 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]. (emphasis mine)

This would appear to describe exactly the behaviour you require, however, a casual perusal of the Linked Data Protocol section 8.1 leaves us none the wiser:

The relationship A describedby B asserts that resource B provides a description of resource A. There are no constraints on the format or representation of either A or B, neither are there any further constraints on either resource (emphasis mine)

After a quick google search, I found this question, which at first glance would appear to be duplicated by your question. However, upon deeper inspection, the question is actually about inheritance within schemas, not the referencing of a schema from it's supported instances.

One of the answers, rather intriguingly, provides a solution which draws on the JSON-Hyper-schema standard - an attempt to extend the JSON-schema standard to support the definition of application-level semantics.

The way it achieves this is by use of the links collection:

{
   ...
   "links":[
      {
         "rel":"describedby",
         "href":"{+fileType}"
      }
   ]
}

It turns out that this is based on another standard RFC5988 - Web Linking which happens to be the same standard which allows us to load CSS into HTML pages.

As @Jason points out in his comment -

Your first quote, the one from the spec, is the right way to do it. The linked data definition of describedby does not contradict the JSON Schema spec. It's a purposefully broad definition so it can be applied to any media type that describes data. That includes JSON Schema, XML Schema, or anything else.

So, it would appear that including a links collection in your schema instance would be the correct way to reference the schema. So in your specific case, you could do this:

{
   ...
   "links":[
      {
         "rel":"describedby",
         "href":"url/schema.json" // I assume!!
      }
   ]
}

Even though this may be correct, I don't know how many JSON parsers will respect this when resolving to an actual schema via the link.

Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • Your first quote, the one from the spec, is the right way to do it. The linked data definition of `describedby` does not contradict the JSON Schema spec. It's a purposefully broad definition so it can be applied to any media type that describes data. That includes JSON Schema, XML Schema, or anything else. – Jason Desrosiers Jul 20 '17 at 02:40
  • Thank you for the clarification, but my problem is that I have 2 Json schema on a server and I have a Json objects and I need to validate it with one schema. But I don't know if I should put the id in the Json object to link them or how to get only the schema for that Object. – Maria Jul 20 '17 at 10:52
  • @Maria - as suggested, you can use the links collection from the JSON Hyper schema standard. However, this may or may not be respected by your parser. – tom redfern Jul 20 '17 at 12:29
  • Actually, @tomredfern, using a Hyper-Schema link is not what I meant. You would have to use a Link header when serving the actual JSON document. Putting the link in the schema doesn't help you find the schema. Maria, this is the only way standard way to describe linkage between a JSON document and it's schema. If that doesn't work for you, you will have to come up with some custom way to do it yourself. – Jason Desrosiers Jul 20 '17 at 16:12
  • Note that the link header does nothing to help those of us who need to validate a JSON document that is *not* served by a web server. JSON is used enough now for static documents that a better solution to this problem is definitely needed! – Robert P. Goldman Mar 01 '21 at 15:01
  • @RobertP.Goldman - there is a convention which you may wish to consider - see my answer here: https://stackoverflow.com/questions/58227262/how-should-you-reference-the-json-schema-that-a-json-object-conforms-to/58233813#58233813 – tom redfern Mar 01 '21 at 15:41