0
votes

I'm attempting to define the following in JSON schema:

"codenumber": {
     "12345": [
        {
           "prop1": "yes",
           "prop2": "no"
        }
     ]
  }

The codenumber object contains a property "12345" which is always a string number that contains an array. The number value can change however, so I cannot simply define this like so:

"codenumber": {
          "type": "object",
          "properties": {
              "12345": { 
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "prop1": { "type": "string" },
                        "prop2": { "type": "string" }
                    }
                }
              }
          }  
      }

Any way I can just define the first property name to be of any type of string?

1
Using a json schema is defining what properties are allowed/can be validated. Having a variable as a property name kind of defeats the purpose of a schema?daveBM

1 Answers

6
votes

You can use "patternProperties" instead of "properties":

"codenumber": {
      "type": "object",
      "patternProperties": {
          "^[0-9]+$": { 
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "prop1": { "type": "string" },
                    "prop2": { "type": "string" }
                }
             }
         }
     }  
 }