Search code examples
jsonjsonschemajson-schema-validator

json schema is not validating correctly


I have this json schema but it is not validating correctly. Only the first pattern is used.

 {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "PrimaryCategory": {
      "type": "object",
      "properties": {
        "CategoryID": {
          "type": "integer"
        },
        "Name": {
          "type": "string"
        },
        "Specifics": {
          "type": "object",
          "patternProperties": {
            "^.*$": {
              "type": "array",
              "items": {
                "type": "object",
                "patternProperties": {
                  "^.*$": {
                    "type": "boolean"
                  }
                }
              },
            "^.*$": {
              "type": "string"
                }
            }
          }
        }
      },
      "required": [
        "CategoryID",
        "Name"
      ]
    }
  },
  "required": [
    "PrimaryCategory"
  ]
}

The example should be:

{
 "PrimaryCategory": {
    "CategoryID": "2312312",
    "Name": "category",
    "Specifics": {
      "field1": [
          {"bool1": true},
          {"bool2": false}
      ],
      "field2": "value1",
      "field3": "value2"
    }
 }
}

Can you find the problem? The "Specifics" field it is an object that can contains an array of objects with an unkown property name, it has an unkown property name and others unkown named properties.


Solution

  • You have two ^.*$ keys in patternProperties. That's duplicate keys in JSON. (JSON allows it, but the behavior is undefined for JSON Schema.)