Skip to content

Commit 5e76223

Browse files
committed
Adds support for Min and MaxLength attributes.
1 parent 658453a commit 5e76223

19 files changed

+1037
-30
lines changed

README-NuGet.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
A C# source generator that automatically extracts values from data annotation
44
attributes and exposes them as strongly-typed constants. Access your
5-
`StringLength`, `Range`, `Required`, `Display` and `Description` attribute
6-
values as constants in your classes.
5+
`StringLength`,`MinLength`, `MaxLength`, `Range`, `Required`, `Display` and
6+
`Description` attribute values as constants in your classes.
77

88
## Why Use This?
99

@@ -25,6 +25,10 @@ public partial class Product
2525
[Required]
2626
[Range(0.01, 999999.99)]
2727
public decimal Price { get; set; }
28+
29+
[MinLength(1)]
30+
[MaxLength(50)]
31+
public string[]? Tags { get; set; }
2832
}
2933
```
3034

@@ -38,6 +42,10 @@ bool nameRequired = Product.Annotations.Name.IsRequired; // true
3842
// Price constraints
3943
double minPrice = Product.Annotations.Price.Minimum; // 0.01
4044
double maxPrice = Product.Annotations.Price.Maximum; // 999999.99
45+
46+
// Tags length constraints
47+
int tagsMinLength = Product.Annotations.Tags.MinLength; // 1
48+
int tagsMaxLength = Product.Annotations.Tags.MaxLength; // 50
4149
```
4250

4351
## Usage Patterns
@@ -51,7 +59,7 @@ Apply `[DataAnnotationValues]` directly to each class you want to generate
5159
constants for:
5260

5361
```csharp
54-
[DataAnnotationValues(StringLength = true, Range = true, Required = true, Display = true, Description = true)]
62+
[DataAnnotationValues(StringLength = true, MinLength = true, MaxLength = true, Range = true, Required = true, Display = true, Description = true)]
5563
public partial class Product
5664
{
5765
[Display(Name = "Product name")]
@@ -66,6 +74,10 @@ public partial class Product
6674
[Range(0.01, 999999.99)]
6775
public decimal Price { get; set; }
6876

77+
[MinLength(1)]
78+
[MaxLength(50)]
79+
public string[]? Tags { get; set; }
80+
6981
public string? Sku { get; set; }
7082
}
7183
```
@@ -79,7 +91,7 @@ each class you want to generate constants for. You can use the
7991
```csharp
8092
using Pekspro.DataAnnotationValuesExtractor;
8193

82-
[DataAnnotationValuesConfiguration(StringLength = true, Range = true, Required = true, Display = true, Description = true)]
94+
[DataAnnotationValuesConfiguration(StringLength = true, Range = true, MinLength = true, MaxLength = true, Required = true, Display = true, Description = true)]
8395
[DataAnnotationValuesToGenerate(typeof(Customer))]
8496
[DataAnnotationValuesToGenerate(typeof(Order))]
8597
[DataAnnotationValuesToGenerate(typeof(Product))]
@@ -122,6 +134,10 @@ bool priceRequired = Product.Annotations.Price.IsRequired;
122134
string? priceDisplayName = Product.Annotations.Price.Display.Name; // Price name
123135
string? priceDescription = Product.Annotations.Price.Description.Text; // Price description
124136
137+
// Tags
138+
int tagsMinLength = Product.Annotations.Tags.MinLength; // 1
139+
int tagsMaxLength = Product.Annotations.Tags.MaxLength; // 50
140+
125141
// Sku
126142
bool skuRequired = Product.Annotations.Sku.IsRequired; // false
127143
```
@@ -144,6 +160,7 @@ it from your output assembly:
144160
</ItemGroup>
145161
```
146162

163+
147164
## Links
148165

149166
You can find more information and can report issues on [GitHub](https://github.com/pekspro/DataAnnotationValuesExtractor).

README.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ status](https://github.com/pekspro/DataAnnotationValuesExtractor/actions/workflo
66

77
A C# source generator that automatically extracts values from data annotation
88
attributes 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
4347
double minPrice = Product.Annotations.Price.Minimum; // 0.01
4448
double 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
5563
constants 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)]
5967
public 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
8496
using 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;
126138
string? priceDisplayName = Product.Annotations.Price.Display.Name; // Price name
127139
string? 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
130146
bool 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.
198216
Given 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)]
202220
public 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
```

Source/Library/Pekspro.DataAnnotationValuesExtractor.Attributes/DataAnnotationValuesAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public class DataAnnotationValuesAttribute : System.Attribute
3636
/// Is set to false by default.
3737
/// </summary>
3838
public bool Description { get; set; }
39+
40+
/// <summary>
41+
/// Whether MaxLength attribute values should be included.
42+
/// Is set to false by default.
43+
/// </summary>
44+
public bool MaxLength { get; set; }
45+
46+
/// <summary>
47+
/// Whether MinLength attribute values should be included.
48+
/// Is set to false by default.
49+
/// </summary>
50+
public bool MinLength { get; set; }
3951
}
4052
}
4153

Source/Library/Pekspro.DataAnnotationValuesExtractor.Attributes/DataAnnotationValuesOptionsAttribute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public class DataAnnotationValuesOptionsAttribute : System.Attribute
3838
/// Is set to false by default.
3939
/// </summary>
4040
public bool Description { get; set; }
41+
42+
/// <summary>
43+
/// Whether MaxLength attribute values should be included.
44+
/// Is set to false by default.
45+
/// </summary>
46+
public bool MaxLength { get; set; }
47+
48+
/// <summary>
49+
/// Whether MinLength attribute values should be included.
50+
/// Is set to false by default.
51+
/// </summary>
52+
public bool MinLength { get; set; }
4153
}
4254
}
4355

Source/Library/Pekspro.DataAnnotationValuesExtractor/DataAnnotationValuesDetailedOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace Pekspro.DataAnnotationValuesExtractor;
1616
public readonly bool AddRequired;
1717
public readonly bool AddDisplay;
1818
public readonly bool AddDescription;
19+
public readonly bool AddMaxLength;
20+
public readonly bool AddMinLength;
1921

2022
public DataAnnotationValuesDetailedOptions(
2123
string name,
@@ -26,7 +28,9 @@ public DataAnnotationValuesDetailedOptions(
2628
bool addRange,
2729
bool addRequired,
2830
bool addDisplay = false,
29-
bool addDescription = false
31+
bool addDescription = false,
32+
bool addMaxLength = false,
33+
bool addMinLength = false
3034
)
3135
{
3236
Name = name;
@@ -48,6 +52,8 @@ public DataAnnotationValuesDetailedOptions(
4852
AddRequired = addRequired;
4953
AddDisplay = addDisplay;
5054
AddDescription = addDescription;
55+
AddMaxLength = addMaxLength;
56+
AddMinLength = addMinLength;
5157
}
5258

5359
public bool Equals(DataAnnotationValuesDetailedOptions other)
@@ -61,6 +67,8 @@ public bool Equals(DataAnnotationValuesDetailedOptions other)
6167
&& AddRequired == other.AddRequired
6268
&& AddDisplay == other.AddDisplay
6369
&& AddDescription == other.AddDescription
70+
&& AddMaxLength == other.AddMaxLength
71+
&& AddMinLength == other.AddMinLength
6472
&& TypesEqual(Types, other.Types);
6573
}
6674

@@ -111,6 +119,8 @@ public override int GetHashCode()
111119
hash = hash * 23 + AddRequired.GetHashCode();
112120
hash = hash * 23 + AddDisplay.GetHashCode();
113121
hash = hash * 23 + AddDescription.GetHashCode();
122+
hash = hash * 23 + AddMaxLength.GetHashCode();
123+
hash = hash * 23 + AddMinLength.GetHashCode();
114124

115125
if (!Types.IsDefault)
116126
{

0 commit comments

Comments
 (0)