Validators reference

Validation in ConsoleFx can be performed for:
  • option parameters, using the Option class ValidateWith extension method
  • arguments, using the Argument class ValidateWith extension method

Both ValidateWith methods accept a list of validator class (deriving from BaseValidator) instances. The Option class has an additional overload that accepts the index of the parameter to validate.

Note: Unless specifically mentioned, all validator classes have a property called ErrorMessage that allows you to customize the error message displayed in case it's validation check fails.

BooleanValidator
CustomValidator
EnumValidator
EnumValidator<TEnum>
IntegerValidator
LookupValidator
PathValidator
RegexValidator
StringValidator

BooleanValidator

Validates whether the value is a valid .NET boolean value, i.e. True or False.

BooleanValidator()

CustomValidator

Validates whether the value satisfies one or more custom conditions specified by a predicate function.

CustomValidator(Func<string, bool> validator)
validator: The predicate function that performs the custom check.

EnumValidator

Validates whether the value belongs to a specified enum type.

EnumValidator(Type enumType, bool ignoreCase = true)
enumType: The type of the enum to check against. The validator will throw an ArgumentException if the type if not an enum.
ignoreCase: Specifies whether the check should be case sensitive.

EnumValidator<TEnum>

Generic version of the EnumValidator, providing a simpler syntax.
This class derives from EnumValidator.;

EnumValidator<TEnum>(bool ignoreCase = true) where TEnum : struct
ignoreCase: Specifies whether the check should be case sensitibe.

IntegerValidator

Validates whether the value is an integer and optionally, whether it falls within a specific range.

IntegerValidator(long minimumValue = long.MinValue, long maximumValue = long.MaxValue)
minimumValue: Optional parameter specifying the minimum allowed value.
maximumValue: Optional parameter specifying the maximum allowed value.

LookupValidator

Validates whether the value belongs to a predefined set of string values.

LookupValidator(IEnumerable<string> items)
items: The predefined set of string values to check against.

PathValidator

Validates whether the value is a valid file or directory path, and optionally whether that path actually exists on the file system.

PathValidator(PathType pathType = PathType.File, bool checkIfExists = true)
pathType: Optional parameter specifying the type of path to check - file or directory. Defaults to file if not specified.
checkIfExists: Optional parameter specifying whether to check for the path's existence on the file system. Defaults to true, if not specified.

RegexValidator

Validates whether the value matches a specified regular expression pattern.

RegexValidator(Regex regex)
regex: The Regex object containing the regular expression pattern to compare against.
RegexValidator(string pattern)
pattern: The regular expression pattern to compare against.

StringValidator

Validates whether the value's length falls within a specified range.

StringValidator(int maxLength)
maxLength: The maximum allowed length of the string. In this case, the minimum allowed length will be 0 (zero).
StringValidator(int minLength, int maxLength)

maxLength: The maximum allowed length of the string.
minLength: The minimum allowed length of the string.

Last edited Jun 23, 2012 at 10:55 PM by jeevanjj, version 9

Comments

No comments yet.