[英]Json schema validation error
我在验证 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”的任何模式。 架构路径:
消息:无效类型。 需要数组但得到字符串。 架构路径:
消息:枚举中未定义值“identification.carrier”。 架构路径:
消息:字符串 'uui#abb0ef56-8562-4056-aa62-afb758a150ad' 未针对格式 'uri' 进行验证。 架构路径:
提供的架构是一个稍微修改的元架构,它验证 JSON 架构结构。
提供的 JSON 数据看起来更像是 DTO,而不是模式。 因此,您会收到有关错误类型的错误消息。
JSON 模式类型必须是单个值或此类字符串的"array","boolean","integer","null","number","object","string"
: "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"]
}
使用架构草案 04或更高版本验证 JSON 时会发生此错误,问题在于“id”应采用 URI 格式,而架构中定义的“类型”是一个数组,则采用此名称,因此如果您想保持模型不变,解决方案可能是重命名 id 和类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.