json 应该且只能对一个架构有效,但多个架构无效错误

7tofc5zh  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(103)

我正在尝试为以下对象模型创建一个json架构。但是,当我尝试根据架构验证数据时,出现以下错误。

should be valid to one and only one of schema, but more than one are valid: {"$ref":"#/$defs/drug-treatment"}{"$ref":"#/$defs/surgery-treatment"}

下面是我的架构

{
  "$id": "someid",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "description": "JSON Schema for treatments",
  "oneOf" : [
    { "$ref" : "#/$defs/drug-treatment" },
    { "$ref" : "#/$defs/surgery-treatment" }
  ],
  "$defs": {
    "base": {
      "type": "object",
      "properties": {
        "type-tag": {
          "enum": [ "SURGERY", "DRUGTREATMENT", "RADIOLOGY", "PHYSIOTHERAPY" ]
        },
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "patient-id": {
          "type": "string",
          "format": "uuid"
        },
        "patient-name": {
          "type": "string"
        },
        "provider-id": {
          "type": "string",
          "format": "uuid"
        },
        "provider-name": {
          "type": "string"
        },
        "diagnosis": {
          "type": "string"
        },
        "followup-treatments": {
          "type" : "array",
          "items" : { "$ref" : "#" }
        }
      },
      "required": [
        "id",
        "type-tag",
        "patient-id",
        "patient-name",
        "provider-id",
        "provider-name",
        "diagnosis",
        "followup-treatments"
      ]
    },
    "drug-treatment": {
      "allOf": [
        { "$ref" : "#/$defs/base" }
      ],
      "properties": {
        "drug": {
          "type": "string"
        },
        "dosage": {
          "type": "number"
        },
        "start-date": {
          "type": "string",
          "format": "date"
        },
        "end-date": {
          "type": "string",
          "format": "date"
        },
        "frequency": "integer"
      },
      "required": [
        "drug",
        "dosage",
        "start-date",
        "end-date",
        "frequency"
      ],
      "unevaluatedProperties" : false
    },
    "surgery-treatment": {
      "allOf": [
        { "$ref" : "#/$defs/base" }
      ],
      "properties": {
        "surgery-date": {
          "type": "string",
          "format": "date"
        },
        "discharge-instructions": {
          "type": "string"
        },
        "required": [
          "surgery-date",
          "discharge-instructions"
        ],
        "unevaluatedProperties" : false
      }
    },
  }
}

下面是正在验证模式的示例数据。

"treatments": [
    {
      "type-tag": "DRUGTREATMENT",
      "drug": "fdsds",
      "dosage": 2.0,
      "start-date": "2222-02-12",
      "end-date": "2222-02-12",
      "frequency": 2,
      "id": "aa7da984-0252-45b1-b0cd-f1dbe98662e2",
      "patient-id": "ab62420e-0bd8-4e39-8e0b-36e464b7abb2",
      "patient-name": "Tom",
      "provider-id": "154523b2-7598-4ed4-aab1-b2ef1692109c",
      "provider-name": "gdsfdsd",
      "diagnosis": "sfdsfds",
      "followup-treatments": []
    },
    {
      "type-tag": "SURGERY",
      "surgery-date": "2222-02-12",
      "discharge-instructions": "dsfdsfdsfds",
      "id": "12d2e565-8966-4029-840b-1959277b37f6",
      "patient-id": "ab62420e-0bd8-4e39-8e0b-36e464b7abb2",
      "patient-name": "Tom",
      "provider-id": "154523b2-7598-4ed4-aab1-b2ef1692109c",
      "provider-name": "gdsfdsd",
      "diagnosis": "fdsfds",
      "followup-treatments": [],
    }

  ]

但当我验证类型“药物治疗”时,它给我错误

should be valid to one and only one of schema, but more than one are valid: {"$ref":"#/$defs/drug-treatment"}{"$ref":"#/$defs/surgery-treatment"}

你知道我错过了什么吗?

lx0bsm1f

lx0bsm1f1#

我对你的模式做了一些修改,但这应该适用于你问题中的样本数据。你的模式有一些语法错误和关键字放置错误:

{
  "$id": "someid",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
  "type": "object",
  "properties": {
    "treatments": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/treatment-item"
      }
    }
  },
  "$defs": {
    "treatment-item": {
      "oneOf": [
        {
          "$ref": "#/$defs/drug-treatment"
        },
        {
          "$ref": "#/$defs/surgery-treatment"
        }
      ]
    },
    "base": {
      "type": "object",
      "properties": {
        "type-tag": {
          "enum": [
            "SURGERY",
            "DRUGTREATMENT",
            "RADIOLOGY",
            "PHYSIOTHERAPY"
          ]
        },
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "patient-id": {
          "type": "string",
          "format": "uuid"
        },
        "patient-name": {
          "type": "string"
        },
        "provider-id": {
          "type": "string",
          "format": "uuid"
        },
        "provider-name": {
          "type": "string"
        },
        "diagnosis": {
          "type": "string"
        },
        "followup-treatments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/treatment-item"
          },
          "minItems": 0
        }
      },
      "required": [
        "id",
        "type-tag",
        "patient-id",
        "patient-name",
        "provider-id",
        "provider-name",
        "diagnosis",
        "followup-treatments"
      ]
    },
    "drug-treatment": {
      "allOf": [ { "$ref": "#/$defs/base" } ],
      "properties": {
        "drug": {
          "type": "string"
        },
        "dosage": {
          "type": "number"
        },
        "start-date": {
          "type": "string",
          "format": "date"
        },
        "end-date": {
          "type": "string",
          "format": "date"
        },
        "frequency": {
          "type": "integer"
        }
      },
      "required": [
        "drug",
        "dosage",
        "start-date",
        "end-date",
        "frequency"
      ],
      "unevaluatedProperties": false
    },
    "surgery-treatment": {
      "allOf": [ { "$ref": "#/$defs/base" } ],
      "properties": {
        "surgery-date": {
          "type": "string",
          "format": "date"
        },
        "discharge-instructions": {
          "type": "string"
        },
        "unevaluatedProperties": false
      },
      "required": [ "surgery-date", "discharge-instructions" ]
    }
  }
}

编辑:显示带有任意附加属性的错误消息。

相关问题