Validus icon indicating copy to clipboard operation
Validus copied to clipboard

Allow the input value to be used in creating the error message

Open abelbraaksma opened this issue 2 years ago • 2 comments

I find myself often wanting to output the actual reason or cause of the error, which requires showing (part of) the input value. For instance, when you parse a string to a number, you want to know why it fails. The errors in Validus only ever take a function that takes the field name, and you cannot (readily) access the input data to give more meaningful errors.

As such, I use the following function (better name wanted, though):

/// Create a new Validator with an advanced message
let createAdvanced
    (message : string -> 'a -> string)
    (rule : 'a -> bool)
    : Validator<'a, 'a> =
    fun (field : string) (value : 'a) ->
        if rule value then Ok value
        else 
            [ message field value ]
            |> ValidationErrors.create field 
            |> Error

abelbraaksma avatar Aug 15 '23 12:08 abelbraaksma

I apologize. My life has been super busy. Doing the best I can to manage my OSS.

It's an interesting thought. At times I thought to myself this would be useful as well. Often wishing the library worked this way in general (maybe v5)? But in practice, I find I don't need this as often as I might think and when I do, the raw input is usually in scope. YMMV of course. Open to discussing it further.

pimbrouwers avatar Jan 28 '24 00:01 pimbrouwers

Well, the idea here is to create standard validators for frequent scenarios that can format data from your domain model.

It is not the end of the world to code around it, but oftentimes it is good if the library forces its will on you to do it using a certain standardized method.

Anyway, I appreciate you being busy (I've been away from OSS myself more than I wanted to), no rush for anything, really. Or if we can agree on a design, I might PR it myself for you :).

abelbraaksma avatar May 05 '24 23:05 abelbraaksma