{"57ecf565817bc3932d8de349": {
"prices": {
"2017-07-01": {
"summer": -1,
"winter": -1,
"xmas": -1},
"2017-08-05": {
"summer": -1,
"winter": -1,
"xmas": -1}
}
}
}
How can I write a JSON schema where each key is different (here : 2017-07-01, 2017-08-05) so I can't use items
as in this example http://json-schema.org/example1.html#definitions (Set of products schema).
This is my schema :
{
"type": "object",
"properties": {
"57ecf565817bc3932d8de349": {
"type": "object",
"properties": {
"prices": {
"type": "object",
"properties": {
"2017-07-01": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
},
"2017-08-05": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
},
"xmas": {
"type": "integer"
}
},
"required": [
"summer",
"winter",
"xmas"
]
}
},
"required": [
"2017-07-01",
"2017-08-05"
]
}
},
"required": [
"prices"
]
}
},
"required": [
"57ecf565817bc3932d8de349"
]
}
In my original JSON I have a lot of dates like this : 2017-07-01, and I would like to avoid to repeat the schema for each date.
You can do this with the additionalProperties
keyword. In this example, every property in the object must validate against the given schema.
{
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"summer": { "type": "integer" },
"winter": { "type": "integer" },
"xmas": { "type": "integer" }
},
"required": ["summer", "winter", "xmas"]
}
}
use patternProperties
https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
something like
{
"type": "object",
"patternProperties": {
"^\d{4}-\d{2}-\d{2}": {
"type": "object",
"properties": {
"summer": {
"type": "integer"
},
"winter": {
"type": "integer"
etc...
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