Reputation: 53342
I want to describe this JSON:
{
"key1": {},
"key2": {}
}
so I created this JSON schema:
{
"type": "object",
"patternProperties": {
".+": {
"type": "object"
}
}
}
The problem is that when I add a $schema
link to the JSON, it's not valid:
First, it seems strange that $schema
would need any kind of special treatment but even if I try this:
{
"type": "object",
"properties": {
"$schema": {
"type": "string"
}
},
"patternProperties": {
".+": {
"type": "object"
}
}
}
it's not fixed:
I'm browsing a couple of schemas at http://schemastore.org/json/ and they don't seem to have any special treatment of $schema
. How does it work?
Upvotes: 0
Views: 249
Reputation: 24489
The accepted answer is correct, but here is the workaround you need.
{
"type": "object",
"properties": {
"$schema": {
"type": "string"
}
},
"additionalProperties": {
"type": "object"
}
}
additionalProperites
applies only to properties that are defined in properties
. patternProperties
, on the other hand, applies to any property that it matches. The way you wrote it with patternProperties
means that "$schema" must be a string and it must be an object. Since those two things can never both be true, "$schema" will never validate with any value.
Upvotes: 1
Reputation: 1965
The $schema keyword is used to declare that a JSON fragment is actually a piece of JSON Schema. But it's not used in your JSON when it's not a schema, i.e. it is not used in your JSON data.
You then use a validator to match the schema against the JSON data. For exmple you can use this validator. In the left you specify the schema in the right you specify JSON data (without any reference or link to the schema, you don't use the $schema keyword in the right side)
The $schema keyword specifies which version of the JSON Schema standard the schema applies to (again the JSON schema, not the JSON data). Most of the time is:
"$schema": "http://json-schema.org/draft-04/schema#"
More info here
Upvotes: 1