Class ValidationExtensions
- Namespace
- McMaster.Extensions.CommandLineUtils
- Assembly
- McMaster.Extensions.CommandLineUtils.dll
Extension methods for adding validation rules to options and arguments.
public static class ValidationExtensions
- Inheritance
-
objectValidationExtensions
Methods
Accepts(CommandArgument)
Creates a builder for specifying a set of rules used to determine if input is valid.
public static IArgumentValidationBuilder Accepts(this CommandArgument argument)
Parameters
argumentCommandArgumentThe argument.
Returns
- IArgumentValidationBuilder
The builder.
Accepts(CommandArgument, Action<IArgumentValidationBuilder>)
Specifies a set of rules used to determine if input is valid.
public static CommandArgument Accepts(this CommandArgument argument, Action<IArgumentValidationBuilder> configure)
Parameters
argumentCommandArgumentThe argument.
configureAction<IArgumentValidationBuilder>A function to configure rules on the validation builder.
Returns
- CommandArgument
The argument.
Accepts(CommandOption)
Creates a builder for specifying a set of rules used to determine if input is valid.
public static IOptionValidationBuilder Accepts(this CommandOption option)
Parameters
optionCommandOptionThe option.
Returns
- IOptionValidationBuilder
The builder.
Accepts(CommandOption, Action<IOptionValidationBuilder>)
Specifies a set of rules used to determine if input is valid.
public static CommandOption Accepts(this CommandOption option, Action<IOptionValidationBuilder> configure)
Parameters
optionCommandOptionThe option.
configureAction<IOptionValidationBuilder>A function to configure rules on the validation builder.
Returns
- CommandOption
The option.
Accepts<T>(CommandArgument<T>)
Creates a builder for specifying a set of rules used to determine if input is valid.
public static IArgumentValidationBuilder<T> Accepts<T>(this CommandArgument<T> argument)
Parameters
argumentCommandArgument<T>The argument.
Returns
- IArgumentValidationBuilder<T>
The builder.
Type Parameters
T
Accepts<T>(CommandArgument<T>, Action<IArgumentValidationBuilder<T>>)
Specifies a set of rules used to determine if input is valid.
public static CommandArgument<T> Accepts<T>(this CommandArgument<T> argument, Action<IArgumentValidationBuilder<T>> configure)
Parameters
argumentCommandArgument<T>The argument.
configureAction<IArgumentValidationBuilder<T>>A function to configure rules on the validation builder.
Returns
- CommandArgument<T>
The argument.
Type Parameters
T
Accepts<T>(CommandOption<T>)
Creates a builder for specifying a set of rules used to determine if input is valid.
public static IOptionValidationBuilder<T> Accepts<T>(this CommandOption<T> option)
Parameters
optionCommandOption<T>The option.
Returns
- IOptionValidationBuilder<T>
The builder.
Type Parameters
T
Accepts<T>(CommandOption<T>, Action<IOptionValidationBuilder<T>>)
Specifies a set of rules used to determine if input is valid.
public static CommandOption<T> Accepts<T>(this CommandOption<T> option, Action<IOptionValidationBuilder<T>> configure)
Parameters
optionCommandOption<T>The option.
configureAction<IOptionValidationBuilder<T>>A function to configure rules on the validation builder.
Returns
- CommandOption<T>
The option.
Type Parameters
T
EmailAddress(IValidationBuilder, string?)
Specifies that values must be a valid email address.
public static IValidationBuilder EmailAddress(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
Enum<TEnum>(IValidationBuilder, bool)
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.
public static IValidationBuilder Enum<TEnum>(this IValidationBuilder builder, bool ignoreCase = false) where TEnum : struct
Parameters
builderIValidationBuilderThe builder.
ignoreCaseboolIgnore case when parsing enums.
Returns
- IValidationBuilder
The builder.
Type Parameters
TEnum
Exceptions
- ArgumentException
When
TEnumis not an enum.
ExistingDirectory(IValidationBuilder, string?)
Specifies that values must be a path to a directory that already exists.
public static IValidationBuilder ExistingDirectory(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
ExistingFile(IValidationBuilder, string?)
Specifies that values must be a path to a file that already exists.
public static IValidationBuilder ExistingFile(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
ExistingFileOrDirectory(IValidationBuilder, string?)
Specifies that values must be a valid file path or directory, and the file path must already exist.
public static IValidationBuilder ExistingFileOrDirectory(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
IsRequired(CommandArgument, bool, string?)
Indicates the argument is required.
public static CommandArgument IsRequired(this CommandArgument argument, bool allowEmptyStrings = false, string? errorMessage = null)
Parameters
argumentCommandArgumentThe argument.
allowEmptyStringsboolIndicates whether an empty string is allowed.
errorMessagestringThe custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.
Returns
- CommandArgument
The argument.
IsRequired(CommandOption, bool, string?)
Indicates the option is required.
public static CommandOption IsRequired(this CommandOption option, bool allowEmptyStrings = false, string? errorMessage = null)
Parameters
optionCommandOptionThe option.
allowEmptyStringsboolIndicates whether an empty string is allowed.
errorMessagestringThe custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.
Returns
- CommandOption
The option.
IsRequired<T>(CommandArgument<T>, bool, string?)
Indicates the argument is required.
public static CommandArgument<T> IsRequired<T>(this CommandArgument<T> argument, bool allowEmptyStrings = false, string? errorMessage = null)
Parameters
argumentCommandArgument<T>The argument.
allowEmptyStringsboolIndicates whether an empty string is allowed.
errorMessagestringThe custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.
Returns
- CommandArgument<T>
The argument.
Type Parameters
T
IsRequired<T>(CommandOption<T>, bool, string?)
Indicates the option is required.
public static CommandOption<T> IsRequired<T>(this CommandOption<T> option, bool allowEmptyStrings = false, string? errorMessage = null)
Parameters
optionCommandOption<T>The option.
allowEmptyStringsboolIndicates whether an empty string is allowed.
errorMessagestringThe custom error message to display. See also: System.ComponentModel.DataAnnotations.ValidationAttribute.ErrorMessage.
Returns
- CommandOption<T>
The option.
Type Parameters
T
LegalFilePath(IValidationBuilder, string?)
Specifies that values must be legal file paths.
public static IValidationBuilder LegalFilePath(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
MaxLength(IValidationBuilder, int, string?)
Specifies that values must be a string no more than length characters long.
public static IValidationBuilder MaxLength(this IValidationBuilder builder, int length, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
lengthintThe maximum length.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
MinLength(IValidationBuilder, int, string?)
Specifies that values must be a string at least length characters long.
public static IValidationBuilder MinLength(this IValidationBuilder builder, int length, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder
lengthintThe minimum length.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
NonExistingDirectory(IValidationBuilder, string?)
Specifies that values must be a path to a directory that does not already exist.
public static IValidationBuilder NonExistingDirectory(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
NonExistingFile(IValidationBuilder, string?)
Specifies that values must be a path to a file that does not already exist.
public static IValidationBuilder NonExistingFile(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- 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.
public static IValidationBuilder NonExistingFileOrDirectory(this IValidationBuilder builder, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
OnValidate(CommandArgument, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
public static CommandArgument OnValidate(this CommandArgument argument, Func<ValidationContext, ValidationResult> validate)
Parameters
argumentCommandArgumentThe argument.
validateFunc<ValidationContext, ValidationResult>The callback. Return Success if there is no error.
Returns
OnValidate(CommandLineApplication, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
public static CommandLineApplication OnValidate(this CommandLineApplication command, Func<ValidationContext, ValidationResult> validate)
Parameters
commandCommandLineApplicationThe command.
validateFunc<ValidationContext, ValidationResult>The callback. Return Success if there is no error.
Returns
OnValidate(CommandOption, Func<ValidationContext, ValidationResult>)
Adds a validator that runs after parsing is complete and before command execution.
public static CommandOption OnValidate(this CommandOption option, Func<ValidationContext, ValidationResult> validate)
Parameters
optionCommandOptionThe option.
validateFunc<ValidationContext, ValidationResult>The callback. Return Success if there is no error.
Returns
Range(IValidationBuilder<double>, double, double, string?)
Specifies that values must be in a given range.
public static IValidationBuilder<double> Range(this IValidationBuilder<double> builder, double minimum, double maximum, string? errorMessage = null)
Parameters
builderIValidationBuilder<double>The builder.
minimumdoubleThe minimum allowed value.
maximumdoubleThe maximum allowed value.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder<double>
The builder.
Range(IValidationBuilder<int>, int, int, string?)
Specifies that values must be in a given range.
public static IValidationBuilder<int> Range(this IValidationBuilder<int> builder, int minimum, int maximum, string? errorMessage = null)
Parameters
builderIValidationBuilder<int>The builder.
minimumintThe minimum allowed value.
maximumintThe maximum allowed value.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder<int>
The builder.
RegularExpression(IValidationBuilder, string, string?)
Specifies that values must match a regular expression.
public static IValidationBuilder RegularExpression(this IValidationBuilder builder, string pattern, string? errorMessage = null)
Parameters
builderIValidationBuilderThe builder.
patternstringThe regular expression.
errorMessagestringA custom error message to display.
Returns
- IValidationBuilder
The builder.
Satisfies<TAttribute>(IValidationBuilder, string?, params object[])
Specifies that values must satisfy the requirements of the validation attribute of type TAttribute.
public static IValidationBuilder Satisfies<TAttribute>(this IValidationBuilder builder, string? errorMessage = null, params object[] ctorArgs) where TAttribute : ValidationAttribute
Parameters
builderIValidationBuilderThe builder.
errorMessagestringA custom error message to display.
ctorArgsobject[]Constructor arguments for
TAttribute.
Returns
- IValidationBuilder
The builder.
Type Parameters
TAttributeThe validation attribute type.
Values(IValidationBuilder, bool, params string[])
Specifies that values must be one of the values in a given set.
public static IValidationBuilder Values(this IValidationBuilder builder, bool ignoreCase, params string[] allowedValues)
Parameters
builderIValidationBuilderThe builder.
ignoreCaseboolIgnore case when comparing inputs to
allowedValues.allowedValuesstring[]Allowed values.
Returns
- IValidationBuilder
The builder.
Values(IValidationBuilder, StringComparison, params string[])
Specifies that values must be one of the values in a given set.
public static IValidationBuilder Values(this IValidationBuilder builder, StringComparison comparer, params string[] allowedValues)
Parameters
builderIValidationBuilderThe builder.
comparerStringComparisonThe comparer used to determine if values match.
allowedValuesstring[]Allowed values.
Returns
- IValidationBuilder
The builder.
Values(IValidationBuilder, params 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, bool, params string[]).
public static IValidationBuilder Values(this IValidationBuilder builder, params string[] allowedValues)
Parameters
builderIValidationBuilderThe builder.
allowedValuesstring[]Allowed values.
Returns
- IValidationBuilder
The builder.