简体   繁体   中英

Variable Properties in JSON Schema

I have the following example JSON, for which I want to write a JSON Schema. The constraint is that property2 holds a list of string, which are dynamic, depending on the dataset. And in the property3 object some of the properties are named afters those strings.

 {
   "property1": "value",
   "property2": ["value1","value2","value2"],
   "property3": {
                   "title": "test",
                   "value1": "hello",
                   "value2": "world"
    }
 }

A JSON-Schema might look like this, but I don't know how to describe that the dynamic properties. Is that possible?

 {
   "title": "Test Object",
   "type": "object",
   "properties": {
              "property1": {
                     "type": "string"
               }
              "property1": {
                     "type": "array"
               }
              "property3": {
                     "type": "object",
                     "properties": {
                               "title": {
                                     "type": "string"
                               }
                               [ Something is missing here ]
                      }
               }
    }
 }

You can not relate property values to property keys with Json-Schema (as of Draft v4).

You can:

  • Define property2 as an schema and reference it (#ref) from property3 (dependencies,allOf, anyOf, additionalProperties...). If these property names are very dynamic in your case you might build this schema on the fly.
  • Use patternProperties in order to retrict valid property names in property3 to some fixed regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM