Reputation: 17441
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:
$schema
attribute?$schema
.[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!
Upvotes: 5
Views: 7375
Reputation: 4385
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.
Upvotes: 2
Reputation: 2247
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}$"
}
}
}
Upvotes: 3
Reputation: 5791
$schema
can be used to specify the schema it conforms to.$ref
to link to another schema that is then pulled in.$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.
Upvotes: 4
Reputation: 5657
The spec seems to suggest you could:
Other type values MAY be used for custom purposes,...
It then goes on to discuss what validators of the minimal implementation might do.
To my mind, what you want to do seems OK. It might help your schema's clarity to keep the type reference and type definition in the same file.
I think your file includes Q should be handled outside of json, e.g. have a dev step that generates the complete json from a script/template (e.g. erb or some such) merging your subfiles. To my mind your service should always deliver the full json required to fully interact with that service. If this gets unmanageable from the clients perspective, it may be a signal to refactor and introduce another service.
Upvotes: 1