Update part1 elm-stuff and README
This commit is contained in:
@@ -1,7 +1,14 @@
|
|||||||
# Part 1
|
# Part 1
|
||||||
|
|
||||||
To build everything:
|
To build everything, `cd` into the `part1/` directory and run:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
elm-live src/Main.elm --output=../server/public/elm.js --dir=../server/public --pushstate
|
||||||
```
|
```
|
||||||
elm make --output=../server/public/elm.js src/Main.elm --pushstate --open
|
|
||||||
```
|
Then open [http://localhost:3000](http://localhost:3000) in your browser.
|
||||||
|
|
||||||
|
## Exercise
|
||||||
|
|
||||||
|
Open `src/Page/Home.elm` in your editor and resolve the TODOs there.
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type alias Model =
|
|||||||
{ name : String, email : String, age : String, selections : List String }
|
{ name : String, email : String, age : String, selections : List String }
|
||||||
|
|
||||||
|
|
||||||
modelValidator : Validator Model String
|
modelValidator : Validator String Model
|
||||||
modelValidator =
|
modelValidator =
|
||||||
Validate.all
|
Validate.all
|
||||||
[ ifBlank .name "Please enter a name."
|
[ ifBlank .name "Please enter a name."
|
||||||
@@ -28,8 +28,7 @@ modelValidator =
|
|||||||
|
|
||||||
validate modelValidator
|
validate modelValidator
|
||||||
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
|
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
|
||||||
== [ "Please enter an email address.", "Age must be a whole number." ]
|
--> [ "Please enter an email address.", "Age must be a whole number." ]
|
||||||
--> True
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You can represent your errors however you like. One nice approach is to use
|
You can represent your errors however you like. One nice approach is to use
|
||||||
@@ -56,8 +55,7 @@ type alias Model =
|
|||||||
|
|
||||||
validate modelValidator
|
validate modelValidator
|
||||||
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
|
{ name = "Sam", email = "", age = "abc", selections = [ "cats" ] }
|
||||||
== [ ( Email, "Please enter an email address." )
|
--> [ ( Email, "Please enter an email address." )
|
||||||
, ( Age, "Age must be a whole number." )
|
--> , ( Age, "Age must be a whole number." )
|
||||||
]
|
--> ]
|
||||||
--> True
|
|
||||||
```
|
```
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"version": "2.0.0",
|
"version": "3.0.0",
|
||||||
"summary": "Convenience functions for validating data.",
|
"summary": "Convenience functions for validating data.",
|
||||||
"repository": "https://github.com/rtfeldman/elm-validate.git",
|
"repository": "https://github.com/rtfeldman/elm-validate.git",
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
@@ -4,6 +4,7 @@ module Validate
|
|||||||
, all
|
, all
|
||||||
, any
|
, any
|
||||||
, firstError
|
, firstError
|
||||||
|
, fromErrors
|
||||||
, ifBlank
|
, ifBlank
|
||||||
, ifEmptyDict
|
, ifEmptyDict
|
||||||
, ifEmptyList
|
, ifEmptyList
|
||||||
@@ -21,7 +22,7 @@ module Validate
|
|||||||
|
|
||||||
{-| Convenience functions for validating data.
|
{-| Convenience functions for validating data.
|
||||||
|
|
||||||
import Validate exposing (ifBlank, ifNotInt, validate)
|
import Validate exposing (Validator, ifBlank, ifNotInt, validate)
|
||||||
|
|
||||||
type Field = Name | Email | Age
|
type Field = Name | Email | Age
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ module Validate
|
|||||||
|
|
||||||
# Creating validators
|
# 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
|
# Combining validators
|
||||||
@@ -86,7 +87,7 @@ type Validator error subject
|
|||||||
{-| Return an error if the given predicate returns `True` for the given
|
{-| Return an error if the given predicate returns `True` for the given
|
||||||
subject.
|
subject.
|
||||||
|
|
||||||
import Validate exposing (ifBlank, ifNotInt, validate)
|
import Validate exposing (Validator, ifBlank, ifNotInt, validate)
|
||||||
|
|
||||||
type Field = Name | Email | Age
|
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
|
{-| Return an error if the given `String` is empty, or if it contains only
|
||||||
whitespace characters.
|
whitespace characters.
|
||||||
|
|
||||||
import Validate exposing (ifBlank, ifNotInt)
|
import Validate exposing (Validator, ifBlank)
|
||||||
|
|
||||||
modelValidator : Validator Model String
|
modelValidator : Validator Model String
|
||||||
modelValidator =
|
modelValidator =
|
||||||
@@ -132,10 +133,31 @@ ifBlank subjectToString error =
|
|||||||
|
|
||||||
|
|
||||||
{-| Return an error if the given `String` cannot be parsed as an `Int`.
|
{-| 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 : (subject -> String) -> (String -> error) -> Validator error subject
|
||||||
ifNotInt subjectToString error =
|
ifNotInt subjectToString errorFromString =
|
||||||
ifFalse (\subject -> isInt (subjectToString subject)) error
|
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.
|
{-| Return an error if a `List` is empty.
|
||||||
@@ -167,16 +189,65 @@ ifNothing subjectToMaybe error =
|
|||||||
|
|
||||||
|
|
||||||
{-| Return an error if an email address is malformed.
|
{-| 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 : (subject -> String) -> (String -> error) -> Validator error subject
|
||||||
ifInvalidEmail subjectToEmail error =
|
ifInvalidEmail subjectToEmail errorFromEmail =
|
||||||
ifFalse (\subject -> isValidEmail (subjectToEmail subject)) error
|
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
|
{-| Return an error if a predicate returns `True` for the given
|
||||||
subject.
|
subject.
|
||||||
|
|
||||||
import Validate exposing (ifTrue)
|
import Validate exposing (Validator, ifTrue)
|
||||||
|
|
||||||
modelValidator : Validator Model String
|
modelValidator : Validator Model String
|
||||||
modelValidator =
|
modelValidator =
|
||||||
@@ -199,7 +270,7 @@ ifTrue test error =
|
|||||||
{-| Return an error if a predicate returns `False` for the given
|
{-| Return an error if a predicate returns `False` for the given
|
||||||
subject.
|
subject.
|
||||||
|
|
||||||
import Validate exposing (ifFalse)
|
import Validate exposing (Validator, ifFalse)
|
||||||
|
|
||||||
modelValidator : Validator Model String
|
modelValidator : Validator Model String
|
||||||
modelValidator =
|
modelValidator =
|
||||||
@@ -226,7 +297,7 @@ ifFalse test error =
|
|||||||
{-| Run each of the given validators, in order, and return their concatenated
|
{-| Run each of the given validators, in order, and return their concatenated
|
||||||
error lists.
|
error lists.
|
||||||
|
|
||||||
import Validate exposing (ifBlank, ifNotInt)
|
import Validate exposing (Validator, ifBlank, ifNotInt)
|
||||||
|
|
||||||
modelValidator : Validator Model String
|
modelValidator : Validator Model String
|
||||||
modelValidator =
|
modelValidator =
|
||||||
@@ -253,7 +324,7 @@ all validators =
|
|||||||
{-| Run each of the given validators, in order, stopping after the first error
|
{-| Run each of the given validators, in order, stopping after the first error
|
||||||
and returning it. If no errors are encountered, return `Nothing`.
|
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 =
|
type alias Model =
|
||||||
@@ -337,7 +408,7 @@ isBlank str =
|
|||||||
Regex.contains lacksNonWhitespaceChars 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.
|
[`ifInvalidEmail`](#ifInvalidEmail) uses this under the hood.
|
||||||
|
|
||||||
Reference in New Issue
Block a user