【问题标题】:JSON schema validation for enum shows same error message twice枚举的 JSON 模式验证两次显示相同的错误消息
【发布时间】:2013-06-21 04:20:18
【问题描述】:

我正在提交这个 JSON 字符串:

{
    "companyName" : "Goog"
}

架构是:

{
    "companyName": { 
        "type":[ "string", "null" ], 
        "description": "Companies list", 
        "required":false ,
        "maxLength": 256,
        "enum": [ "Google", "Apple" , null ]
    }
}

我使用 Newtonsoft.Json 验证器函数 IsValid

JObject jobj= _jsonParser.ToJSonObject(jsonString);
IList<string> errMessages;
var valid = jobj.IsValid(jsonSchema, out errMessages);

验证时,我收到相同的错误消息重复两次:

枚举中未定义值“Goog”。第 34 行, 位置 35., 枚举中未定义值“Goog”。第 34 行, 位置 35。

我在做什么有问题吗?或者这是 JSON 验证器的问题?

【问题讨论】:

  • 你能告诉我们你用来验证 json 模式的代码吗?
  • @JanR,我正在使用 Newtonsoft.Json 函数 IsValid。 Newtonsoft.Json.Schema.Extensions.IsValid(this JToken, JsonSchema, out IList)。这是我的代码: var entity = _jsonParser.ToObject(jsonEntity); IList errMessages; var valid = entity.IsValid(jsonSchema, out errMessages);
  • 向我们展示代码时,您应该通过编辑将其放入您的问题中。不要为此使用 cmets。
  • @JeffMercado ,我已经对问题进行了编辑。我正在使用 Newtonsoft.Json 模式验证器。
  • @JanR 用我正在使用的函数更新了问题。

标签: c# json.net jsonschema


【解决方案1】:

对于我收到的错误消息,我将其放在一个列表中并调用 distinct()

errMessages.Distinct().ToList()

【讨论】:

    猜你喜欢
    • 2014-04-19
    • 2012-09-12
    • 1970-01-01
    • 2017-10-05
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多