Search Results for

    Show / Hide Table of Contents

    Class ValidationExtensions

    Extension methods for adding validation rules to options and arguments.

    Inheritance
    System.Object
    ValidationExtensions
    Namespace: McMaster.Extensions.CommandLineUtils
    Assembly: McMaster.Extensions.CommandLineUtils.dll
    Syntax
    public static class ValidationExtensions

    Methods

    | Improve this Doc View Source

    Accepts(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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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 TEnum is not an enum.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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
    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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 TAttribute.

    Returns
    Type Description
    IValidationBuilder

    The builder.

    Type Parameters
    Name Description
    TAttribute

    The validation attribute type.

    | Improve this Doc View Source

    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 allowedValues.

    System.String[] allowedValues

    Allowed values.

    Returns
    Type Description
    IValidationBuilder

    The builder.

    | Improve this Doc View Source

    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.

    | Improve this Doc View Source

    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.

    • Improve this Doc
    • View Source
    In This Article
    Back to top