Skip to content

Unable to get empty value from nullable instance like TextPrompt<Uri?> #714

@danielklecha

Description

@danielklecha

I want to get AbsoluteUri or null but it's not supported by TextPrompt even with AllowEmpty=true.

var uri = await new TextPrompt<Uri?>( $"abc" )
    .AllowEmpty()
    .Validate( value =>
    {
        if ( !( value?.IsAbsoluteUri ?? true ) )
            return ValidationResult.Error( "[red]It must be absolute url[/]" );
        return ValidationResult.Success();
    } )
    .ShowAsync( AnsiConsole.Console, CancellationToken.None );

If I press enter then I get "Invalid input".

I suppose that input is string.Empty when we call TryConvertFromString.
UriConverter allow to convert from string.Empty and return null.
In that case condition in line 146 in TextPrompt should be modified and allow result to be null if conversion was successfull.

else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result) || result == null && Nullable.GetUnderlyingType(typeof(T)) == null)
//or maybe
else if (!TypeConverterHelper.TryConvertFromString<T>(input, out result))

This issue could happen for other types if allow to convert from string.Empty and return null.
What do you think?


Please upvote 👍 this issue if you are interested in it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⭐ top bugTop bug.bugSomething isn't working

    Type

    No type

    Projects

    Status

    Todo 🕑

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions