Home > Back-end >  JSON-schema object array validation
JSON-schema object array validation

Time:03-04

I have a mission to validate such a JSON message :

{
  "header": {
    "action": "change_time",
    "taskGuid": "someTaskGuid",
    "publishDate": "2012-04-23T18:25:43.511Z"
  },
  "data": {
    "code": "f2103839",
    "conditions": [
      {
        "conditionsType": "A",
        "dateBegin": "2021-11-22T17:30:43.511Z",
        "dateEnd": "2021-11-22T17:35:43.511Z"
      },
      {
        "conditionsType": "B",
        "dateBegin": "2021-11-22T17:30:43.511Z",
        "dateEnd": "2021-11-22T17:35:43.511Z"
      },
      {
        "conditionsType": "C",
        "dateBegin": "2021-11-22T17:30:43.511Z",
        "dateEnd": "2021-11-22T17:35:43.511Z"
      }
    ]
  }
}

I've made such a JSON-schema to achieve that :

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "title": "Some schema",
  "description": "Some schema",
  "type": "object",
  "required": [
    "header",
    "data"
  ],
  "properties": {
    "header": {
      "type": "object",
      "required": [
        "action",
        "taskGuid",
        "publishDate"
      ],
      "properties": {
        "action": {
          "enum": [
            "create_work_order",
            "change_time",
            "cancel_work"
          ]
        },
        "taskGuid": {
          "type": "string"
        },
        "publishDate": {
          "type": "string",
          "format": "date-time"
        }
      }
    },
    "data": {
      "type": "object",
      "required": [
        "code",
        "conditions"
      ],
      "properties": {
        "code": {
          "type": "string"
        },
        "conditions": {
          "type": "array",
          "items": [
            {
              "conditionsType": "object",
              "properties": {
                "type": {
                  "enum": [
                    "A",
                    "B",
                    "C"
                  ]
                },
                "dateBegin": {
                  "type": "string",
                  "format": "date-time"
                },
                "dateEnd": {
                  "type": "string",
                  "format": "date-time"
                }
              },
              "required": [
                "conditionsType",
                "dateBegin",
                "dateEnd"
              ]
            }
          ]
        }
      }
    }
  }
}

The conditions array will consist of 1-3 objects described by items. Each object should have a unique conditionsType.

I'm checking validation with this instrument - enter image description here

  • Related