Skip to content

DataAnnotations#

Use System.ComponentModel.DataAnnotations with CommandDotNet to validate arguments.

TLDR, How to enable#

dotnet add package CommandDotNet.DataAnnotations
Install-Package CommandDotNet.DataAnnotations

Enable with appRunner.UseDataAnnotationValidations(), or appRunner.UseDataAnnotationValidations(showHelpOnError: true)
to print help when there are validation errors.

Example#

public class Program
{
    static int Main(string[] args) => AppRunner.Run(args);

    public static AppRunner AppRunner =>
        new AppRunner<Program>()
            .UseNameCasing(Case.LowerCase)
            .UseDataAnnotationValidations();

    public void Create(IConsole console, Table table, [Option, Url] string server, Verbosity verbosity)
    {
        console.WriteLine($"created {table.Name} as {server}. notifying: {table.Owner}");
    }
}

public class Table : IArgumentModel
{
    [Operand, Required, MaxLength(10)]
    public string Name { get; set; }

    [Option, Required, EmailAddress]
    public string Owner { get; set; }
}

public class Verbosity : IArgumentModel, IValidatableObject
{
    [Option('q')]
    public bool Quiet { get; set; }
    [Option('v')]
    public bool Verbose { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Quiet && Verbose)
            yield return new ValidationResult("quiet and verbose are mutually exclusive. There can be only one!");
    }
}
snippet source | anchor

DataAnnotations can be defined on the method parameters and model properties. Notice

  • the Url validation on the server option
  • Required, MaxLength, and EmailAddress in the Table model
  • IValidatableObject implementation of the Verbosity model.

If the validation fails, the application exits with return code 2 and outputs validation error messages to error output.

$ dotnet table.dll create TooLongTableName --server bossman --owner abc -qv
'server' is not a valid fully-qualified http, https, or ftp URL.
'name' must be a string or array type with a maximum length of '10'.
'owner' is not a valid e-mail address.
quiet and verbose are mutually exclusive. There can be only one!
snippet source | anchor

Back to top