Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify that a JSON instance is defined by a specific JSON Schema

Tags:

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
            }
        }
    }
 }
like image 501
Maria Avatar asked Jul 19 '17 12:07

Maria


1 Answers

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": "[email protected]" }

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
        }
      }
    }
  }
}
like image 70
Jason Desrosiers Avatar answered Oct 04 '22 16:10

Jason Desrosiers