commandr.validators

User input validation.

This module contains core functionality for veryfing values as well as some basic validators.

Options and arguments (no flags, as they cannot take value) can have any number of validators attached. Validators are objects that verify user input after parsing values - every validator is run once per option/argument that it is attached to with complete vector of user input (e.g. for repeating values).

Validators are ran in order they are defined aborting on first failure with ValidationException. Exception contains the validator that caused the exception (optionally) along with error message.

Validators can be attached using validate method, or using helper methods in form of accepts*:

1 new Program("test")
2  .add(new Option("t", "test", "description")
3      .acceptsValues(["test", "bar"])
4  )
5  // both are equivalent
6  .add(new Option("T", "test2", "description")
7      .validate(new EnumValidator(["test", "bar"]))
8  )

Custom Validators

To add custom validating logic, you can either create custom Validator class that implements IValidator interface or use DelegateValidator passing delegate with your validating logic:

1 new Program("test")
2  .add(new Option("t", "test", "description")
3      .validateWith((entry, value) {
4          if (value == "test") throw new ValidationException("Value must be test");
5      })
6      .validateWith(arg => isNumericString(arg), "must be numeric")
7  )

Validators already provided: - EnumValidator (acceptsValues) - FileSystemValidator (acceptsFiles, acceptsDirectories) - DelegateValidator (validateWith, validateEachWith)

Members

Classes

DelegateValidator
class DelegateValidator

Validates input based on delegate.

EnumValidator
class EnumValidator

Input whitelist check.

FileSystemValidator
class FileSystemValidator

FileSystem validator.

ValidationException
class ValidationException

Validation error.

Enums

FileType
enum FileType

Specified expected entry type for FileSystemValidator.

Functions

acceptsDirectories
T acceptsDirectories(T entry)

Helper function to require passing a path pointing to existing directory.

acceptsFiles
T acceptsFiles(T entry)

Helper function to require passing a path pointing to existing file.

acceptsPaths
T acceptsPaths(T entry, bool existing)

Helper function to require passing a path pointing to existing file or directory.

acceptsValues
T acceptsValues(T entry, string[] values)

Helper function to define allowed values for an option or argument.

validateEachWith
T validateEachWith(T entry, void delegate(IEntry, string) validator)

Helper function to add custom validating delegate.

validateEachWith
T validateEachWith(T entry, bool delegate(string) validator, string errorMessage)

Helper function to add custom validating delegate.

validateWith
T validateWith(T entry, DelegateValidator.ValidatorFunc validator)

Helper function to add custom validating delegate.

Interfaces

IValidator
interface IValidator

Interface for validators.

See Also

IValidator, ValidationException

Meta