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中。

相关推荐
EdisonZhou5 天前
MAF快速入门(6)混合编排工作流
llm·aigc·agent·.net core
EdisonZhou11 天前
MAF快速入门(5)开发自定义Executor
llm·aigc·agent·.net core
时光追逐者12 天前
Visual Studio 2026 正式版下载与安装详细教程!
ide·c#·.net·.net core·visual studio
EdisonZhou13 天前
MAF快速入门(4)多Agent工作流编排
llm·aigc·agent·.net core
时光追逐者14 天前
C# 中 ?、??、??=、?: 、?. 、?[] 各种问号的用法和说明
开发语言·c#·.net·.net core
时光追逐者15 天前
分享5款.NET开源免费的Redis客户端组件库
数据库·redis·开源·c#·.net·.net core
EdisonZhou18 天前
MAF快速入门(3)聊天记录持久化到数据库
llm·aigc·agent·.net core
EdisonZhou21 天前
MAF快速入门(2)Agent的花样玩法
llm·aigc·agent·.net core
hez201022 天前
TypedSql:在 C# 类型系统上实现一个 SQL 查询引擎
c#·.net·.net core·compiler
EdisonZhou1 个月前
MAF快速入门(1)化繁为简的Agent创建范式
llm·aigc·agent·.net core