@@ -7,32 +7,32 @@ import Data.String (length)
77import Data.String.Regex (Regex , test , regex )
88import Data.String.Regex.Flags (noFlags )
99import Data.Traversable (traverse )
10- import Data.Validation.Semigroup (V , unV , invalid )
10+ import Data.Validation.Semigroup (V , invalid )
1111import Partial.Unsafe (unsafePartial )
1212
1313-- ---------------
1414-- Some simple early examples returning `Either` instead of `V`:
1515
1616-- ANCHOR: nonEmpty1
17- nonEmpty1 :: String -> Either String Unit
18- nonEmpty1 " " = Left " Field cannot be empty"
19- nonEmpty1 _ = Right unit
17+ nonEmpty1 :: String -> Either String String
18+ nonEmpty1 " " = Left " Field cannot be empty"
19+ nonEmpty1 value = Right value
2020-- ANCHOR_END: nonEmpty1
2121
2222-- ANCHOR: validatePerson1
2323validatePerson1 :: Person -> Either String Person
2424validatePerson1 p =
25- person <$> ( nonEmpty1 p.firstName *> pure p.firstName)
26- <*> ( nonEmpty1 p.lastName *> pure p.lastName)
25+ person <$> nonEmpty1 p.firstName
26+ <*> nonEmpty1 p.lastName
2727 <*> pure p.homeAddress
2828 <*> pure p.phones
2929-- ANCHOR_END: validatePerson1
3030
3131-- ANCHOR: validatePerson1Ado
3232validatePerson1Ado :: Person -> Either String Person
3333validatePerson1Ado p = ado
34- f <- nonEmpty1 p.firstName *> pure p.firstName
35- l <- nonEmpty1 p.lastName *> pure p.firstName
34+ f <- nonEmpty1 p.firstName
35+ l <- nonEmpty1 p.lastName
3636 in person f l p.homeAddress p.phones
3737-- ANCHOR_END: validatePerson1Ado
3838
@@ -44,24 +44,24 @@ type Errors
4444-- ANCHOR_END: Errors
4545
4646-- ANCHOR: nonEmpty
47- nonEmpty :: String -> String -> V Errors Unit
48- nonEmpty field " " = invalid [ " Field '" <> field <> " ' cannot be empty" ]
49- nonEmpty _ _ = pure unit
47+ nonEmpty :: String -> String -> V Errors String
48+ nonEmpty field " " = invalid [ " Field '" <> field <> " ' cannot be empty" ]
49+ nonEmpty _ value = pure value
5050-- ANCHOR_END: nonEmpty
5151
52- -- ANCHOR: arrayNonEmpty
53- arrayNonEmpty :: forall a . String -> Array a -> V Errors Unit
54- arrayNonEmpty field [] =
52+ -- ANCHOR: validatePhoneNumbers
53+ validatePhoneNumbers :: String -> Array PhoneNumber -> V Errors ( Array PhoneNumber )
54+ validatePhoneNumbers field [] =
5555 invalid [ " Field '" <> field <> " ' must contain at least one value" ]
56- arrayNonEmpty _ _ =
57- pure unit
58- -- ANCHOR_END: arrayNonEmpty
56+ validatePhoneNumbers _ phones =
57+ traverse validatePhoneNumber phones
58+ -- ANCHOR_END: validatePhoneNumbers
5959
6060-- ANCHOR: lengthIs
61- lengthIs :: String -> Int -> String -> V Errors Unit
61+ lengthIs :: String -> Int -> String -> V Errors String
6262lengthIs field len value | length value /= len =
6363 invalid [ " Field '" <> field <> " ' must have length " <> show len ]
64- lengthIs _ _ _ = pure unit
64+ lengthIs _ _ value = pure value
6565-- ANCHOR_END: lengthIs
6666
6767-- ANCHOR: phoneNumberRegex
@@ -72,62 +72,60 @@ phoneNumberRegex =
7272-- ANCHOR_END: phoneNumberRegex
7373
7474-- ANCHOR: matches
75- matches :: String -> Regex -> String -> V Errors Unit
75+ matches :: String -> Regex -> String -> V Errors String
7676matches _ regex value | test regex value =
77- pure unit
77+ pure value
7878matches field _ _ =
7979 invalid [ " Field '" <> field <> " ' did not match the required format" ]
8080-- ANCHOR_END: matches
8181
8282-- ANCHOR: validateAddress
8383validateAddress :: Address -> V Errors Address
8484validateAddress a =
85- address <$> ( nonEmpty " Street" a.street *> pure a.street)
86- <*> ( nonEmpty " City" a.city *> pure a.city)
87- <*> ( lengthIs " State" 2 a.state *> pure a.state)
85+ address <$> nonEmpty " Street" a.street
86+ <*> nonEmpty " City" a.city
87+ <*> lengthIs " State" 2 a.state
8888-- ANCHOR_END: validateAddress
8989
9090-- ANCHOR: validateAddressAdo
9191validateAddressAdo :: Address -> V Errors Address
9292validateAddressAdo a = ado
93- street <- ( nonEmpty " Street" a.street *> pure a.street)
94- city <- ( nonEmpty " City" a.city *> pure a.city)
95- state <- ( lengthIs " State" 2 a.state *> pure a.state)
93+ street <- nonEmpty " Street" a.street
94+ city <- nonEmpty " City" a.city
95+ state <- lengthIs " State" 2 a.state
9696 in address street city state
9797-- ANCHOR_END: validateAddressAdo
9898
9999-- ANCHOR: validatePhoneNumber
100100validatePhoneNumber :: PhoneNumber -> V Errors PhoneNumber
101101validatePhoneNumber pn =
102102 phoneNumber <$> pure pn." type"
103- <*> ( matches " Number" phoneNumberRegex pn.number *> pure pn.number)
103+ <*> matches " Number" phoneNumberRegex pn.number
104104-- ANCHOR_END: validatePhoneNumber
105105
106106-- ANCHOR: validatePhoneNumberAdo
107107validatePhoneNumberAdo :: PhoneNumber -> V Errors PhoneNumber
108108validatePhoneNumberAdo pn = ado
109109 tpe <- pure pn." type"
110- number <- ( matches " Number" phoneNumberRegex pn.number *> pure pn.number)
110+ number <- matches " Number" phoneNumberRegex pn.number
111111 in phoneNumber tpe number
112112-- ANCHOR_END: validatePhoneNumberAdo
113113
114114-- ANCHOR: validatePerson
115115validatePerson :: Person -> V Errors Person
116116validatePerson p =
117- person <$> ( nonEmpty " First Name" p.firstName *> pure p.firstName)
118- <*> ( nonEmpty " Last Name" p.lastName *> pure p.lastName)
117+ person <$> nonEmpty " First Name" p.firstName
118+ <*> nonEmpty " Last Name" p.lastName
119119 <*> validateAddress p.homeAddress
120- <*> (arrayNonEmpty " Phone Numbers" p.phones *>
121- traverse validatePhoneNumber p.phones)
120+ <*> validatePhoneNumbers " Phone Numbers" p.phones
122121-- ANCHOR_END: validatePerson
123122
124123-- ANCHOR: validatePersonAdo
125124validatePersonAdo :: Person -> V Errors Person
126125validatePersonAdo p = ado
127- firstName <- ( nonEmpty " First Name" p.firstName *> pure p.firstName)
128- lastName <- ( nonEmpty " Last Name" p.lastName *> pure p.lastName)
126+ firstName <- nonEmpty " First Name" p.firstName
127+ lastName <- nonEmpty " Last Name" p.lastName
129128 address <- validateAddress p.homeAddress
130- numbers <- (arrayNonEmpty " Phone Numbers" p.phones *>
131- traverse validatePhoneNumber p.phones)
129+ numbers <- validatePhoneNumbers " Phone Numbers" p.phones
132130 in person firstName lastName address numbers
133131-- ANCHOR_END: validatePersonAdo
0 commit comments