Class ValidationExtensions
Extension methods for adding validation rules to options and arguments.
Inheritance
Namespace: McMaster.Extensions.CommandLineUtils
Assembly: McMaster.Extensions.CommandLineUtils.dll
Syntax
public static class ValidationExtensions
Methods
| Improve this Doc View SourceAccepts(CommandArgument)
Creates a builder for specifying a set of rules used to determine if input is valid.
Declaration
public static IArgumentValidationBuilder Accepts(this CommandArgument argument)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument | argument | The argument. |
Returns
Type | Description |
---|---|
IArgumentValidationBuilder | The builder. |
Accepts(CommandArgument, Action<IArgumentValidationBuilder>)
Specifies a set of rules used to determine if input is valid.
Declaration
public static CommandArgument Accepts(this CommandArgument argument, Action<IArgumentValidationBuilder> configure)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument | argument | The argument. |
System.Action<IArgumentValidationBuilder> | configure | A function to configure rules on the validation builder. |
Returns
Type | Description |
---|---|
CommandArgument | The argument. |
Accepts(CommandOption)
Creates a builder for specifying a set of rules used to determine if input is valid.
Declaration
public static IOptionValidationBuilder Accepts(this CommandOption option)
Parameters
Type | Name | Description |
---|---|---|
CommandOption | option | The option. |
Returns
Type | Description |
---|---|
IOptionValidationBuilder | The builder. |
Accepts(CommandOption, Action<IOptionValidationBuilder>)
Specifies a set of rules used to determine if input is valid.
Declaration
public static CommandOption Accepts(this CommandOption option, Action<IOptionValidationBuilder> configure)
Parameters
Type | Name | Description |
---|---|---|
CommandOption | option | The option. |
System.Action<IOptionValidationBuilder> | configure | A function to configure rules on the validation builder. |
Returns
Type | Description |
---|---|
CommandOption | The option. |
Accepts<T>(CommandArgument<T>)
Creates a builder for specifying a set of rules used to determine if input is valid.
Declaration
public static IArgumentValidationBuilder<T> Accepts<T>(this CommandArgument<T> argument)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument<T> | argument | The argument. |
Returns
Type | Description |
---|---|
IArgumentValidationBuilder<T> | The builder. |
Type Parameters
Name | Description |
---|---|
T |
Accepts<T>(CommandArgument<T>, Action<IArgumentValidationBuilder<T>>)
Specifies a set of rules used to determine if input is valid.
Declaration
public static CommandArgument<T> Accepts<T>(this CommandArgument<T> argument, Action<IArgumentValidationBuilder<T>> configure)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument<T> | argument | The argument. |
System.Action<IArgumentValidationBuilder<T>> | configure | A function to configure rules on the validation builder. |
Returns
Type | Description |
---|---|
CommandArgument<T> | The argument. |
Type Parameters
Name | Description |
---|---|
T |
Accepts<T>(CommandOption<T>)
Creates a builder for specifying a set of rules used to determine if input is valid.
Declaration
public static IOptionValidationBuilder<T> Accepts<T>(this CommandOption<T> option)
Parameters
Type | Name | Description |
---|---|---|
CommandOption<T> | option | The option. |
Returns
Type | Description |
---|---|
IOptionValidationBuilder<T> | The builder. |
Type Parameters
Name | Description |
---|---|
T |
Accepts<T>(CommandOption<T>, Action<IOptionValidationBuilder<T>>)
Specifies a set of rules used to determine if input is valid.
Declaration
public static CommandOption<T> Accepts<T>(this CommandOption<T> option, Action<IOptionValidationBuilder<T>> configure)
Parameters
Type | Name | Description |
---|---|---|
CommandOption<T> | option | The option. |
System.Action<IOptionValidationBuilder<T>> | configure | A function to configure rules on the validation builder. |
Returns
Type | Description |
---|---|
CommandOption<T> | The option. |
Type Parameters
Name | Description |
---|---|
T |
EmailAddress(IValidationBuilder, String)
Specifies that values must be a valid email address.
Declaration
public static IValidationBuilder EmailAddress(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Enum<TEnum>(IValidationBuilder, Boolean)
Specifies that values must be one of the values in a given set.
By default, value comparison is case-sensitive. To make matches case-insensitive, set ignoreCase
to true
.
Declaration
public static IValidationBuilder Enum<TEnum>(this IValidationBuilder builder, bool ignoreCase = false)
where TEnum : struct
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.Boolean | ignoreCase | Ignore case when parsing enums. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Type Parameters
Name | Description |
---|---|
TEnum |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | When |
ExistingDirectory(IValidationBuilder, String)
Specifies that values must be a path to a directory that already exists.
Declaration
public static IValidationBuilder ExistingDirectory(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
ExistingFile(IValidationBuilder, String)
Specifies that values must be a path to a file that already exists.
Declaration
public static IValidationBuilder ExistingFile(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
ExistingFileOrDirectory(IValidationBuilder, String)
Specifies that values must be a valid file path or directory, and the file path must already exist.
Declaration
public static IValidationBuilder ExistingFileOrDirectory(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
IsRequired(CommandArgument, Boolean, String)
Indicates the argument is required.
Declaration
public static CommandArgument IsRequired(this CommandArgument argument, bool allowEmptyStrings = false, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument | argument | The argument. |
System.Boolean | allowEmptyStrings | Indicates whether an empty string is allowed. |
System.String | errorMessage | The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage. |
Returns
Type | Description |
---|---|
CommandArgument | The argument. |
IsRequired(CommandOption, Boolean, String)
Indicates the option is required.
Declaration
public static CommandOption IsRequired(this CommandOption option, bool allowEmptyStrings = false, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
CommandOption | option | The option. |
System.Boolean | allowEmptyStrings | Indicates whether an empty string is allowed. |
System.String | errorMessage | The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage. |
Returns
Type | Description |
---|---|
CommandOption | The option. |
IsRequired<T>(CommandArgument<T>, Boolean, String)
Indicates the argument is required.
Declaration
public static CommandArgument<T> IsRequired<T>(this CommandArgument<T> argument, bool allowEmptyStrings = false, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument<T> | argument | The argument. |
System.Boolean | allowEmptyStrings | Indicates whether an empty string is allowed. |
System.String | errorMessage | The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage. |
Returns
Type | Description |
---|---|
CommandArgument<T> | The argument. |
Type Parameters
Name | Description |
---|---|
T |
IsRequired<T>(CommandOption<T>, Boolean, String)
Indicates the option is required.
Declaration
public static CommandOption<T> IsRequired<T>(this CommandOption<T> option, bool allowEmptyStrings = false, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
CommandOption<T> | option | The option. |
System.Boolean | allowEmptyStrings | Indicates whether an empty string is allowed. |
System.String | errorMessage | The custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage. |
Returns
Type | Description |
---|---|
CommandOption<T> | The option. |
Type Parameters
Name | Description |
---|---|
T |
LegalFilePath(IValidationBuilder, String)
Specifies that values must be legal file paths.
Declaration
public static IValidationBuilder LegalFilePath(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
MaxLength(IValidationBuilder, Int32, String)
Specifies that values must be a string no more than length
characters long.
Declaration
public static IValidationBuilder MaxLength(this IValidationBuilder builder, int length, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.Int32 | length | The maximum length. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
MinLength(IValidationBuilder, Int32, String)
Specifies that values must be a string at least length
characters long.
Declaration
public static IValidationBuilder MinLength(this IValidationBuilder builder, int length, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder |
System.Int32 | length | The minimum length. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
NonExistingDirectory(IValidationBuilder, String)
Specifies that values must be a path to a directory that does not already exist.
Declaration
public static IValidationBuilder NonExistingDirectory(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
NonExistingFile(IValidationBuilder, String)
Specifies that values must be a path to a file that does not already exist.
Declaration
public static IValidationBuilder NonExistingFile(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
NonExistingFileOrDirectory(IValidationBuilder, String)
Specifies that values must be a valid file path or directory, and the file path must not already exist.
Declaration
public static IValidationBuilder NonExistingFileOrDirectory(this IValidationBuilder builder, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
OnValidate(CommandArgument, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
Declaration
public static CommandArgument OnValidate(this CommandArgument argument, Func<ValidationContext, ValidationResult> validate)
Parameters
Type | Name | Description |
---|---|---|
CommandArgument | argument | The argument. |
System.Func<System.ComponentModel.DataAnnotations.ValidationContext, System.ComponentModel.DataAnnotations.ValidationResult> | validate | The callback. Return System.ComponentModel.DataAnnotations.ValidationResult.Success if there is no error. |
Returns
Type | Description |
---|---|
CommandArgument |
OnValidate(CommandLineApplication, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
Declaration
public static CommandLineApplication OnValidate(this CommandLineApplication command, Func<ValidationContext, ValidationResult> validate)
Parameters
Type | Name | Description |
---|---|---|
CommandLineApplication | command | The command. |
System.Func<System.ComponentModel.DataAnnotations.ValidationContext, System.ComponentModel.DataAnnotations.ValidationResult> | validate | The callback. Return System.ComponentModel.DataAnnotations.ValidationResult.Success if there is no error. |
Returns
Type | Description |
---|---|
CommandLineApplication |
OnValidate(CommandOption, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
Declaration
public static CommandOption OnValidate(this CommandOption option, Func<ValidationContext, ValidationResult> validate)
Parameters
Type | Name | Description |
---|---|---|
CommandOption | option | The option. |
System.Func<System.ComponentModel.DataAnnotations.ValidationContext, System.ComponentModel.DataAnnotations.ValidationResult> | validate | The callback. Return System.ComponentModel.DataAnnotations.ValidationResult.Success if there is no error. |
Returns
Type | Description |
---|---|
CommandOption |
Range(IValidationBuilder<Double>, Double, Double, String)
Specifies that values must be in a given range.
Declaration
public static IValidationBuilder<double> Range(this IValidationBuilder<double> builder, double minimum, double maximum, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder<System.Double> | builder | The builder. |
System.Double | minimum | The minimum allowed value. |
System.Double | maximum | The maximum allowed value. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder<System.Double> | The builder. |
Range(IValidationBuilder<Int32>, Int32, Int32, String)
Specifies that values must be in a given range.
Declaration
public static IValidationBuilder<int> Range(this IValidationBuilder<int> builder, int minimum, int maximum, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder<System.Int32> | builder | The builder. |
System.Int32 | minimum | The minimum allowed value. |
System.Int32 | maximum | The maximum allowed value. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder<System.Int32> | The builder. |
RegularExpression(IValidationBuilder, String, String)
Specifies that values must match a regular expression.
Declaration
public static IValidationBuilder RegularExpression(this IValidationBuilder builder, string pattern, string errorMessage = null)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | pattern | The regular expression. |
System.String | errorMessage | A custom error message to display. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Satisfies<TAttribute>(IValidationBuilder, String, Object[])
Specifies that values must satisfy the requirements of the validation attribute of type TAttribute
.
Declaration
public static IValidationBuilder Satisfies<TAttribute>(this IValidationBuilder builder, string errorMessage = null, params object[] ctorArgs)
where TAttribute : ValidationAttribute
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String | errorMessage | A custom error message to display. |
System.Object[] | ctorArgs | Constructor arguments for |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Type Parameters
Name | Description |
---|---|
TAttribute | The validation attribute type. |
Values(IValidationBuilder, Boolean, String[])
Specifies that values must be one of the values in a given set.
Declaration
public static IValidationBuilder Values(this IValidationBuilder builder, bool ignoreCase, params string[] allowedValues)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.Boolean | ignoreCase | Ignore case when comparing inputs to |
System.String[] | allowedValues | Allowed values. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Values(IValidationBuilder, String[])
Specifies that values must be one of the values in a given set.
By default, value comparison is case-sensitive. To make matches case-insensitive, use Values(IValidationBuilder, Boolean, String[]).
Declaration
public static IValidationBuilder Values(this IValidationBuilder builder, params string[] allowedValues)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.String[] | allowedValues | Allowed values. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |
Values(IValidationBuilder, StringComparison, String[])
Specifies that values must be one of the values in a given set.
Declaration
public static IValidationBuilder Values(this IValidationBuilder builder, StringComparison comparer, params string[] allowedValues)
Parameters
Type | Name | Description |
---|---|---|
IValidationBuilder | builder | The builder. |
System.StringComparison | comparer | The comparer used to determine if values match. |
System.String[] | allowedValues | Allowed values. |
Returns
Type | Description |
---|---|
IValidationBuilder | The builder. |