You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vertx 4.5.7中OutputUnit验证失败无校验消息问题求助

Vert.x JSON Schema Draft202012验证未检测到必填字段缺失的问题

问题背景

我使用Vert.x的JSON Schema验证功能,针对Draft2020-12版本的Schema进行验证:

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

验证代码:

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

使用包含所有必填字段的有效数据验证时,OutputUnit返回true,符合预期;但传入缺失stockCount的无效数据时:

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

OutputUnit未返回任何错误消息,无法检测到必填字段缺失的问题。

解决思路

  • 检查Vert.x版本兼容性:部分旧版本Vert.x的JSON Schema模块对Draft2020-12的嵌套对象required关键字支持不完善,建议升级到4.5.x及以上的稳定版本,确保验证逻辑完全符合规范。

  • 启用严格验证模式:在JsonSchemaOptions中开启严格模式,强制验证器严格遵循Schema约束:

    JsonSchemaOptions options = new JsonSchemaOptions()
        .setBaseUri("https://example.com")
        .setDraft(Draft.DRAFT202012)
        .setStrict(true);
    OutputUnit schemaValidateResponse = Validator.create(schema, options).validate(dataToValidate);
    
  • 确认Schema加载正确性:确保Schema被正确解析为Vert.x的JsonSchema对象,避免因解析错误导致约束失效。示例加载方式:

    String schemaJsonStr = "..."; // 你的Schema JSON字符串
    JsonSchema schema = JsonSchema.of(Json.decodeValue(schemaJsonStr));
    
  • 主动提取验证错误细节:不要仅依赖isValid()结果,主动获取错误和警告列表,排查是否有被忽略的问题:

    if (!schemaValidateResponse.isValid()) {
        List<ValidationException> errors = schemaValidateResponse.getErrors();
        for (ValidationException error : errors) {
            System.out.println("验证错误:" + error.getMessage());
        }
    } else {
        List<ValidationException> warnings = schemaValidateResponse.getWarnings();
        if (!warnings.isEmpty()) {
            System.out.println("验证警告:" + warnings);
        }
    }
    
  • 添加additionalProperties约束(可选):在嵌套对象中设置"additionalProperties": false,强制验证器检查对象结构的严谨性,避免额外字段干扰必填项验证:

    "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"
      ],
      "additionalProperties": false
    }
    

内容的提问来源于stack exchange,提问作者Tijo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 22:08:09