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

TYPE MISMATCH ifInvalidEmail #32

Closed taj closed 5 years ago

taj commented 5 years ago

I get an error when trying to implement the ifInvalidEmail validator.

Here's my code (that I copied from the Sign Up Form Example):

type alias Form =
    { email : String
    , password : String
    }

type Field
    = Email
    | Password

formValidator : Validator ( Field, String ) Form
formValidator =
    Validate.all
        [ Validate.firstError
            [ ifBlank .email ( Email, "Please enter an email address." )
            , ifInvalidEmail .email ( Email, "This is not a valid email address." )
            ]
        , ifBlank .password ( Password, "Please enter a password." )
        ]

and the error is:

The 2nd argument to `ifInvalidEmail` is not what I expect:

129|             , ifInvalidEmail .email ( Email, "This is not a valid email address." )
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This argument is a tuple of type:

    ( Field, String )

But `ifInvalidEmail` needs the 2nd argument to be:

    String -> error

Same thing happens for ifNotIntvalidators.

thiloplanz commented 5 years ago

README.md says:

Functions the detect something wrong with a value, such as isNotInt and isInvalidEmail, accept a function as the second argument so you can include the incorrect value in the error message:

modelValidator : Validator ( Field, String ) Model
modelValidator =
    Validate.all
        [ ifNotInt .count (\value -> ( Count, value ++ " is not an integer." ))
        , ifInvalidEmail .email (\value -> ( Email, value ++ " is not a valid email address." ))
        ]

So you need to pass a function that takes the incorrect value and returns your tuple. (It can ignore that value, but you have to make a function).

taj commented 5 years ago

Thanks @thiloplanz

I will make a pull request to fix the documentation!

taj commented 5 years ago

I've created a Pull Request to correct the documentation and the Sign Up Form example.

Rolograaf commented 5 years ago

I tried the example examples/SignupForm.elm in Ellie but got a bunch of errors, does this have the same reason that the example might not be updated to latest revision?

https://ellie-app.com/474hyJ4MKTya1

if so, what should the validate be like than?

result : Bool
result =
    validate modelValidator
        { name = "Sam", email = "", age = "abc", selections = [ 1.2 ] }
        == [ ( Email, "Please enter an email address." )
           , ( Age, "Age must be a whole number." )
           ]

gives error

The left side of (==) is:

    Result (List ( Field, String )) (Validate.Valid Model)

But the right side is:

    List ( Field, String )

Different types can never be equal though! Which side is messed up?
taj commented 5 years ago

The documentaion is not updated to the latest release:

change you're code to: ifInvalidEmail .email (\_ -> ( Email, "This is not a valid email address.") ) and ifNotInt .age ( \_ -> ( Age, "Age must be a whole number. )" )

to fix it