用.NET代码生成JSON Schema 验证器

问题

对于验证复杂JSON数据是否合法的需求,通常的解决方式是标准JSON Schema,.Net下有对应的JSON Schema实现库。应用程序通常需要将标准JSON schema传入实现库,来做后续的数据验证。这里有一种情况,就是如果使用者不太了解标准JSON Schema格式,但又希望能在自己的service中使用其强大的功能,或者适配需要JSON Schema的其他service。

解决方案

如果不熟悉标准JSON Schema,可以用Lateapexearlyspeed.Json.Schema 实现库的 fluent schema builder模式 来创建 JSON Schema 验证器。

这种fluent schema builder用法的接口设计思路是不完全与标准json schema的格式和命名保持一致。标准JSON Schema虽然强大,但大多开发者一般更熟悉"强类型"风格,因此实现库的fluent schema builder在配置时会先"问"用户他们期望哪种JSON token类型,然后在后续链式调用时会基于当前限定的JSON token类型继续"追问"相关的验证需求。通过这种调用方式,开发者会写出更安全的验证代码,利用IDE也会有更友好的使用体验。如下:

csharp 复制代码
    var b = new JsonSchemaBuilder();
    b.ObjectHasProperty("A", b => b.IsJsonString().HasMinLength(5))
    .HasProperty("B", b => b.IsJsonNumber().IsGreaterThan(1).IsLessThan(10))
    .HasProperty("C", b => b.IsJsonArray().HasMinLength(5).HasItems(b =>
    {
        b.NotJsonNull();
    }))
    .HasProperty("D", b => b.Or(
        b => b.IsJsonFalse(),
        b => b.IsJsonNumber().Equal(0),
        b => b.IsJsonObject().HasCustomValidation((JsonElement element) => element.GetProperty("Prop").ValueKind == JsonValueKind.True, 
            jsonElement => $"Cannot pass my custom validation, data is {jsonElement}")
        )
    );
    JsonValidator jsonValidator = b.BuildValidator();
    jsonValidator.Validate(...);

看起来风格是不是很熟悉,即使没接触过JSON Schema,相信也能理解上面的.Net代码。

目前 fluent schema builder 模式下支持的验证方法:

  • NotJsonNull
  • IsJsonTrue
  • IsJsonFalse
  • IsJsonBoolean
  • IsJsonNull
  • IsJsonString:
    • Equal
    • IsIn
    • HasMaxLength
    • HasMinLength
    • HasPattern
    • HasCustomValidation
    • NotMatch
    • EndsWith
  • StringEqual
  • StringHasPattern
  • IsJsonNumber:
    • Equal
    • IsIn
    • IsGreaterThan
    • IsLessThan
    • NotGreaterThan
    • NotLessThan
    • MultipleOf
    • HasCustomValidation
  • IsJsonArray:
    • SerializationEquivalent
    • HasItems
    • HasLength
    • HasMaxLength
    • HasMinLength
    • HasUniqueItems
    • HasCustomValidation
    • Contains
    • NotContains
    • Equivalent
    • HasCollection
    • Empty
    • NotEmpty
    • Single
  • ArrayHasItems
  • ArrayContains
  • IsJsonObject:
    • SerializationEquivalent
    • HasProperty
    • HasCustomValidation
    • Equivalent
    • HasNoProperty
  • ObjectHasProperty
  • Or

其中还有HasCustomValidation() overloads 重载方法可以为 JSON树中的指定节点 创建更高级的自定义验证需求。

不仅想验证JSON数据,还想用 代码Build出来的 Json验证器 来生成 JSON Schema ?

也可以的

csharp 复制代码
string standardJsonSchema = jsonValidator.GetStandardJsonSchemaText();

注意:

为了支持更高级和友好的验证体验,虽然 Lateapexearlyspeed.Json.Schema 实现库在其内部实现时尽可能使用标准JSON Schema keywords, 但一些Build方法会用到"扩展"keywords,因此当你用了那些Build()生成了 JsonValidator 实例后, 将不支持调用 GetStandardJsonSchemaText(),因为扩展keywords无法放回标准JSON Schema且被其他application 所认识,如下:

csharp 复制代码
var builder = new JsonSchemaBuilder();
builder.IsJsonNumber().HasCustomValidation((double _) => true, _ => "");
JsonValidator jsonValidator = builder.BuildValidator();

Assert.Throws<NotSupportedException>(() => jsonValidator.GetStandardJsonSchemaText());

牵扯到扩展keywords的Build方法有:

  • HasCustomValidation(...)
  • HasNoProperty()
  • NotContains()
  • StartsWith()
  • EndsWith()

总结

对于验证JSON数据方面的复杂需求,可以用JSON Schema解决。

对于不希望直接交互JSON Schema格式的service来说,可以用.Net下的 Lateapexearlyspeed.Json.Schema 实现库的 fluent schema builder模式,通过写代码的形式生成JSON验证器。

对于希望用强类型风格的代码生成JSON Schema的需求,也可以用 Lateapexearlyspeed.Json.Schema 实现库的 fluent schema builder模式。

Github repo: https://github.com/lateapexearlyspeed/Lateapexearlyspeed.JsonSchema, 欢迎将使用时发现的问题提到issue。

相关推荐
步步为营DotNet20 小时前
深入.NET 11:C# 14 在边缘计算数据处理的优化与实践
c#·.net·边缘计算
步步为营DotNet2 天前
洞悉.NET 11:Blazor 与 Microsoft.Extensions.AI 的融合创新实践
人工智能·microsoft·.net
回忆2012初秋2 天前
.NET 8.0 实战:基于 MQTTnet 封装高可用的 MQTT 发布/订阅工具类
开发语言·mqtt·.net
回忆2012初秋2 天前
.NET 8.0 工业物联网实战:基于 S7netplus 封装高可用西门子 PLC 通信类
物联网·.net
学以智用3 天前
.NET Core 仓储模式(Repository Pattern)完整教程
后端·.net
.NET修仙日记3 天前
.NET EFCore批量插入性能优化实战:30秒 → 0.5秒
性能优化·c#·.net·.netcore·微软技术·efcore·踩坑实录
步步为营DotNet3 天前
深挖.NET 11:.NET Aspire 在云原生应用状态管理的创新与实践
云原生·.net·wpf
我是唐青枫3 天前
C#.NET YARP 跨域配置详解:网关统一处理 CORS
开发语言·c#·.net
唐青枫4 天前
C#.NET YARP 跨域配置详解:网关统一处理 CORS
c#·.net
rockey6274 天前
AScript如何实现LINQ语法
sql·c#·.net·linq·script·eval·expression