【问题标题】:Newtonsoft JSON Schema - $ref is resolved, ignoring required thoughNewtonsoft JSON Schema - $ref 已解决,但忽略必需
【发布时间】:2019-12-01 12:02:31
【问题描述】:

我将一部分 json 架构外部化到一个单独的架构文件中。 例如:"$ref": "http://schema.company.com/boroughschema.json" 在此架构中,我指定了必需的属性,当验证已知的错误 json 文件时,它不会抱怨缺少必需的属性。

"required": [
        "Name",
        "Representative",
        "District"
]

我特意在源json中去掉了“District”,验证的时候没有投诉。

使用 Newtonsoft.Json.Schema 3.0.11。 如果我将架构部分移动到也可以正常工作的定义,则原始架构验证得很好。

private bool ValidateViaExternalReferences(string jsonstring)
{
    JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();

    // load schema
    var schemaText = System.IO.File.ReadAllText(COMPLEXSCHEMAFILE);

    // Rather than rely on 100% http access, use a resolver and
    // preload schema with http://schema.company.com/boroughschema.json
    var schemaTextBorough = System.IO.File.ReadAllText(BOROUGHSCHEMAFILE);
    resolver.Add(new Uri("http://schema.company.com/boroughschema.json"), schemaTextBorough);

    JSchema schema = JSchema.Parse(schemaText, resolver);
    JToken json = JToken.Parse(jsonstring);

    // validate json
    IList<ValidationError> errors;
    bool valid = json.IsValid(schema, out errors);
    if (!valid)
    {
    foreach (var validationerr in errors)
    {
        Append2Log(validationerr.ToString());
    }
    }
    return valid;
}

缺少“区域”不会产生错误,我希望使用原始架构时会出现相同的正确行为。

【问题讨论】:

    标签: json json.net schema


    【解决方案1】:

    我放置了: “必需的”: [ “姓名”, “代表”, “区” ] 片段在错误的位置。

    抱歉,它的行为符合预期。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2015-03-26
      相关资源
      最近更新 更多