经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » XML相关 » Schema » 查看文章
.NET下 支持大小写不敏感的JSON Schema验证方法
来源:cnblogs  作者:dotnet程序故障诊断  时间:2024/6/26 8:55:02  对本文有异议

问题

有很多应用程序在验证JSON数据的时候用到了JSON Schema。
在微服务架构下,有时候各个微服务由于各种历史原因,它们所生成的数据对JSON Object属性名的大小写规则可能并不统一,它们需要消费的JSON数据的属性名可能需要大小写无关。
遗憾的是,目前的JSON Schema没有这方面的标准,标准中都是大小写敏感的。在类似上述情况下,这给使用JSON Schema进行数据验证造成了困难。

解决方案

一种 临时解决方案 是利用JSON Schema中的patternProperties关键字,写正则表达式来表示当前属性名是大小写无关的。
比如你的数据是这样的:

  1. [
  2. { "Count": 1 },
  3. { "count": 3 }
  4. ]

那么你可以这样写JSON Schema:

  1. {
  2. "type": "array",
  3. "items": {
  4. "patternProperties": {
  5. "^[Cc]ount$": { "minimum": 1 }
  6. }
  7. }
  8. }

显然这样的JSON Schema会比原来的更复杂。

更优雅的解决方案

想象一下.NET下的JSON library System.Text.Json,它的反序列化器支持 属性名大小写无关的选项PropertyNameCaseInsensitive,这个是用来反序列化的。

那么,有没有JSON Schema实现库支持大小写无关的扩展选项呢?在.NET下,目前 实现库 Lateapexearlyspeed.Json.Schema支持 属性名大小写无关的 JSON Schema级验证。由于该实现库遵循标准JSON Schema(规定属性名只能大小写敏感),所以这个扩展功能需要显式配置:

  1. /// <summary>
  2. /// Gets or sets a value that determines whether a property's name uses a case-insensitive comparison during validation. The default value is false.
  3. /// </summary>
  4. /// <returns>
  5. /// true to compare property names using case-insensitive comparison; otherwise, false.
  6. /// </returns>
  7. public bool PropertyNameCaseInsensitive { get; set; }

例子:

  1. string jsonSchema = """
  2. {
  3. "properties": {
  4. "A": {
  5. "properties": {
  6. "B": {"type": "string"}
  7. }
  8. }
  9. }
  10. }
  11. """;
  12. string jsonInstance = """
  13. {
  14. "a": {
  15. "b": 123
  16. }
  17. }
  18. """;
  19. // By default, JSON Schema validation is property names case sensitive, so instance data's property names are not matched:
  20. ValidationResult validationResult = new JsonValidator(jsonSchema).Validate(jsonInstance);
  21. Assert.True(validationResult.IsValid);
  22. // Opt in to feature of property names case Insensitive:
  23. validationResult = new JsonValidator(jsonSchema, new JsonValidatorOptions { PropertyNameCaseInsensitive = true }).Validate(jsonInstance);
  24. Assert.False(validationResult.IsValid);
  25. Assert.Equal("Expect type(s): 'String' but actual is 'Number'", validationResult.ErrorMessage);
  26. Assert.Equal("/a/b", validationResult.InstanceLocation!.ToString());
  27. Assert.Equal("/properties/A/properties/B/type", validationResult.RelativeKeywordLocation!.ToString());

总结

本文介绍了.NET下 实现属性名大小写无关的JSON Schema验证方法,其中最优雅的方式应该是用 .NET实现库 Lateapexearlyspeed.Json.Schema中的扩展选项 PropertyNameCaseInsensitive

欢迎大家将使用过程中发现的问题报到repo issue,希望.NET实现库 Lateapexearlyspeed.Json.Schema 能帮到大家。

Github repo: https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema

原文链接:https://www.cnblogs.com/dotnet-diagnostic/p/18261226

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号