JSON schema for validating list with different possible field values

百般思念 提交于 2021-02-05 11:59:26

问题


Within a JSON structure (“event bundle”), I am collecting several lists of events. These are called event lists. Each event in that list has at least a type field, whose value is dependent on which list it is stored in. Consider the following valid example:

{
    "event_bundle": {
        "alert_events": [
            { "type": "fooAlert1", "value": "bar" },
            { "type": "fooAlert2", "value": "bar" },
            { "type": "fooAlert3", "value": "bar" }
        ],
        "user_events": [
            { "type": "fooUser1", "value": "bar" },
            { "type": "fooUser2", "value": "bar" },
            { "type": "fooUser3", "value": "bar" }
        ]
    }
}

Here, alert_events can only contain events whose types are fooAlert1, fooAlert2 and fooAlert3, not others. The same goes for user_events. So, this JSON would not be valid:

{
    "event_bundle": {
        "alert_events": [
            { "type": "fooAlert1", "value": "bar" },
            { "type": "fooUser2", "value": "bar" },
            { "type": "foo", "value": "bar" }
        ],
        "user_events": [
            { "type": "fooAlert1", "value": "bar" },
            { "type": "fooUser2", "value": "bar" },
            { "type": "foo", "value": "bar" }
        ]
    }
}

It's invalid, since fooUser2 cannot appear in an event that is in an alert_events list, and fooAlert1 cannot appear in the user_events. Also, the event foo is not valid at all.

I currently have the following simple schema:

{
  "type": "object",
  "definitions": {
    "event_list": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/event"
      }
    },
    "event": {
      "type": "object",
      "required": ["type", "value"],
      "properties": {
        "type": { "type": "string" },
        "value": { "type": ["string", "object"] },
        "source": { "type": ["string", "null"] }
      }
    }
  },
  "properties": {
    "event_bundle": {
      "type": "object",
      "properties": {
        "alert_events": {
          "$ref": "#/definitions/event_list"
        },
        "user_events": {
          "$ref": "#/definitions/event_list"
        }
      }
    }
  }
}

However, this does not fully validate the allowable types.

I know I could define different event_lists with different events, but that would mean I'd have to define potentially dozens of lists and events.

Is there an easier way to say, I want to validate that event objects in an alert_events list can have one set of types, and for another list another set of types?


回答1:


You cannot reference and use instance data as values in your validation.

What you are asking is business logic, and not to do with the "shape" of your JSON data, which is the remit of JSON Schema.

You would need to implement this as your application logic.



来源:https://stackoverflow.com/questions/57671202/json-schema-for-validating-list-with-different-possible-field-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!