Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Unisay opened this issue Mar 26, 2020 · 3 comments
Open

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

Unisay opened this issue Mar 26, 2020 · 3 comments

Comments

@Unisay
Copy link

Unisay commented Mar 26, 2020

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
Copy link

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
Copy link

@Unisay I switched to stoeffel/elm-validate.

@Unisay
Copy link
Author

Unisay commented Mar 26, 2020

In the meantime I wrote it myself:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants