0

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 - https://www.jsonschemavalidator.net/

The problem is that this schema does validate the message, But only the first object of array is processed as de. For instance, such a JSON is validated as well (see "conditions" object #2):

{
  "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": 123,
        "dateBegin": [1,2,3],
        "dateEnd": 1
      },
      {
        "conditionsType": "C",
        "dateBegin": "2021-11-22T17:30:43.511Z",
        "dateEnd": "2021-11-22T17:35:43.511Z"
      }
    ]
  }
}

Is that actually the right direction I've chosen for this task ?

1 Answer 1

1

Two things. You have a typo in your items schema where you actually want to have type and not conditionsType. Secondly, if the items keyword is an array, the items of the array are validated against the schemas in this order. You want to have the items keyword as a single schema which is then applied to all items. Your corrected schema for copy-paste:

{"$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":{"type":"object","properties":{"conditionsType":{"enum":["A","B","C"]},"dateBegin":{"type":"string","format":"date-time"},"dateEnd":{"type":"string","format":"date-time"}},"required":["conditionsType","dateBegin","dateEnd"]}}}}}}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.