针对JSON模式的JSON验证:为什么这个明显的JSON数据没有失败验证

时间:2016-03-07 04:52:33

标签: json validation schema jsonschema json-schema-validator

此JSON文件应该无法通过验证,但事实并非如此。有人告诉我原因。

将以下json数据和架构插入此网站,验证,
http://json-schema-validator.herokuapp.com 我在Mule Validate JSON Schema中得到了相同的结果。它显然不符合架构(我添加了一些字段,我拼错了一些字段,日期时间值不是真正的日期时间),但它不会失败。有人可以告诉我为什么吗?

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"
      }
    }

2 个答案:

答案 0 :(得分:1)

它不会失败,因为您的架构不强制执行任何约束。请注意,definitions不是约束验证的jsonschema关键字。它通常用于放置在模式定义的其他部分中重用的子模式。因此,首先,您应该更改definitions的{​​{1}}关键字。

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

properties

您必须将其读作:{ "type" : "object", "properties" : { "key1" : { "type" : "string" } } } 必须是对象,如果它包含等于json的键,则其值必须是字符串。根据以下两个json对象是有效的:

key1

{
    "key2":12
}

最后,与{ "key1":"sdf" } 格式相关,您必须检查section 6 of RFC3339以确保您拥有有效的日期时间。在任何情况下,date-time验证器都不强制执行格式。

答案 1 :(得分: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"
    }
  }
}