【问题标题】:JSON validation against JSON schemas: Why is this obvious JSON data not failing validation针对 JSON 模式的 JSON 验证:为什么这个明显的 JSON 数据没有通过验证
【发布时间】:2016-06-20 13:40:07
【问题描述】:

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

【问题讨论】:

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


    【解决方案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 验证器中格式的实现不是强制性的。

    【讨论】:

    • 更正:definitions 架构关键字;但是,它的使用被明确提到作为子模式的占位符,以在当前模式中进行验证。否则你的解释是正确的。假设它不是限制验证的关键字:)
    【解决方案2】:

    感谢@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"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2017-01-09
      • 2014-12-16
      • 2021-10-21
      • 2016-08-27
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多