diff --git a/part5/elm-package.json b/part5/elm-package.json index 2a97361..7c979f1 100644 --- a/part5/elm-package.json +++ b/part5/elm-package.json @@ -19,7 +19,7 @@ "evancz/url-parser": "2.0.1 <= v < 3.0.0", "lukewestby/elm-http-builder": "5.1.0 <= v < 6.0.0", "mgold/elm-date-format": "1.3.0 <= v < 2.0.0", - "rtfeldman/elm-validate": "2.0.0 <= v < 3.0.0", + "rtfeldman/elm-validate": "3.0.0 <= v < 4.0.0", "rtfeldman/selectlist": "1.0.0 <= v < 2.0.0" }, "elm-version": "0.18.0 <= v < 0.19.0" diff --git a/part5/elm-stuff/exact-dependencies.json b/part5/elm-stuff/exact-dependencies.json index 0298d1a..77b48b4 100644 --- a/part5/elm-stuff/exact-dependencies.json +++ b/part5/elm-stuff/exact-dependencies.json @@ -1,5 +1,5 @@ { - "rtfeldman/elm-validate": "2.0.0", + "rtfeldman/elm-validate": "3.0.0", "rtfeldman/selectlist": "1.0.0", "elm-lang/navigation": "2.1.0", "elm-lang/virtual-dom": "2.0.4", diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/.gitignore b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/.gitignore similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/.gitignore rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/.gitignore diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/CHANGELOG.md b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/CHANGELOG.md similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/CHANGELOG.md rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/CHANGELOG.md diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/LICENSE b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/LICENSE similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/LICENSE rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/LICENSE diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/README.md b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/README.md similarity index 86% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/README.md rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/README.md index be1baf7..423e1b7 100644 --- a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/README.md +++ b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/README.md @@ -16,7 +16,7 @@ type alias Model = { name : String, email : String, age : String, selections : List String } -modelValidator : Validator Model String +modelValidator : Validator String Model modelValidator = Validate.all [ ifBlank .name "Please enter a name." @@ -28,8 +28,7 @@ modelValidator = validate modelValidator { name = "Sam", email = "", age = "abc", selections = [ "cats" ] } - == [ "Please enter an email address.", "Age must be a whole number." ] - --> True + --> [ "Please enter an email address.", "Age must be a whole number." ] ``` You can represent your errors however you like. One nice approach is to use @@ -56,8 +55,7 @@ type alias Model = validate modelValidator { name = "Sam", email = "", age = "abc", selections = [ "cats" ] } - == [ ( Email, "Please enter an email address." ) - , ( Age, "Age must be a whole number." ) - ] - --> True + --> [ ( Email, "Please enter an email address." ) + --> , ( Age, "Age must be a whole number." ) + --> ] ``` diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/elm-package.json b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/elm-package.json similarity index 94% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/elm-package.json rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/elm-package.json index 395c724..3afc2ea 100644 --- a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/elm-package.json +++ b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/elm-package.json @@ -1,5 +1,5 @@ { - "version": "2.0.0", + "version": "3.0.0", "summary": "Convenience functions for validating data.", "repository": "https://github.com/rtfeldman/elm-validate.git", "license": "BSD-3-Clause", diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/examples/SignupForm.elm b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/examples/SignupForm.elm similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/examples/SignupForm.elm rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/examples/SignupForm.elm diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/examples/elm-package.json b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/examples/elm-package.json similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/examples/elm-package.json rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/examples/elm-package.json diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/src/Validate.elm b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/src/Validate.elm similarity index 75% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/src/Validate.elm rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/src/Validate.elm index 211abfa..4d1b4a1 100644 --- a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/src/Validate.elm +++ b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/src/Validate.elm @@ -4,6 +4,7 @@ module Validate , all , any , firstError + , fromErrors , ifBlank , ifEmptyDict , ifEmptyList @@ -21,7 +22,7 @@ module Validate {-| Convenience functions for validating data. - import Validate exposing (ifBlank, ifNotInt, validate) + import Validate exposing (Validator, ifBlank, ifNotInt, validate) type Field = Name | Email | Age @@ -49,7 +50,7 @@ module Validate # Creating validators -@docs ifBlank, ifNotInt, ifEmptyList, ifEmptyDict, ifEmptySet, ifNothing, ifInvalidEmail, ifTrue, ifFalse +@docs ifBlank, ifNotInt, ifEmptyList, ifEmptyDict, ifEmptySet, ifNothing, ifInvalidEmail, ifTrue, ifFalse, fromErrors # Combining validators @@ -86,7 +87,7 @@ type Validator error subject {-| Return an error if the given predicate returns `True` for the given subject. - import Validate exposing (ifBlank, ifNotInt, validate) + import Validate exposing (Validator, ifBlank, ifNotInt, validate) type Field = Name | Email | Age @@ -116,7 +117,7 @@ validate (Validator getErrors) subject = {-| Return an error if the given `String` is empty, or if it contains only whitespace characters. - import Validate exposing (ifBlank, ifNotInt) + import Validate exposing (Validator, ifBlank) modelValidator : Validator Model String modelValidator = @@ -132,10 +133,31 @@ ifBlank subjectToString error = {-| Return an error if the given `String` cannot be parsed as an `Int`. + + import Validate exposing (Validator, ifNotInt) + + modelValidator : Validator Model String + modelValidator = + Validate.all + [ ifNotInt .followers (\_ -> "Please enter a whole number for followers.") + , ifNotInt .stars (\stars -> "Stars was \"" ++ stars ++ "\", but it needs to be a whole number.")" + ] + -} -ifNotInt : (subject -> String) -> error -> Validator error subject -ifNotInt subjectToString error = - ifFalse (\subject -> isInt (subjectToString subject)) error +ifNotInt : (subject -> String) -> (String -> error) -> Validator error subject +ifNotInt subjectToString errorFromString = + let + getErrors subject = + let + str = + subjectToString subject + in + if isInt str then + [] + else + [ errorFromString str ] + in + Validator getErrors {-| Return an error if a `List` is empty. @@ -167,16 +189,65 @@ ifNothing subjectToMaybe error = {-| Return an error if an email address is malformed. + + import Validate exposing (Validator, ifBlank, ifNotInt) + + modelValidator : Validator Model String + modelValidator = + Validate.all + [ ifInvalidEmail .primaryEmail (\_ -> "Please enter a valid primary email address.") + , ifInvalidEmail .superSecretEmail (\email -> "Unfortunately, \"" ++ email ++ "\" is not a valid Super Secret Email Address.") + ] + -} -ifInvalidEmail : (subject -> String) -> error -> Validator error subject -ifInvalidEmail subjectToEmail error = - ifFalse (\subject -> isValidEmail (subjectToEmail subject)) error +ifInvalidEmail : (subject -> String) -> (String -> error) -> Validator error subject +ifInvalidEmail subjectToEmail errorFromEmail = + let + getErrors subject = + let + email = + subjectToEmail subject + in + if isValidEmail email then + [] + else + [ errorFromEmail email ] + in + Validator getErrors + + +{-| Create a custom validator, by providing a function that returns a list of +errors given a subject. + + import Validate exposing (Validator, fromErrors) + + modelValidator : Validator Model String + modelValidator = + fromErrors modelToErrors + + modelToErrors : Model -> List String + modelToErrors model = + let + usernameLength = + String.length model.username + in + if usernameLength < minUsernameChars then + [ "Username not long enough" ] + else if usernameLength > maxUsernameChars then + [ "Username too long" ] + else + [] + +-} +fromErrors : (subject -> List error) -> Validator error subject +fromErrors toErrors = + Validator toErrors {-| Return an error if a predicate returns `True` for the given subject. - import Validate exposing (ifTrue) + import Validate exposing (Validator, ifTrue) modelValidator : Validator Model String modelValidator = @@ -199,7 +270,7 @@ ifTrue test error = {-| Return an error if a predicate returns `False` for the given subject. - import Validate exposing (ifFalse) + import Validate exposing (Validator, ifFalse) modelValidator : Validator Model String modelValidator = @@ -226,7 +297,7 @@ ifFalse test error = {-| Run each of the given validators, in order, and return their concatenated error lists. - import Validate exposing (ifBlank, ifNotInt) + import Validate exposing (Validator, ifBlank, ifNotInt) modelValidator : Validator Model String modelValidator = @@ -253,7 +324,7 @@ all validators = {-| Run each of the given validators, in order, stopping after the first error and returning it. If no errors are encountered, return `Nothing`. - import Validate exposing (ifBlank, ifInvalidEmail, ifNotInt) + import Validate exposing (Validator, ifBlank, ifInvalidEmail, ifNotInt) type alias Model = @@ -337,7 +408,7 @@ isBlank str = Regex.contains lacksNonWhitespaceChars str -{-| Returns `True` if the email is malformed. +{-| Returns `True` if the email is valid. [`ifInvalidEmail`](#ifInvalidEmail) uses this under the hood. diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/ValidateTests.elm b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/ValidateTests.elm similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/ValidateTests.elm rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/ValidateTests.elm diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/elm-package.json b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/elm-package.json similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/elm-package.json rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/elm-package.json diff --git a/part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/elm-verify-examples.json b/part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/elm-verify-examples.json similarity index 100% rename from part5/elm-stuff/packages/rtfeldman/elm-validate/2.0.0/tests/elm-verify-examples.json rename to part5/elm-stuff/packages/rtfeldman/elm-validate/3.0.0/tests/elm-verify-examples.json diff --git a/part5/index.html b/part5/index.html deleted file mode 100644 index 0cfb2fc..0000000 --- a/part5/index.html +++ /dev/null @@ -1,18266 +0,0 @@ - -