@@ -23,8 +23,10 @@ public static class ValidationExtensions
23
23
public static CommandOption IsRequired ( this CommandOption option , bool allowEmptyStrings = false , string ? errorMessage = null )
24
24
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
25
25
{
26
- var attribute = GetValidationAttr < RequiredAttribute > ( errorMessage ) ;
27
- attribute . AllowEmptyStrings = allowEmptyStrings ;
26
+ var attribute = AddErrorMessage ( new RequiredAttribute
27
+ {
28
+ AllowEmptyStrings = allowEmptyStrings
29
+ } , errorMessage ) ;
28
30
option . Validators . Add ( new AttributeValidator ( attribute ) ) ;
29
31
return option ;
30
32
}
@@ -57,8 +59,10 @@ public static CommandOption<T> IsRequired<T>(this CommandOption<T> option, bool
57
59
public static CommandArgument IsRequired ( this CommandArgument argument , bool allowEmptyStrings = false , string ? errorMessage = null )
58
60
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
59
61
{
60
- var attribute = GetValidationAttr < RequiredAttribute > ( errorMessage ) ;
61
- attribute . AllowEmptyStrings = allowEmptyStrings ;
62
+ var attribute = AddErrorMessage ( new RequiredAttribute
63
+ {
64
+ AllowEmptyStrings = allowEmptyStrings
65
+ } , errorMessage ) ;
62
66
argument . Validators . Add ( new AttributeValidator ( attribute ) ) ;
63
67
return argument ;
64
68
}
@@ -247,7 +251,7 @@ public static IValidationBuilder Values(this IValidationBuilder builder, bool ig
247
251
/// <returns>The builder.</returns>
248
252
public static IValidationBuilder Values ( this IValidationBuilder builder , StringComparison comparer , params string [ ] allowedValues )
249
253
{
250
- return builder . Satisfies < AllowedValuesAttribute > ( ctorArgs : new object [ ] { comparer , allowedValues } ) ;
254
+ return builder . Satisfies ( new AllowedValuesAttribute ( comparer , allowedValues ) ) ;
251
255
}
252
256
253
257
/// <summary>
@@ -257,7 +261,7 @@ public static IValidationBuilder Values(this IValidationBuilder builder, StringC
257
261
/// <param name="errorMessage">A custom error message to display.</param>
258
262
/// <returns>The builder.</returns>
259
263
public static IValidationBuilder EmailAddress ( this IValidationBuilder builder , string ? errorMessage = null )
260
- => builder . Satisfies < EmailAddressAttribute > ( errorMessage ) ;
264
+ => builder . Satisfies ( new EmailAddressAttribute ( ) , errorMessage ) ;
261
265
262
266
/// <summary>
263
267
/// Specifies that values must be a path to a file that already exists.
@@ -266,7 +270,7 @@ public static IValidationBuilder EmailAddress(this IValidationBuilder builder, s
266
270
/// <param name="errorMessage">A custom error message to display.</param>
267
271
/// <returns>The builder.</returns>
268
272
public static IValidationBuilder ExistingFile ( this IValidationBuilder builder , string ? errorMessage = null )
269
- => builder . Satisfies < FileExistsAttribute > ( errorMessage ) ;
273
+ => builder . Satisfies ( new FileExistsAttribute ( ) , errorMessage ) ;
270
274
271
275
/// <summary>
272
276
/// Specifies that values must be a path to a file that does not already exist.
@@ -275,7 +279,7 @@ public static IValidationBuilder ExistingFile(this IValidationBuilder builder, s
275
279
/// <param name="errorMessage">A custom error message to display.</param>
276
280
/// <returns>The builder.</returns>
277
281
public static IValidationBuilder NonExistingFile ( this IValidationBuilder builder , string ? errorMessage = null )
278
- => builder . Satisfies < FileNotExistsAttribute > ( errorMessage ) ;
282
+ => builder . Satisfies ( new FileNotExistsAttribute ( ) , errorMessage ) ;
279
283
280
284
/// <summary>
281
285
/// Specifies that values must be a path to a directory that already exists.
@@ -284,7 +288,7 @@ public static IValidationBuilder NonExistingFile(this IValidationBuilder builder
284
288
/// <param name="errorMessage">A custom error message to display.</param>
285
289
/// <returns>The builder.</returns>
286
290
public static IValidationBuilder ExistingDirectory ( this IValidationBuilder builder , string ? errorMessage = null )
287
- => builder . Satisfies < DirectoryExistsAttribute > ( errorMessage ) ;
291
+ => builder . Satisfies ( new DirectoryExistsAttribute ( ) , errorMessage ) ;
288
292
289
293
/// <summary>
290
294
/// Specifies that values must be a path to a directory that does not already exist.
@@ -293,7 +297,7 @@ public static IValidationBuilder ExistingDirectory(this IValidationBuilder build
293
297
/// <param name="errorMessage">A custom error message to display.</param>
294
298
/// <returns>The builder.</returns>
295
299
public static IValidationBuilder NonExistingDirectory ( this IValidationBuilder builder , string ? errorMessage = null )
296
- => builder . Satisfies < DirectoryNotExistsAttribute > ( errorMessage ) ;
300
+ => builder . Satisfies ( new DirectoryNotExistsAttribute ( ) , errorMessage ) ;
297
301
298
302
/// <summary>
299
303
/// Specifies that values must be a valid file path or directory, and the file path must already exist.
@@ -302,7 +306,7 @@ public static IValidationBuilder NonExistingDirectory(this IValidationBuilder bu
302
306
/// <param name="errorMessage">A custom error message to display.</param>
303
307
/// <returns>The builder.</returns>
304
308
public static IValidationBuilder ExistingFileOrDirectory ( this IValidationBuilder builder , string ? errorMessage = null )
305
- => builder . Satisfies < FileOrDirectoryExistsAttribute > ( errorMessage ) ;
309
+ => builder . Satisfies ( new FileOrDirectoryExistsAttribute ( ) , errorMessage ) ;
306
310
307
311
/// <summary>
308
312
/// Specifies that values must be a valid file path or directory, and the file path must not already exist.
@@ -311,7 +315,7 @@ public static IValidationBuilder ExistingFileOrDirectory(this IValidationBuilder
311
315
/// <param name="errorMessage">A custom error message to display.</param>
312
316
/// <returns>The builder.</returns>
313
317
public static IValidationBuilder NonExistingFileOrDirectory ( this IValidationBuilder builder , string ? errorMessage = null )
314
- => builder . Satisfies < FileOrDirectoryNotExistsAttribute > ( errorMessage ) ;
318
+ => builder . Satisfies ( new FileOrDirectoryNotExistsAttribute ( ) , errorMessage ) ;
315
319
316
320
/// <summary>
317
321
/// Specifies that values must be legal file paths.
@@ -320,7 +324,7 @@ public static IValidationBuilder NonExistingFileOrDirectory(this IValidationBuil
320
324
/// <param name="errorMessage">A custom error message to display.</param>
321
325
/// <returns>The builder.</returns>
322
326
public static IValidationBuilder LegalFilePath ( this IValidationBuilder builder , string ? errorMessage = null )
323
- => builder . Satisfies < LegalFilePathAttribute > ( errorMessage ) ;
327
+ => builder . Satisfies ( new LegalFilePathAttribute ( ) , errorMessage ) ;
324
328
325
329
/// <summary>
326
330
/// Specifies that values must be a string at least <paramref name="length"/> characters long.
@@ -330,7 +334,7 @@ public static IValidationBuilder LegalFilePath(this IValidationBuilder builder,
330
334
/// <param name="errorMessage">A custom error message to display.</param>
331
335
/// <returns>The builder.</returns>
332
336
public static IValidationBuilder MinLength ( this IValidationBuilder builder , int length , string ? errorMessage = null )
333
- => builder . Satisfies < MinLengthAttribute > ( errorMessage , length ) ;
337
+ => builder . Satisfies ( new MinLengthAttribute ( length ) , errorMessage ) ;
334
338
335
339
/// <summary>
336
340
/// Specifies that values must be a string no more than <paramref name="length"/> characters long.
@@ -340,7 +344,7 @@ public static IValidationBuilder MinLength(this IValidationBuilder builder, int
340
344
/// <param name="errorMessage">A custom error message to display.</param>
341
345
/// <returns>The builder.</returns>
342
346
public static IValidationBuilder MaxLength ( this IValidationBuilder builder , int length , string ? errorMessage = null )
343
- => builder . Satisfies < MaxLengthAttribute > ( errorMessage , length ) ;
347
+ => builder . Satisfies ( new MaxLengthAttribute ( length ) , errorMessage ) ;
344
348
345
349
/// <summary>
346
350
/// Specifies that values must match a regular expression.
@@ -350,7 +354,7 @@ public static IValidationBuilder MaxLength(this IValidationBuilder builder, int
350
354
/// <param name="errorMessage">A custom error message to display.</param>
351
355
/// <returns>The builder.</returns>
352
356
public static IValidationBuilder RegularExpression ( this IValidationBuilder builder , string pattern , string ? errorMessage = null )
353
- => builder . Satisfies < RegularExpressionAttribute > ( errorMessage , pattern ) ;
357
+ => builder . Satisfies ( new RegularExpressionAttribute ( pattern ) , errorMessage ) ;
354
358
355
359
/// <summary>
356
360
/// Specifies that values must satisfy the requirements of the validation attribute of type <typeparamref name="TAttribute"/>.
@@ -380,7 +384,7 @@ public static IValidationBuilder Satisfies<TAttribute>(this IValidationBuilder b
380
384
public static IValidationBuilder < int > Range ( this IValidationBuilder < int > builder , int minimum , int maximum , string ? errorMessage = null )
381
385
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
382
386
{
383
- var attribute = GetValidationAttr < RangeAttribute > ( errorMessage , new object [ ] { minimum , maximum } ) ;
387
+ var attribute = AddErrorMessage ( new RangeAttribute ( minimum , maximum ) , errorMessage ) ;
384
388
builder . Use ( new AttributeValidator ( attribute ) ) ;
385
389
return builder ;
386
390
}
@@ -397,7 +401,7 @@ public static IValidationBuilder<int> Range(this IValidationBuilder<int> builder
397
401
public static IValidationBuilder < double > Range ( this IValidationBuilder < double > builder , double minimum , double maximum , string ? errorMessage = null )
398
402
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
399
403
{
400
- var attribute = GetValidationAttr < RangeAttribute > ( errorMessage , new object [ ] { minimum , maximum } ) ;
404
+ var attribute = AddErrorMessage ( new RangeAttribute ( minimum , maximum ) , errorMessage ) ;
401
405
builder . Use ( new AttributeValidator ( attribute ) ) ;
402
406
return builder ;
403
407
}
@@ -438,6 +442,16 @@ public static CommandOption OnValidate(this CommandOption option, Func<Validatio
438
442
return option ;
439
443
}
440
444
445
+ private static IValidationBuilder Satisfies ( this IValidationBuilder builder , ValidationAttribute attribute , string ? errorMessage = null )
446
+ {
447
+ if ( errorMessage is not null )
448
+ {
449
+ attribute . ErrorMessage = errorMessage ;
450
+ }
451
+ builder . Use ( new AttributeValidator ( attribute ) ) ;
452
+ return builder ;
453
+ }
454
+
441
455
private static T GetValidationAttr < T > ( string ? errorMessage , object [ ] ? ctorArgs = null )
442
456
where T : ValidationAttribute
443
457
{
@@ -448,5 +462,15 @@ private static T GetValidationAttr<T>(string? errorMessage, object[]? ctorArgs =
448
462
}
449
463
return attribute ;
450
464
}
465
+
466
+ private static T AddErrorMessage < T > ( T attribute , string ? errorMessage )
467
+ where T : ValidationAttribute
468
+ {
469
+ if ( errorMessage != null )
470
+ {
471
+ attribute . ErrorMessage = errorMessage ;
472
+ }
473
+ return attribute ;
474
+ }
451
475
}
452
476
}
0 commit comments