[英]How to create a custom validation attribute with parameters consisting of model properties
[英]How can I create a custom JSchemaGenerationProvider that adds a title attribute to both the model and to the model's properties?
我正在使用 Newtonsoft.Json.Schema package 生成 JSON 模式。 目前,模式不包含“標題”屬性,因此我按照文檔中的示例創建了一個自定義提供程序,但是該提供程序僅在父節點上運行並跳過所有屬性節點。
class User {
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedDate { get; set; }
}
class TitleProvider : JSchemaGenerationProvider {
public override JSchema GetSchema(JSchemaTypeGenerationContext context) {
var schema = new JSchemaGenerator().Generate(context.ObjectType);
schema.Title = "foo";
return schema;
}
}
public class Program {
public static void Main() {
var generator = new JSchemaGenerator();
generator.GenerationProviders.Add(new TitleProvider());
var schema = generator.Generate(typeof(User));
Console.WriteLine(schema);
}
}
// OUTPUT:
//{
// "title": "foo",
// "type": "object",
// "properties": {
// "Id": {
// "type": "integer"
// },
// "Name": {
// "type": [
// "string",
// "null"
// ]
// },
// "CreatedDate": {
// "type": "string"
// }
// },
// "required": [
// "Id",
// "Name",
// "CreatedDate"
// ]
//}
如何配置此提供程序以在屬性節點上運行(類似於鏈接文檔中提供的示例)?
其他注意事項:
GetSchema
方法返回 null,它會遍歷所有屬性(我在調試器中觀察到),盡管它沒有我想要的功能所以我最終下載了代碼並單步執行它,發現一旦你的提供者返回一個模式,它就會繞過當前節點下所有節點的所有默認處理。 所以你必須自己做所有的解析,或者以某種方式解決默認行為。 我最終創建了一個提供程序,允許您在每個節點上執行邏輯,但仍然為您提供默認生成的模式:
abstract class RecursiveProvider : JSchemaGenerationProvider {
public string SkipType { get; set; }
public override JSchema GetSchema(JSchemaTypeGenerationContext context) {
var type = context.ObjectType;
JSchema schema = null;
var generator = new JSchemaGenerator();
Console.WriteLine(type.Name);
var isObject = type.Namespace != "System";
if (isObject) {
if (SkipType == type.Name)
return null;
this.SkipType = type.Name;
generator.GenerationProviders.Add(this);
}
schema = generator.Generate(type);
return ModifySchema(schema, context);
}
public abstract JSchema ModifySchema(JSchema schema, JSchemaTypeGenerationContext context);
}
class PropertyProvider : RecursiveProvider {
public override JSchema ModifySchema(JSchema schema, JSchemaTypeGenerationContext context) {
schema.Title = "My Title";
return schema;
}
}
最新版本的Json.NET Schema支持DisplayNameAttribute
和DescriptionAttribute
。 將它們放置在類型或屬性上將向生成的模式添加title
和description
屬性。
類型:
[DisplayName("Postal Address")]
[Description("The mailing address.")]
public class PostalAddress
{
[DisplayName("Street Address")]
[Description("The street address. For example, 1600 Amphitheatre Pkwy.")]
public string StreetAddress { get; set; }
[DisplayName("Locality")]
[Description("The locality. For example, Mountain View.")]
public string AddressLocality { get; set; }
[DisplayName("Region")]
[Description("The region. For example, CA.")]
public string AddressRegion { get; set; }
[DisplayName("Country")]
[Description("The country. For example, USA. You can also provide the two letter ISO 3166-1 alpha-2 country code.")]
public string AddressCountry { get; set; }
[DisplayName("Postal Code")]
[Description("The postal code. For example, 94043.")]
public string PostalCode { get; set; }
}
碼:
JSchemaGenerator generator = new JSchemaGenerator();
generator.DefaultRequired = Required.DisallowNull;
JSchema schema = generator.Generate(typeof(PostalAddress));
// {
// "title": "Postal Address",
// "description": "The mailing address.",
// "type": "object",
// "properties": {
// "StreetAddress": {
// "title": "Street Address",
// "description": "The street address. For example, 1600 Amphitheatre Pkwy.",
// "type": "string"
// },
// "AddressLocality": {
// "title": "Locality",
// "description": "The locality. For example, Mountain View.",
// "type": "string"
// },
// "AddressRegion": {
// "title": "Region",
// "description": "The region. For example, CA.",
// "type": "string"
// },
// "AddressCountry": {
// "title": "Country",
// "description": "The country. For example, USA. You can also provide the two-letter ISO 3166-1 alpha-2 country code.",
// "type": "string"
// },
// "PostalCode": {
// "title": "Postal Code",
// "description": "The postal code. For example, 94043.",
// "type": "string"
// }
// }
// }
https://www.newtonsoft.com/jsonschema/help/html/GenerateWithDescriptions.htm
做這樣的事情:
class FormatSchemaProvider : JSchemaGenerationProvider
{
public override JSchema GetSchema(JSchemaTypeGenerationContext context)
{
if (context.ObjectType == typeof(Sprite) || context.ObjectType == typeof(AudioClip) || context.ObjectType == typeof(GameObject) || context.ObjectType == typeof(Vector3))
{
JSchema schema = new JSchema
{
Title = context.SchemaTitle,
Description = context.SchemaDescription,
Type = JSchemaType.String
};
return schema;
}
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.