.NET下 支持大小写不敏感的JSON Schema验证方法

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

问题

有很多应用程序在验证JSON数据的时候用到了JSON Schema。

在微服务架构下,有时候各个微服务由于各种历史原因,它们所生成的数据对JSON Object属性名的大小写规则可能并不统一,它们需要消费的JSON数据的属性名可能需要大小写无关。

遗憾的是,目前的JSON Schema没有这方面的标准,标准中都是大小写敏感的。在类似上述情况下,这给使用JSON Schema进行数据验证造成了困难。

解决方案

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

比如你的数据是这样的:

[
  { "Count": 1 },
  { "count": 3 }
]

那么你可以这样写JSON Schema:

{
  "type": "array",
  "items": {
    "patternProperties": {
      "^[Cc]ount$": { "minimum": 1 }
    }
  }
}

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

更优雅的解决方案

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

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

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

例子:

string jsonSchema = """
    {
      "properties": {
        "A": {
                "properties": { 
                  "B": {"type": "string"} 
                }
        }
      }
    }
    """;
string jsonInstance = """
    {
      "a": {
        "b": 123
      }
    }
    """;
// By default, JSON Schema validation is property names case sensitive, so instance data's property names are not matched:
ValidationResult validationResult = new JsonValidator(jsonSchema).Validate(jsonInstance);
Assert.True(validationResult.IsValid);
// Opt in to feature of property names case Insensitive:
validationResult = new JsonValidator(jsonSchema, new JsonValidatorOptions { PropertyNameCaseInsensitive = true }).Validate(jsonInstance);
Assert.False(validationResult.IsValid);
Assert.Equal("Expect type(s): 'String' but actual is 'Number'", validationResult.ErrorMessage);
Assert.Equal("/a/b", validationResult.InstanceLocation!.ToString());
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

相关文章
|
2月前
|
SQL 缓存 开发框架
分享一个 .NET EF6 应用二级缓存提高性能的方法
分享一个 .NET EF6 应用二级缓存提高性能的方法
|
2月前
|
JSON 开发框架 JavaScript
【Azure Developer】使用.Net Core解析JSON的笔记
【Azure Developer】使用.Net Core解析JSON的笔记
|
2月前
|
JSON Java API
【Azure Developer】如何验证 Azure AD的JWT Token (JSON Web 令牌)?
【Azure Developer】如何验证 Azure AD的JWT Token (JSON Web 令牌)?
|
2月前
|
程序员 数据库
分享 2 个 .NET EF 6 只更新某些字段的方法
分享 2 个 .NET EF 6 只更新某些字段的方法
|
2月前
|
数据库
分享一个 .NET EF 6 扩展 Where 的方法
分享一个 .NET EF 6 扩展 Where 的方法
|
2月前
|
开发框架 前端开发 算法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
分享 .NET EF6 查询并返回树形结构数据的 2 个思路和具体实现方法
|
2月前
|
开发框架 中间件 .NET
分享 ASP.NET Core Web Api 中间件获取 Request Body 两个方法
分享 ASP.NET Core Web Api 中间件获取 Request Body 两个方法
|
2月前
|
开发框架 .NET API
如何在 ASP.NET Core Web API 方法执行前后 “偷偷“ 作一些 “坏“ 事?初识 ActionFilterAttribute
如何在 ASP.NET Core Web API 方法执行前后 “偷偷“ 作一些 “坏“ 事?初识 ActionFilterAttribute
|
2月前
|
开发框架 前端开发 .NET
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
Asp.net Webapi 的 Post 方法不能把参数加到 URL 中?试试这样写
|
3月前
|
算法 API 数据安全/隐私保护
.NET使用原生方法实现文件压缩和解压
.NET使用原生方法实现文件压缩和解压
.NET使用原生方法实现文件压缩和解压
下一篇
无影云桌面