2016-03-07 88 views
0

这个JSON文件应该验证失败,但它没有验证。有人告诉我为什么。针对JSON模式的JSON验证:为什么这个显而易见的JSON数据没有通过验证

插件以下JSON数据和模式进入这个网站,验证,
http://json-schema-validator.herokuapp.com 和我的骡子验证JSON模式相同的结果。它显然不符合模式(我添加了一些字段,我拼错了一些字段,日期时间值不是真正的日期时间),但它并没有使它失败。有人能告诉我为什么吗?

JSON模式:

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "id": "http://hud.gov/ocio/xsd/esb/serviceauditingframework/2.0#", 
    "definitions": { 
    "serviceAuditLogData": { 
     "type": "object", 
     "title": "serviceAuditLogData", 
     "required": [ 
     "serviceRequestTimestamp", 
     "sourceSystem" 
     ], 
     "properties": { 
     "auditId": { 
      "type": "string" 
     }, 
     "serviceRequestTimestamp": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "serviceProvider": { 
      "type": "string" 
     }, 
     "serviceProviderVersion": { 
      "type": "string" 
     }, 
     "serviceProviderTimestamp": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "eventType": { 
      "type": "string" 
     }, 
     "eventDetail": { 
      "type": "string" 
     }, 
     "hostName": { 
      "type": "string" 
     }, 
     "sourceSystem": { 
      "type": "string" 
     }, 
     "authenticationId": { 
      "type": "string" 
     }, 
     "endUserId": { 
      "type": "string" 
     }, 
     "inputData": { 
      "type": "string" 
     } 
     }, 
     "propertiesOrder": [ 
     "auditId", 
     "serviceRequestTimestamp", 
     "serviceProvider", 
     "serviceProviderVersion", 
     "serviceProviderTimestamp", 
     "eventType", 
     "eventDetail", 
     "hostName", 
     "sourceSystem", 
     "authenticationId", 
     "endUserId", 
     "inputData" 
     ] 
    } 
    } 
} 

JSON数据

{ 
     "serviceAuditLogData": { 
     "junk":"asdfasdf", 
     "serviceRequestTimestamp": "2004-09-29T12:58:31.470Z", 
     "serviceProvider": "FLQS", 
     "serviceProviderVersion": "v1.0.1", 
     "audit_id": "17f24136-2494-4bf8-9d3b-9baafaae0cc9", 
     "serviceProviderTimestamp": "2012-11-04T21:44:57.997Z", 
     "eventType": "Query Pool", 
     "eventDetail": "default pool", 
     "hostName": "esb-d-srv1.", 
     "sourceSystem": "LRS", 
     "authenticationId": "EsbLrsAccount", 
     "endUserId": "H574857", 
     "inputData": "L234234234, L32453462345, L23452346" 
     } 
    } 

回答

1

它不会失败,因为你的架构没有任何强制约束。请注意,definitions不是jsonschema关键字,表示约束验证。它通常用于放置在模式定义的其他部分重新使用的子模式。因此,首先,您应该更改definitions关键字properties

jsonschema另一个常见的误解与properties关键字有关。让我们来看看下面的例子:

{ 
    "type" : "object", 
    "properties" : { 
     "key1" : { 
      "type" : "string" 
     } 
    } 
} 

你必须把它读作:json必须是一个对象,并且它包含一个键等于key1的情况下,其值必须是一个字符串。根据以下两个JSON对象是有效的:

{ 
    "key2":12 
} 

和:

{ 
    "key1":"sdf" 
} 

最后,涉及到date-time格式,你必须检查section 6 of RFC3339,以确保你有一个有效的日期时间。在任何情况下,jsonschema验证器都不是强制实施格式。

+0

纠错:'定义'_是一个模式关键字;然而,它的使用被明确提到作为subschemas的占位符来在当前模式中进行验证。否则你的解释是正确的。假设它不是一个限制验证的关键字:) – fge

+0

已更正。谢谢 – jruizaranguren

0

谢谢@jruizaranguren我还了解到,我需要将 “additionalProperties”:false和“required”:确保在API中传递什么是预期的。

以下是我如何解决我的问题。

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "type": "object", 
    "definitions": { 
    "serviceAuditLogData": { 
     "type": "object", 
     "additionalProperties": false, 
     "required": [ 
     "auditCorrelationId", 
     "serviceRequestTimestamp", 
     "serviceProvider", 
     "serviceProviderVersion", 
     "serviceProviderTimestamp", 
     "eventType", 
     "hostName", 
     "sourceSystem", 
     "authenticationId" 
     ], 
     "properties": { 
     "auditCorrelationId": { 
      "type": "string" 
     }, 
     "serviceRequestTimestamp": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "serviceProvider": { 
      "type": "string" 
     }, 
     "serviceProviderVersion": { 
      "type": "string" 
     }, 
     "serviceProviderTimestamp": { 
      "type": "string", 
      "format": "date-time" 
     }, 
     "eventType": { 
      "type": "string" 
     }, 
     "eventDetail": { 
      "type": "string" 
     }, 
     "hostName": { 
      "type": "string" 
     }, 
     "sourceSystem": { 
      "type": "string" 
     }, 
     "authenticationId": { 
      "type": "string" 
     }, 
     "endUserId": { 
      "type": "string" 
     }, 
     "inputData": { 
      "type": "string" 
     } 
     } 
    } 
    }, 
    "additionalProperties": false, 
    "required": [ 
    "serviceAuditLogData" 
    ], 
    "properties": { 
    "serviceAuditLogData": { 
     "$ref": "#/definitions/serviceAuditLogData" 
    } 
    } 
}