Vertx(4.5.7) 当模式验证失败时,OutputUnit 不会给出验证消息

问题描述 投票:0回答:1

我尝试使用 Vertx 的 JSON Schema 标准来验证以下架构:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/create.stockincdecevent.schema.json",
  "title": "Create-Stock-Inc-Dec-event",
  "description": "Schema for the event to request a stock inc dec operation",
  "type": "object",
  "properties": {
    "userMetaData": {
      "type": "object",
      "description": "Meta attributes used by the user of the system",
      "properties": {
        "userId": {
          "type": "string",
          "description": "The user ID of the connected client"
        }
      },
      "required": [
        "userId"
      ]
    },
    "data": {
      "type": "object",
      "description": "The data that has to be processed.",
      "properties": {
        "itemID": {
          "type": "string",
          "description": "The item for which the stock has to be deducted."
        },
        "stockCount": {
          "type": "number",
          "description": "stock count to deduct."
        }
      },
      "required": [
        "itemID",
        "stockCount"
      ]
    }
  },
  "required": [
    "userMetaData",
    "data"
  ]
}

验证上述模式的数据:

{
  "userMetaData": {
    "userId": "user-1"
  },
  "data": {
    "itemID": "Item-1",
    "stockCount": 1
  }
}

验证码

OutputUnit schemaValidateResponse = Validator
  .create(schema, new JsonSchemaOptions().setBaseUri("https://example.com").setDraft(Draft.DRAFT202012))
  .validate(dataToValidate)

OutputUnit 对于上述实例返回

true
,但是,如果我给出无效实例,
OutputUnit
不会有任何错误消息。

{
  "userMetaData": {
    "userId": "user-1"
  },
  "data": {
    "itemID": "Item-1"
  }
}

任何线索将不胜感激。

提前致谢。

java jsonschema vert.x json-schema-validator
1个回答
0
投票

根据 2020-12 规范,建议但不要求显示错误消息。我同意不包含错误消息是一种糟糕的用户体验。我会向图书馆提出一个问题,看看你是否可以让他们添加一个。

与此同时,您可以使用 https://json-everything.net/json-schema (我的在线游乐场),它会给您所需的错误消息。 (请注意,我的网站提供的输出格式实际上是下一版本规范的建议格式,而不是 2020-12。)

这是您发布的实例的内容:

{
  "valid": false,
  "evaluationPath": "",
  "schemaLocation": "https://example.com/create.stockincdecevent.schema.json#",
  "instanceLocation": "",
  "details": [
    {
      "valid": true,
      "evaluationPath": "/properties/userMetaData",
      "schemaLocation": "https://example.com/create.stockincdecevent.schema.json#/properties/userMetaData",
      "instanceLocation": "/userMetaData",
      "annotations": {
        "description": "Meta attributes used by the user of the system",
        "properties": [
          "userId"
        ]
      },
      "details": [
        {
          "valid": true,
          "evaluationPath": "/properties/userMetaData/properties/userId",
          "schemaLocation": "https://example.com/create.stockincdecevent.schema.json#/properties/userMetaData/properties/userId",
          "instanceLocation": "/userMetaData/userId",
          "annotations": {
            "description": "The user ID of the connected client"
          }
        }
      ]
    },
    {
      "valid": false,
      "evaluationPath": "/properties/data",
      "schemaLocation": "https://example.com/create.stockincdecevent.schema.json#/properties/data",
      "instanceLocation": "/data",
      "errors": {
        "required": "Required properties [\"stockCount\"] are not present"
      },
      "details": [
        {
          "valid": true,
          "evaluationPath": "/properties/data/properties/itemID",
          "schemaLocation": "https://example.com/create.stockincdecevent.schema.json#/properties/data/properties/itemID",
          "instanceLocation": "/data/itemID",
          "annotations": {
            "description": "The item for which the stock has to be deducted."
          }
        }
      ]
    }
  ]
}

您要查找的错误是:

所需属性 [“stockCount”] 不存在

© www.soinside.com 2019 - 2024. All rights reserved.