Search code examples
c#error-handlingfunctional-programminglanguage-extlanguage-extension

Language-Ext- How do i get just the error message from an Error Type from Validation<Error,T>


In using LanguageExt, I am trying to display just the error message of a failed validation from using Validation<Error,Unit> in LanguageExt.

However, I get the following string:

ValidationData(Fail, 0, [Starting number is not a valid number])

I can't seem to find a way to get just the error message which is

Starting number is not a valid number

Can anyone provide any advice on how to do this?

Here's a snippet of the code

ValidateStartingNumberError(int number)
    .Match(
        () => None,
        error => DisplayError(error.ToString())
    );

private Validation<Error, Unit> ValidateStartingNumberError(int number)
    => int.TryParse(this.number, out var parsedNumber) ?
         unit :
         Fail<Error,Unit>("Starting number is not a valid number");

private void DisplayError(string message)=> MessageBox.Show(message);

Solution

  • Found the reason: I implemented the wrong .Match(...). It should be

    ValidateStartingNumberError(int number)
        .Match(
            _ => None,
            error => DisplayError(error.Head.Message)
        );