Json Schema高性能.net实现库 LateApexEarlySpeed.Json.Schema - 直接从code生成json schema validator

LateApexEarlySpeed.Json.Schema - Json schema validator generation from code

除了用户手动传入标准的json schema来生成validator以外,LateApexEarlySpeed.Json.Schema 实现库也支持直接从用户代码中生成json schema validator.

基本用法

csharp 复制代码
JsonValidator validator = JsonSchemaGenerator.GenerateJsonValidator<TestClass>();

// Now use validator instance as normal

目前为止支持的.net类型

Numeric types: byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal.

Boolean, String, Dictionary<string,TAny>, JsonElement, JsonDocument, JsonNode, JsonValue, JsonArray, JsonObject, IEnumerable , Enum, Guid, Uri, DateTimeOffset, DateTime, Nullable value type (Nullable ), Custom object.

目前为止支持的validation attributes

LateApexEarlySpeed.Json.Schema实现库支持通过attributes来表示需要的数据限制。查看各attributes的构造函数的参数应该就可以了解其作用:

  • EmailAttribute
  • ExclusiveMaximumAttribute
  • ExclusiveMinimumAttribute
  • MaximumAttribute
  • MinimumAttribute
  • MultipleOfAttribute
  • StringEnumAttribute
  • IntegerEnumAttribute
  • IPv4Attribute
  • IPv6Attribute
  • LengthRangeAttribute (for both string length and array length)
  • MaxLengthAttribute (for both string length and array length)
  • MinLengthAttribute (for both string length and array length)
  • UniqueItemsAttribute (for array)
  • NumberRangeAttribute
  • PatternAttribute (for string)

attribute用法:

csharp 复制代码
class TestClass
{
    [Maximum(2)]
    public int Prop { get; set; }

    [LengthRange(10, 20)]
    [Pattern("*abc*")]
    public string StringProp { get; set; }
}

Nullable

默认情况下,library会认为所有reference类型为可空。如果你希望指定某引用类型的属性不能为空,可以给这个属性加[LateApexEarlySpeed.Json.Schema.Generator.NotNullAttribute]

Required or ignored

默认情况下,当json数据中出现要验证的类属性时,library才会验证这个json中的属性的值。

如果你希望验证某属性必须出现在json数据中,可以给这个属性加[System.Text.Json.Serialization.JsonRequiredAttribute][System.ComponentModel.DataAnnotations.RequiredAttribute]

如果你希望library忽略对于某个属性的验证,可以给这个属性加[System.Text.Json.Serialization.JsonIgnoreAttribute]

library为这种需求设计为复用.net core默认自带的attribute,是为了尽可能让用户代码有一致体验。

自定义属性名

和System.Text.Json类似, library支持通过attribute和option来提供用户定义的属性名称:

System.Text.Json.Serialization.JsonPropertyNameAttribute:

csharp 复制代码
class CustomNamedPropertyTestClass
    {
        [JsonPropertyName("NewPropName")]
        public int Prop { get; set; }
    }

JsonSchemaNamingPolicy options:

  • JsonSchemaNamingPolicy.CamelCase:

    First word starts with a lower case character. Successive words start with an uppercase character. TempCelsius => tempCelsius

  • JsonSchemaNamingPolicy.KebabCaseLower: Words are separated by hyphens. All characters are lowercase. TempCelsius -> temp-celsius

  • JsonSchemaNamingPolicy.KebabCaseUpper: Words are separated by hyphens. All characters are uppercase. TempCelsius => TEMP-CELSIUS

  • JsonSchemaNamingPolicy.SnakeCaseLower: Words are separated by underscores. All characters are lowercase. TempCelsius -> temp_celsius

  • JsonSchemaNamingPolicy.SnakeCaseUpper: Words are separated by underscores. All characters are uppercase. TempCelsius -> TEMP_CELSIUS

  • JsonSchemaNamingPolicy.SharedDefault: default option, not change original property name

  • 也可以写自己需要的其他JsonSchemaNamingPolicy:

csharp 复制代码
internal class YourNamingPolicy : JsonSchemaNamingPolicy
{
    public override string ConvertName(string name)
    {
        // convert and return new name.
    }
}

用option来自定义property naming policy:

csharp 复制代码
JsonValidator validator = JsonSchemaGenerator.GenerateJsonValidator(type, new JsonSchemaGeneratorOptions { PropertyNamingPolicy = JsonSchemaNamingPolicy.CamelCase }));

Note: 当对某类属性指定了 JsonPropertyNameAttribute,同时又在option中指定了自定义 PropertyNamingPolicy, 则那个属性将使用JsonPropertyNameAttribute,其他属性将使用option.

Issue report

使用中遇到任何问题,或者希望增加的功能,欢迎提到doc repo issue中。

相关推荐
WikeSoft11 天前
2.3.net core 工作流WorkFlow流程(流程节点附件设置)
.net core·workflow·流程引擎·工作流
时光追逐者15 天前
一个开源的 Blazor 跨平台入门级实战项目
c#·asp.net·.net core·blazor
时光追逐者16 天前
一个.NET开源、轻量级的运行耗时统计库
开源·c#·.net·.net core
飞人博尔特的摄影师18 天前
C#开发利器:SharpBoxesCore全解析
开发语言·设计模式·系统架构·c#·.net·.net core
hez201018 天前
.NET 的全新低延时高吞吐自适应 GC - Satori GC
c#·.net·.net core·clr
CSharp精选营23 天前
ASP.NET Core EFCore 属性配置与DbContext 详解
.net core·efcore·dbcontext
WikeSoft1 个月前
.net core workflow流程定义
.net·.net core·workflow·流程引擎·工作流
江沉晚呤时1 个月前
深入了解 OpenIddict:实现 OAuth 2.0 和 OpenID Connect 协议的 .NET 库
后端·c#·.net·.net core
宝桥南山2 个月前
Model Context Protocol (MCP) - 尝试创建和测试一下MCP Server
microsoft·ai·微软·c#·.net·.net core
代码拾光2 个月前
面试官:如果某个业务量突然提升100倍QPS你会怎么做?
.net core·架构设计