rtfeldman / elm-validate

Convenience functions for validating Elm data.
http://package.elm-lang.org/packages/rtfeldman/elm-validate/latest
BSD 3-Clause "New" or "Revised" License
145 stars 28 forks source link

Allow validation result type to be different from subject type #44

Open Unisay opened 4 years ago

Unisay commented 4 years ago

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/

janwirth commented 4 years ago

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 commented 4 years ago

@Unisay I switched to stoeffel/elm-validate.

Unisay commented 4 years ago

In the meantime I wrote it myself:

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