I want to have a JSON Schema with unknown property names in an array of objects. A good example is the meta-data of a web page:
"meta": { "type": "array", "items": { "type": "object", "properties": { "unknown-attribute-1": { "type": "string" }, "unknown-attribute-2": { "type": "string" }, ... } } }
Any ideas please, or other way to reach the same?
The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.
By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .
JSON Schema Example A little description of the schema. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. Defines various keys and their value types, minimum and maximum values to be used in JSON file. This keeps a list of required properties.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant.
Use patternProperties
instead of properties
. In the example below, the pattern match regex .*
accepts any property name and I am allowing types of string
or null
only by using "additionalProperties": false
.
"patternProperties": { "^.*$": { "anyOf": [ {"type": "string"}, {"type": "null"} ] } }, "additionalProperties": false
... or if you just want to allow a string in your "object" (like in the original question):
"patternProperties": { "^.*$": { {"type": "string"}, } }, "additionalProperties": false
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