elm-validate icon indicating copy to clipboard operation
elm-validate copied to clipboard

Allow validation result type to be different from subject type

Open Unisay opened this issue 5 years ago • 3 comments

I have the following problem with the current design:

It assumes that type of dirty un-validatied input ~~ the type of clean validated output: validation turns Subject into a Valid Subject.

This doesn't play well with custom types that make invalid state non-representable, e.g. Email I want to turn String into Email (not to Valid String)

Which actually makes it a parsing, not validation.

"Parse, don't validate!" is well explained here: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

Unisay avatar Mar 26 '20 09:03 Unisay

I do agree. This is a real problem for me, as elm does not compile the following code:

        postValidator : Validate.Validator Lang.TextIdentifier Model.Model
        postValidator = Validate.firstError
            [ Validate.ifNothing .newPostCategory Lang.PostCategoryUnset
            , Validate.ifBlank .newPostTitle Lang.PostTitleEmpty
            , Validate.ifBlank .newPostBody Lang.PostBodyEmpty
            ]
        validationResult =
            Validate.validate postValidator model |> Result.map Validate.fromValid
    in
        case (validationResult, model.newPostCategory) of
            (Ok data, Just newPostCategory) ->
                Ok <| ValidNewPostData
                    newPostCategory
                    model.newPostTitle
                    model.newPostBody
                    data.selectedTab
            (Err problems, _) -> Err problems
Compiling ...-- MISSING PATTERNS ------------------------------------------------ src/Api.elm

This `case` does not have branches for all possibilities:

239|>        case (validationResult, model.newPostCategory) of
240|>            (Ok data, Just newPostCategory) ->
241|>                Ok <| ValidNewPostData
242|>                    newPostCategory
243|>                    model.newPostTitle
244|>                    model.newPostBody
245|>                    data.selectedTab
246|>            (Err problems, _) -> Err problems

Missing possibilities include:

    ( Ok _, Nothing )

While the missing possibility is clearly never going to happen.

janwirth avatar Mar 26 '20 17:03 janwirth

@Unisay I switched to stoeffel/elm-validate.

janwirth avatar Mar 26 '20 18:03 janwirth

In the meantime I wrote it myself:

https://github.com/tricycle/elm-parse-dont-validate

Unisay avatar Mar 26 '20 19:03 Unisay