- A+
LateApexEarlySpeed.Json.Schema - Json schema validator generation from code
除了用户手动传入标准的json schema来生成validator以外,LateApexEarlySpeed.Json.Schema 实现库也支持直接从用户代码中生成json schema validator.
基本用法
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
目前为止支持的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用法:
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:
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:
internal class YourNamingPolicy : JsonSchemaNamingPolicy { public override string ConvertName(string name) { // convert and return new name. } }
用option来自定义property naming policy:
JsonValidator validator = JsonSchemaGenerator.GenerateJsonValidator(type, new JsonSchemaGeneratorOptions { PropertyNamingPolicy = JsonSchemaNamingPolicy.CamelCase }));
Note: 当对某类属性指定了 JsonPropertyNameAttribute,同时又在option中指定了自定义 PropertyNamingPolicy, 则那个属性将使用JsonPropertyNameAttribute,其他属性将使用option.
Issue report
使用中遇到任何问题,或者希望增加的功能,欢迎提到doc repo issue中。