允许其中一个基于相同的基本类型

时间:2017-11-08 11:02:05

标签: json jsonschema

我有以下json架构:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "schema validating people and vehicles",
  "definitions": {
    "base": {
      "properties": {
        "age": {
          "type": "integer"
        }
      },
      "required": [
        "age"
      ]
    },
    "person": {
      "$ref": "#/definitions/base",
      "additionalProperties": false,
      "properties": {
        "firstName": {
          "type": "string"
        },
        "lastName": {
          "type": "string"
        },
        "sport": {
          "type": "string"
        }
      },
      "required": [
        "firstName"
      ]
    },
    "vehicle": {
      "$ref": "#/definitions/base",
      "additionalProperties": false,
      "properties": {
        "vehicle": {
          "type": "string"
        },
        "price": {
          "type": "integer"
        }
      }
    }
  },
  "type": "object",
  "oneOf": [
    {
      "$ref": "#/definitions/person",
    },
    {
      "$ref": "#/definitions/vehicle",
    }
  ]
}

我希望它能够验证

{"firstName":"John", "lastName":"Doe", "sport": "football", "age": 15}

以及

{"type": "car", "price": 100, "age": 3}

我收到以下错误JSON is valid against more than one schema from 'oneOf'. Valid schema indexes: 0, 1.

为什么它对多于一个有效? (firstName仅在person中定义,type仅在vehicle中定义。)