Home > Software design >  How to use patternProperties in the Json Schema
How to use patternProperties in the Json Schema

Time:12-16

My json format is

{
    "group_1": {
        "sensor_1": {},
        "sensor_2": {}
    },
    "group_2": {
        "sensor_1": {},
        "sensor_2": {}
    }
}

and json schema is


{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "patternProperties": {
        "^(group_[0-9] )$": {
            "type": "object",
            "patternProperties": {
                "^(sensor_[0-9] )$": {
                    "type": "object"
                }
            }
        }
    },
    "additionalProperties": false
}

If my json data as below, the "sensor_aaaa" key is still verified successfully, but I want it to fail. Because its pattern is "^(sensor_[0-9] )$".

{
    "group_1":{
        "sensor_1":{
        },
        "sensor_2":{
        },
        "sensor_aaaa":{
        }
    }
}

If I add "group_aaaa" key in the first level, it can verified failed, so why the "sensor_aaaa" verified successfully? How can I modify my json schema ?

{
    "group_1":{
        "sensor_1":{
        },
        "sensor_2":{
        }
    },
    "group_aaaa":{
        "sensor_1":{
        },
        "sensor_2":{
        }
    }
}

Thanks !!!!! (Sorry, My English is not very good >__<)

Oh...

I forgot to add ("additionalProperties": false). The following json schema can be correctly verified.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "additionalProperties":false,
    "patternProperties": {
        "^(group_[0-9] )$": {
           "additionalProperties":false,
            "type": "object",
            "patternProperties": {
                "^(sensor_[0-9] )$": {
                    "type": "object",
                }
            }
        }
    }
}

CodePudding user response:

Oh...

I forgot to add ("additionalProperties": false). The following json schema can be correctly verified.

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "additionalProperties":false,
    "patternProperties": {
        "^(group_[0-9] )$": {
           "additionalProperties":false,
            "type": "object",
            "patternProperties": {
                "^(sensor_[0-9] )$": {
                    "type": "object",
                }
            }
        }
    }
}

CodePudding user response:

This did have me scratching my head for a while.

additionalProperties only works by evaluating properties and patternProperties within the same schema object.

"sensor_aaa" is consider valid because the subschema does not define `additionalProperties: false".

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "patternProperties": {
    "^(group_[0-9] )$": {
      "type": "object",
      "patternProperties": {
        "^(sensor_[0-9] )$": {
          "type": "object"
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}

You can see this working using this live playground: https://jsonschema.dev/s/DMIi1. Although it only supports draft-07 of JSON Schema, the keywords used here do not change meaning between draft-06 and draft-07.

  • Related