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!
$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.
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}$"
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With