JSON模式验证错误

6
我在验证JSON时遇到了一些错误。我无法理解这些错误,有人能帮忙解释一下吗?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Modified JSON Schema draft v4 that includes the optional '$ref' and 'format'",
"definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
    },
    "positiveInteger": {
        "type": "integer",
        "minimum": 0
    },
    "positiveIntegerDefault0": {
        "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
    },
    "simpleTypes": {
        "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
    },
    "stringArray": {
        "type": "array",
        "items": { "type": "string" },
        "minItems": 1,
        "uniqueItems": true
    }
},
"type": "object",
"properties": {
    "id": {
        "type": "string",
        "format": "uri"
    },
    "$schema": {
        "type": "string",
        "format": "uri"
    },
    "$ref": {
        "type": "string"
    },
    "format": {
        "type": "string"
    },
    "title": {
        "type": "string"
    },
    "description": {
        "type": "string"
    },
    "default": { },
    "multipleOf": {
        "type": "number",
        "minimum": 0,
        "exclusiveMinimum": true
    },
    "maximum": {
        "type": "number"
    },
    "exclusiveMaximum": {
        "type": "boolean",
        "default": false
    },
    "minimum": {
        "type": "number"
    },
    "exclusiveMinimum": {
        "type": "boolean",
        "default": false
    },
    "maxLength": { "$ref": "#/definitions/positiveInteger" },
    "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "pattern": {
        "type": "string",
        "format": "regex"
    },
    "additionalItems": {
        "anyOf": [
            { "type": "boolean" },
            { "$ref": "#" }
        ],
        "default": { }
    },
    "items": {
        "anyOf": [
            { "$ref": "#" },
            { "$ref": "#/definitions/schemaArray" }
        ],
        "default": { }
    },
    "maxItems": { "$ref": "#/definitions/positiveInteger" },
    "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "uniqueItems": {
        "type": "boolean",
        "default": false
    },
    "maxProperties": { "$ref": "#/definitions/positiveInteger" },
    "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "required": { "$ref": "#/definitions/stringArray" },
    "additionalProperties": {
        "anyOf": [
            { "type": "boolean" },
            { "$ref": "#" }
        ],
        "default": { }
    },
    "definitions": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "default": { }
    },
    "properties": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "default": { }
    },
    "patternProperties": {
        "type": "object",
        "additionalProperties": { "$ref": "#" },
        "default": { }
    },
    "dependencies": {
        "type": "object",
        "additionalProperties": {
            "anyOf": [
                { "$ref": "#" },
                { "$ref": "#/definitions/stringArray" }
            ]
        }
    },
    "enum": {
        "type": "array",
        "minItems": 1,
        "uniqueItems": true
    },
    "type": {
        "anyOf": [
            { "$ref": "#/definitions/simpleTypes" },
            {
                "type": "array",
                "items": { "$ref": "#/definitions/simpleTypes" },
                "minItems": 1,
                "uniqueItems": true
            }
        ]
    },
    "allOf": { "$ref": "#/definitions/schemaArray" },
    "anyOf": { "$ref": "#/definitions/schemaArray" },
    "oneOf": { "$ref": "#/definitions/schemaArray" },
    "not": { "$ref": "#" }
},
"dependencies": {
    "exclusiveMaximum": [ "maximum" ],
    "exclusiveMinimum": [ "minimum" ]
},
"default": { }

这里是 JSON。

{
  "type": "identification.carrier",
  "id": "uui#abb0ef56-8562-4056-aa62-afb758a150ad",
  "happened": "2018-03-28T10:52:44.35+02:00",
  "processed": "2018-03-28T10:52:44.35+02:00",
  "tracking_id": "tid#ad2ebdb1-5d5b-4dcb-a058-494b6af89127",
  "source_attributes": {
    "id": "idsadm@app44A0",
    "data": {
      "info": "resumeIdentify: result: carrier has operator and mobile network",
      "callId": "ad2ebdb1-5d5b-4dcb-a058-494b6af89127",
      "contextId": "0aeb6745-a43c-4257-a535-6905d21a5297",
      "ids_state": "SUCCESS",
      "ids_strategy_type": "REDIRECT_MULTI_WITH_MSISDN_IN_HEADER",
      "ids_identity_type": "MSISDN",
      "country": "DE",
      "ids_result_type": "IDENTITY",
      "ids_result_network": "MobileNetwork[MOBILE=Vodafone D2 GmbH,operator=Operator[ref=10013,MNO=VODAFONE,countryIsoName=DE],countryIsoName=DE,ref=64,mnc=2,mcc=262,mvnos=[Operator[ref=30,MVNO=MOBILCOM-DEBITEL,countryIsoName=DE]]",
      "idsServiceLine": "599 387 240"
    }
  },
  "event_data": {
    "service": {
      "name": "IdsService",
      "version": "4.1"
    },
    "result": {
      "country": "DE",
      "mno": "VODAFONE",
      "mvno": "MOBILCOM-DEBITEL",
      "mcc": "262",
      "mnc": "2"
    }
  }
}

信息: JSON不符合任何“anyOf”模式。 模式路径:

/属性/类型/anyOf

信息: 无效类型。期望数组但得到字符串。 模式路径:

/属性/类型/anyOf/1/类型

信息: 值“identification.carrier”在枚举中未定义。 模式路径:

/定义/简单类型/枚举

信息: 字符串“uui#abb0ef56-8562-4056-aa62-afb758a150ad”不符合格式“uri”。 模式路径:

/属性/id/格式


你是说你不知道如何阅读错误信息还是不知道JSON Schema?你是自己编写了模式和JSON吗? - Relequestual
2个回答

4
提供的模式是略微修改过的元模式,它验证JSON模式结构。
提供的JSON数据看起来更像DTO,而不是模式。因此,您会收到有关错误类型的错误。
JSON模式类型必须是单个值或此类字符串的数组:"array","boolean","integer","null","number","object","string""identification.carrier"不在列表中,这就是为什么您会收到验证错误的原因。
最可能的情况是,您应该根据特定模式(而不是元模式)验证数据,例如(为简单起见省略了大多数属性):
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "id": {"type": "string"},
    "happened": {"type":"string", "format":"date-time"}
  },
  "required": ["type", "id"]
}

0

当使用draft-04或更高版本的模式验证JSON时,会出现此错误。问题在于“id”应该是URI格式,“type”如模式中定义的是一个数组,这些名称已被占用。因此,如果您想保持模型不变,则解决方案可能是重命名id和type。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接