0

我似乎无法让 const 或 enum 作为 if-then-else JSON 模式验证的一部分工作。

当涉及 1 个验证值时,它们似乎可以互换。(参考

这是我的 JSON 模式

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "title": "Test_Schema",
      "description": "A schema for validating a test object",
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "GeneralData": {
          "type": "object",
          "description": "Advsor and admin customer information",
          "properties": {
            "Name": {
              "type": [ "string", "null" ],
              "description": "Customer's advisor name"
            },
            "Age": {
              "type": [ "string", "null" ],
              "description": "Customer's advisor email"
            },
            "Location": {
              "type": [ "string", "null" ],
              "description": "The advisor's manager email'"
            }
          },
          "required": [ "Name", "Location", "Age" ]
        },
        "ClientData": {
          "type": "object",
          "description": "Customer's information",
          "additionalProperties": false,
          "properties": {
            "Title": {
              "type": [ "string", "null" ]
            },
            "Forename": {
              "type": [ "string", "null" ]
            },
            "Surname": {
              "type": [ "string" ]
            }
          },
          "required": [ "Title" ],
          "if": {
            "properties": {
              "Forename": { "enum": [ "Soameonea" ] }
            },
            "required": [ "Forename" ]
          },
          "then": { "required": [ "Surname" ] },
          "else": false 
        }
      }
    }

如果 Forename = "Someone" 我希望姓氏是必需的。

这是我序列化后的jsonObject:

     {
        "GeneralData": {
            "Name": "Not Relevant",
            "Age": "Not Relevant",
            "Location": "Not Relevant"
        },
        "ClientData": {
            "Title": "SomeTitle",
            "Forename": "Someone",
            "Surname": null
        }
    }

验证码:

     internal void ValidateDataObjectOnSchema()
            {
                var dataObject = PopulateDataObject();
                var json = JsonConvert.SerializeObject(dataObject);
                var result = GetSchema().Validate(json);
                var errors = result.Select(x =>
                {
                    return new Errors() { Title = x.Property, Description = x.Schema.Description };
                });
                var i = errors;
    
            }

      internal JsonSchema GetSchema()
            {
                return JsonSchema.FromFileAsync("Test/testSchema.json").Result;
            }

现在它仍然需要姓氏,即使枚举 Soameonea != 某人和我只需要标题。<== 问题 1

在 JSON 模式中,如果我设置 "Surname":{"type":["string","null]} 然后错误消失,如果我将 Forename 枚举的 if 条件更改为“某人”。<== 第 2 期

如果我用 Const 替换 Enum,我将无法获得不同的验证输出,因此,如果我能让一个工作,我确信另一个会跟随。

我找到了这个问题的几个答案(答案 1),我尝试实现同样的事情,但由于某种原因,它在我的情况下失败了。

我错过了什么?

4

2 回答 2

1

更新:正如@Relequestial 在其中一条评论中提到的,NJson Schema 似乎只支持 Draft 04,这就是 if-then-else 不起作用的原因,而应该使用暗示。以下是我使用替代包的解决方案。

相同的代码和 json 与包 Newtonsoft.Json.Schema 一起工作(参考

代码:

  internal void ValidateDataObjectOnSchemaWithNewtonSoftJson()
        {
            var dataObject = PopulateDataObject();
            var settings = new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore

            };
            var jsonDataObjectString = JsonConvert.SerializeObject(dataObject, settings);
            JObject jsonDataObject = JObject.Parse(jsonDataObjectString);
            var jsonSchemaFile = File.ReadAllText("Test/testSchema.json");
            var schema = JSchema.Parse(jsonSchemaFile);
            ;
            IList<ValidationError> messages;
            var result = jsonDataObject.IsValid(schema, out messages);
            var errors = GetReadableResult(result, messages);

        }

        private List<Errors> GetReadableResult(bool result, IList<ValidationError> messages)
        {
            var errors = new List<Errors>();

            if (!result)
            {
                foreach (var error in messages)
                {
                    if (error.ChildErrors.Count > 0)
                    {
                        errors.Add(
                            new Errors()
                            {
                                Path = error.ChildErrors.FirstOrDefault()?.Path,
                                Kind = error.ChildErrors.FirstOrDefault()?.Message
                            });
                    }
                    else
                    {
                        errors.Add(new Errors()
                        {
                            Path = error.Path,
                            Kind = error.Message
                        });
                    }
                }
            }

            return errors;
        }

JsonSchema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Test_Schema",
  "description": "A schema for validating a test object",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "GeneralData": {
      "type": "object",
      "description": "Advsor and admin customer information",
      "properties": {
        "Name": {
          "type": [ "string", "null" ],
          "description": "Customer's advisor name"
        },
        "Age": {
          "type": [ "string", "null" ],
          "description": "Customer's advisor email"
        },
        "Location": {
          "type": [ "string", "null" ],
          "description": "The advisor's manager email'"
        }
      },
      "required": [ "Name", "Location", "Age" ]
    },
    "ClientData": {
      "type": "object",
      "description": "Customer's information",
      "additionalProperties": false,
      "properties": {
        "Title": {
          "type": [ "string", "null" ]
        },
        "Forename": {
          "type": "string"
        },
        "Surname": {
          "type": [ "string" ]
        }
      },
      "required": [ "Title" ],
      "if": {
        "properties": {
          "Forename": { "enum": [ "Someone" ] }
        },
        "required": [ "Forename" ]
      },
      "then": { "required": [ "Surname" ] },
      "else": {}
    }
  }
}

唯一的补充是使 GetReadableResult 对于具有多个子项的错误递归。

于 2022-02-12T14:17:07.840 回答
1

在 JSON 模式中,如果我设置 "Surname":{"type":["string","null]} 那么错误就会消失

具有空值的属性仍然具有值。如果你想说属性值不仅必须存在,而且不能为空,那么添加"type": "string"到你的then条件。

于 2022-02-13T22:29:13.357 回答