JSON schema for unknown properties that are objects with date-time property names

Viewed 29

Here's some sample data I'm trying to build a schema for:

{
    "measurement data": {
        "interval data": {
            "2022-09-14T16:00:00+0:00": {
                "duration": 300,
                "Leq": 65.2
            },
            "2022-09-14T16:05:00+0:00": {
                "duration": 300,
                "Leq": 64.3
            },
            "2022-09-14T16:10:00+0:00": {
                "duration": 300,
                "Leq": 61.1
            }
        }
    }
}

The properties of interval data are objects with property names that are ISO 8601 formatted date-time strings. What's the best way to implement this?

I think I could use patternProperties with a regex to match an ISO 8601 string. I took a stab at this below (there's an escaping issue I'm stuck on, so it's broken).

There's already a format definition called 'date-time' in the JSON schema which would be handy to use but I don't see how this can be used in patternProperties.

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$id": "http://example.com/example.json",
  "type": "object",
  "default": {},
  "title": "Root Schema",
  "required": [
    "measurement data"
  ],
  "properties": {
    "measurement data": {
      "type": "object",
      "default": {},
      "title": "The measurement data Schema",
      "required": [
        "interval data"
      ],
      "properties": {
        "interval data": {
          "type": "object",
          "default": {},
          "title": "The interval data Schema",
          "patternProperties": {
            "^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$": {
              "type": "object",
              "default": {},
              "title": "The timestamp Schema",
              "required": [
                "duration",
                "Leq"
              ],
              "properties": {
                "duration": {
                  "type": "integer",
                  "default": 0,
                  "title": "The duration Schema",
                  "examples": [
                    300
                  ]
                },
                "Leq": {
                  "type": "number",
                  "default": 0,
                  "title": "The Leq Schema",
                  "examples": [
                    65.2
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}
2 Answers

Regular expressions start in / and end in /. So maybe instead of:
"^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$"
try:
"/^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$/".

Related