@@ -6,8 +6,8 @@ status](https://github.com/pekspro/DataAnnotationValuesExtractor/actions/workflo
66
77A C# source generator that automatically extracts values from data annotation
88attributes and exposes them as strongly-typed constants. Access your
9- ` StringLength ` , ` Range ` , ` Required ` , ` Display ` and ` Description ` attribute
10- values as constants in your classes.
9+ ` StringLength ` ,` MinLength ` , ` MaxLength ` , ` Range ` , ` Required ` , ` Display ` and
10+ ` Description ` attribute values as constants in your classes.
1111
1212## Why Use This?
1313
@@ -29,6 +29,10 @@ public partial class Product
2929 [Required ]
3030 [Range (0 . 01 , 999999 . 99 )]
3131 public decimal Price { get ; set ; }
32+
33+ [MinLength (1 )]
34+ [MaxLength (50 )]
35+ public string []? Tags { get ; set ; }
3236}
3337```
3438
@@ -42,6 +46,10 @@ bool nameRequired = Product.Annotations.Name.IsRequired; // true
4246// Price constraints
4347double minPrice = Product .Annotations .Price .Minimum ; // 0.01
4448double maxPrice = Product .Annotations .Price .Maximum ; // 999999.99
49+
50+ // Tags length constraints
51+ int tagsMinLength = Product .Annotations .Tags .MinLength ; // 1
52+ int tagsMaxLength = Product .Annotations .Tags .MaxLength ; // 50
4553```
4654
4755## Usage Patterns
@@ -55,7 +63,7 @@ Apply `[DataAnnotationValues]` directly to each class you want to generate
5563constants for:
5664
5765``` csharp
58- [DataAnnotationValues (StringLength = true , Range = true , Required = true , Display = true , Description = true )]
66+ [DataAnnotationValues (StringLength = true , MinLength = true , MaxLength = true , Range = true , Required = true , Display = true , Description = true )]
5967public partial class Product
6068{
6169 [Display (Name = " Product name" )]
@@ -70,6 +78,10 @@ public partial class Product
7078 [Range (0 . 01 , 999999 . 99 )]
7179 public decimal Price { get ; set ; }
7280
81+ [MinLength (1 )]
82+ [MaxLength (50 )]
83+ public string []? Tags { get ; set ; }
84+
7385 public string ? Sku { get ; set ; }
7486}
7587```
@@ -83,7 +95,7 @@ each class you want to generate constants for. You can use the
8395``` csharp
8496using Pekspro .DataAnnotationValuesExtractor ;
8597
86- [DataAnnotationValuesConfiguration (StringLength = true , Range = true , Required = true , Display = true , Description = true )]
98+ [DataAnnotationValuesConfiguration (StringLength = true , Range = true , MinLength = true , MaxLength = true , Required = true , Display = true , Description = true )]
8799[DataAnnotationValuesToGenerate (typeof (Customer ))]
88100[DataAnnotationValuesToGenerate (typeof (Order ))]
89101[DataAnnotationValuesToGenerate (typeof (Product ))]
@@ -126,6 +138,10 @@ bool priceRequired = Product.Annotations.Price.IsRequired;
126138string ? priceDisplayName = Product .Annotations .Price .Display .Name ; // Price name
127139string ? priceDescription = Product .Annotations .Price .Description .Text ; // Price description
128140
141+ // Tags
142+ int tagsMinLength = Product .Annotations .Tags .MinLength ; // 1
143+ int tagsMaxLength = Product .Annotations .Tags .MaxLength ; // 50
144+
129145// Sku
130146bool skuRequired = Product .Annotations .Sku .IsRequired ; // false
131147```
@@ -163,6 +179,8 @@ the following properties to control which constants are generated:
163179| Property | Default | Generated Constants | Description |
164180| -------------- | ------- | ---------------------------------------------------------------- | ----------------------------------------------- |
165181| ` StringLength ` | ` true ` | ` MaximumLength ` , ` MinimumLength ` | Extract values from ` [StringLength] ` attribute. |
182+ | ` MinLength ` | ` false ` | ` MinLength ` | Extract value from ` [MinLength] ` attribute. |
183+ | ` MaxLength ` | ` false ` | ` MaxLength ` | Extract value from ` [MaxLength] ` attribute. |
166184| ` Range ` | ` true ` | ` Minimum ` , ` Maximum ` , ` MinimumIsExclusive ` , ` MaximumIsExclusive ` | Extract values from ` [Range] ` attribute. |
167185| ` Required ` | ` false ` | ` IsRequired ` | Detect presence of ` [Required] ` attribute. |
168186| ` Display ` | ` false ` | ` Name ` , ` Description ` , ` ShortName ` | Extract values from ` [Display] ` attribute. |
@@ -198,7 +216,7 @@ directory.
198216Given this input:
199217
200218``` csharp
201- [DataAnnotationValues (StringLength = true , Range = true , Required = true , Display = true , Description = true )]
219+ [DataAnnotationValues (StringLength = true , MinLength = true , MaxLength = true , Range = true , Required = true , Display = true , Description = true )]
202220public partial class Player
203221{
204222 [Display (Name = " Player name" , ShortName = " Name" , Description = " Name of player" )]
@@ -212,6 +230,10 @@ public partial class Player
212230
213231 [Range (1 , 100 )]
214232 public int Score { get ; set ; }
233+
234+ [MinLength (1 )]
235+ [MaxLength (10 )]
236+ public string []? Tags { get ; set ; }
215237}
216238```
217239
@@ -329,6 +351,27 @@ public partial class Player
329351 /// </summary >
330352 public const bool IsRequired = false ;
331353 }
354+
355+ /// <summary >
356+ /// Data annotation values for Tags.
357+ /// </summary >
358+ public static class Tags
359+ {
360+ /// <summary >
361+ /// Minimum length for Tags.
362+ /// </summary >
363+ public const int MinLength = 1 ;
364+
365+ /// <summary >
366+ /// Maximum length for Tags.
367+ /// </summary >
368+ public const int MaxLength = 10 ;
369+
370+ /// <summary >
371+ /// Indicates whether Tags is required.
372+ /// </summary >
373+ public const bool IsRequired = false ;
374+ }
332375 }
333376}
334377```
0 commit comments