Json模式oneOf无法按预期工作

时间:2020-07-02 08:56:33

标签: json schema

我正在尝试创建一个模式,该模式允许有效负载或有效负载转换,但不能同时允许两者。如果payloadTransition存在,则stateMaps必须存在,但是如果payload存在,则stateMaps不存在。这是我的json模式:

  "$id": "mqttsources-op",
  "constructor": {
    "schema": {
      "v0": {
        "type": "object",
        "properties": {
          "name": {
            "description": "Name of the mqttsources version of the service",
            "type": "string"
          },
          "host": {
            "description": "An IP-adress to the broker",
            "type": "string"
          },
          "port": {
            "description": "Port to the broker (default is 1883)",
            "type": "integer",
            "default": 1883
          },
          "modelMaps": {
            "description": "Array of topics that you wish to subscribe to and their corresponding service, model, interface etc..",
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "topic": {
                  "description": "The topic to subscribe to",
                  "type": "string"
                },
                "service": {
                  "description": "The service where the desired interface is located",
                  "type": "string"
                },
                "interface": {
                  "description": "The interface you wish to implement",
                  "type": "string"
                },
                "model": {
                  "description": "The model that you wish to emulate",
                  "type": "string"
                },
                "defaultState": {
                  "description": "Starting state or if no maps match or no payload provided",
                  "type": "string"
                },
                "stateMaps": {
                  "$id": "#/stateMaps",
                  "description": "Translates a payload to a state and vice versa",
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "payload": {
                        "description": "Value that represent the state, for example 0",
                        "type": "string"
                      },
                      "state": {
                        "description": "The state, for example isClosed",
                        "type": "string"
                      }
                    }
                  }
                },
                "eventMaps": {
                  "$id": "#/eventMaps",
                  "description": "Translates a payloadTransition OR a payload to an event and vice versa",
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "payload": {
                        "description": "Payload that represent the event, for example 0",
                        "type": "string"
                      },
                      "payloadTransition": {
                        "description": "Payload transistion that represents the event",
                        "type": "object",
                        "properties": {
                          "from": {
                            "description": "The payload it transitions from",
                            "type": "string"
                          },
                          "to": {
                            "description": "The payload it transitions from",
                            "type": "string"
                          }
                        }
                      },
                      "event": {
                        "description": "The event, for example doorOpenedEvent",
                        "type": "string"
                      }
                    }
                  }
                },
                "oneOf": [
                  {"$ref": "#/eventMaps/payloadTransition"},
                  {"$ref": "#/eventMaps/payload"}
                ],
                "oneOf": [
                  { "required": [ {"$ref": "#/eventMaps/payloadTransition"},
                                  {"$ref": "#stateMaps"}]
                  },
                  { "required": [ {"$ref": "#/eventMaps/payload"}],
                    "not": { "required": [ {"$ref": "#/stateMaps"}]}
                  }
                ]
              }
            }
          },
          "ttl": {
            "description": "TIME TO LIVE: Interval for how often a ping is sent to the server (default is 60 seconds)",
            "type": "integer",
            "default": 60
          },
          "qos": {
            "description": "QUALITY OF SERVICE: Desired quality of service (default is 0)",
            "type": "integer",
            "default": 0
          }
        },
        "required": [ "host", "modelMaps"],
        "additionalProperties": false
      }
    }
  }
}

总体上来说,我对json还是很陌生,对oneOf和复杂的模式(如我当前所需的模式)来说,还是很新的。我正在使用的输入文件如下所示,它会验证不应该使用的架构:

{
  "name": "MQTT-DPS",
  "host": "1.2.3.4",
  "port": 1883,
  "modelMaps": [
    {
      "topic": "joelsigurd/feeds/pir",
      "service": "portalcomp",
      "interface": "doormonitor-op-obs",
      "model": "doorPositionModel",
      "defaultState": "isUnknown",
      "stateMaps": [
        {
          "payload": "0",
          "state": "isClosed"
        },
        {
          "payload": "1",
          "state": "isOpen"
        }
      ],
      "eventMaps": [
        {
          "payloadTransition": {
            "from": "0",
            "to": "1"
          },
          "payload": "1",
          "event": "doorOpenedEvent"
        },
        {
          "payloadTransition": {
            "from": "1",
            "to": "0"
          },
          "event": "doorClosedEvent"
        }
      ]
    }
  ]
}

我在做什么错了?

0 个答案:

没有答案