Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema with unknown property names

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?

like image 966
Thami Bouchnafa Avatar asked Aug 17 '15 07:08

Thami Bouchnafa


People also ask

What is JSON Schema additionalProperties?

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.

What does allOf mean in JSON Schema?

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 .

What is required in JSON Schema?

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.

What are properties in JSON?

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.


1 Answers

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 
like image 115
pink spikyhairman Avatar answered Sep 19 '22 12:09

pink spikyhairman