JSON Schema Validation 支持自定义错误消息吗?

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

我正在使用 JSON Schema 来验证 json 对象,如下所示:

{
     "json": 
           { "is_expanded_identification": false, "identity":
                                                  { "concat_id": 44446543R, "concat_id_type_code": 1 } 
           }
        ,
     "study_year": "2025"      
}

这是我的 Json 架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "json",
    "study_year"
  ],
  "properties": {
    "json": {
      "type": "object",
      "required": [
        "is_expanded_identification",
        "identity"
      ],
      "properties": {
        "is_expanded_identification": {
          "type": "boolean"
        },
        "identity": {
          "type": "object",
          "required": [
            "concat_id",
            "concat_id_type_code"
          ],
          "properties": {
            "concat_id": {
              "type": "integer",
                 "errorMessage": {
                      "type": "My custom messsage"
                                 }
            },
            "concat_id_type_code": {
              "type": "integer"
            }
          }
        }
      }
    },
    "study_year": {
      "type": "string",
      "pattern": "^\\d{4}$"
    }
  }
}

由于“contact_id”字段 R 字母,我希望它显示我的自定义消息, 相反,它显示 JSchema 默认消息“解析数字时遇到意外字符:R。”。

我尝试过“errorMessage”/“message”,但没有一个对我有用。 newtosoft 不支持 Json Schema Validations 吗?

jsonschema json-schema-validator
1个回答
0
投票

JSON 模式本身(规范)没有规定错误消息,因此由工具来决定如何支持它们。

我不确定 Newtonsoft,但我的 lib JsonSchema.Net 确实如此(基于 System.Text.Jsondocs)。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.