Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some questions related to custom json schema

I'm noob to json. While defining the format of my RESTful API's result (viz JSON), I felt it would be easier to document it as my own JSON schema. While writing one I had few questions:

  1. In my result JSON, how do I specify the URI to teh schema it confirms to? --edit-- is it using $schema attribute?
  2. Are there any conventions/guidelines for JSON schema versioning? Are there some attributes that I should/can define inside my schema as attributes? I see JSON schema itself has no version defined except in it's URI specified as value of key $schema.
  3. Can I break down my one BIG JSON schema into multiple smaller ones and include one in another? Like #include in C++, then refer to multiple schemas in the JSON I sent to user as result.
  4. Can I define a custom value for key "type"? E.g. I would like to reuse the definition of "date" like this:

[ignore this line, it's to get the formatting working for following json..]

{
    "date":{
        "type":"object",
        "properties":{
            "month":{
                "type":"integer",
                "minimum":1,
                "maximum":12
            },
            "year":{
                "type":"integer",
                "minimum":0
            }
        }
    },
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"date"
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"date"
            }
        }
    }
}

instead of providing properties of "date" in multiple places like this:

{
    "personInfo":{
        "type":"object",
        "properties":{
            "name":{
                "type":"string"
            },
            "dateOfBirth":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    },
    "student":{
        "type":"object",
        "properties":{
            "id":{
                "type":"personInfo"
            },
            "pass_out_year":{
                "type":"object",
                "properties":{
                    "month":{
                        "type":"integer",
                        "minimum":1,
                        "maximum":12
                    },
                    "year":{
                        "type":"integer",
                        "minimum":0
                    }
                }
            }
        }
    }
}

according to 5.1 type in the spec, it's not possible, but it seems like such a basic usecase!

like image 864
Kashyap Avatar asked Nov 20 '11 19:11

Kashyap


3 Answers

  1. As you correctly figured out, $schema can be used to specify the schema it conforms to.
  2. I actually found this topic while googling for JSON Schema versioning, and the idea of using the URI for versioning sounds logical.
  3. You can use $ref to link to another schema that is then pulled in.
  4. Again, you could use $ref and JSON Pointer to import definitions from other schemas.

You can always test things out by validating your schema to see if you've made any mistakes.

like image 89
Miha Hribar Avatar answered Sep 28 '22 03:09

Miha Hribar


At the time of this writing, the current version of the JSON Schema specification is draft-v4, in which the format date-time for string instances is clearly specified and is very useful in practice.

There is no simpler format date defined so far, but you can easily define your object's property as type string and then apply a format pattern validation (ECMA 262 regex dialect) on top of it.

For example:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "title": "Example Schema"
    "description": "This schema contains only a birth date property"
    "type": "object",
    "required": ["birth_date"],
    "properties": {
        "birth_date": {
            "type": "string",
            "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
        }
    }
}
like image 39
jose.angel.jimenez Avatar answered Sep 28 '22 05:09

jose.angel.jimenez


Why don't you just use the "format" : "date" as per #5.23 in JSON Schema Draft 03?

Plus your definition of birth date doesn't contain date which seems to be an error.

like image 33
Artem Oboturov Avatar answered Sep 28 '22 03:09

Artem Oboturov