question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for validation and custom types

See original GitHub issue

Issue by clmcgrath Monday Jun 26, 2017 at 18:21 GMT _Originally opened as https://github.com/gsscoder/commandline/issues/455_


Add some sort of hook or attribute to plug validation into the parser pipeline

similar some similar frameworks use attributes or factory registration to allow an entrypoint for custom validation and type mapping (DI support would be awesome as well 😉 )

a typical and widely used usage scenario for this would be filesystem path validation and mapping to IO classes such as FileInfo / DirectoryInfo an seemingly easy way to to add this type of validation functionality would be to allow validation attributes that inherit from a validation base class or interface and run custom or pre-written code

some examples of validation provided in similar frameworks are

[FileExists] 
[DirectoryExists] 
[FileNotExists]
[DirectoryNotExists] 
[RegExValidation]

Powerargs provides a special class called a Reviver for custom type mapping as well that the framework looks for to try and map values to a custom or complex type ie path string => FileInfo

It is very hard to find a framework that does all 3 of these well currently and make this library a go to framework for myself and many other developers i am sure

as CLP 2.x is still in beta and major rewrite phase i think now would be a good time to look at feature expansion as well to increase the power of this framework

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:11
  • Comments:25 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
ohadschncommented, Aug 21, 2019

The way I do this in the meantime is by adding a Validate method to my options class:

[Verb("open", HelpText = "Open the foo file")]
public class FooOptions
{
    [Option('f', "fooPath", Required = true, HelpText = "Foo")]
    public string FooPath { get; }

    protected FooOptions(string fooPath)
    {
        FooPath = fooPath;
    }

    public void Validate()
    {
        if (!File.Exists(FooPath))
        {
            throw new ArgumentValidationException($"Foo path does not exist!");
        }
    }
}

The exception I’m throwing is just a custom type:

class ArgumentValidationException : Exception
{
    public ArgumentValidationException(string message) : base(message) { }
}

And in my parsing code I do something like this:

static int Main(string[] args)
{
    try
    {
        return Parser.Default.ParseArguments<FooOptions>(args)
            .MapResult(
                (FooOptions opts) =>
                {
                    opts.Validate();
                    //do something with opts.FooPath
                    return 0;
                },
                errors => 1);
    }
    catch (ArgumentValidationException e)
    {
        Console.WriteLine("Invalid arguments detected: {0}", e);
        return 1;
    }
}
4reactions
lupino3commented, Nov 5, 2018

Is there still interest to add validation capabilities to the framework?

I am thinking about adding to Option an optional Func<Error> which contains user-provided validation logic and runs after parsing is done.

The approach listed by @rducom would also be good IMHO.

In any case, I think it’s an useful addition to the library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for custom types for validation and schema ...
Question (relevant to 1.5.1 and current master) In my project I have a class FloatProperty that implements numbers.
Read more >
Response Requirements & Validation
Custom validation : Used to force a certain type of response (for example, making sure a respondent selects the right amount of answer...
Read more >
jsonchema: Custom type, format and validator in Python
To add support for custom type, format, and validator, we must create a new IValidator class. We will base it on Draft4Validator.
Read more >
Custom Data Types - Pydantic
Pydantic takes advantage of this to allow you to create types that are identical to the original type as far as type checkers...
Read more >
Defining Constraints (Validations) on Entities and/or Fields.
This method is used to add constraints to a field on an entity type defined in a module other than your own, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found