@@ -10,74 +10,124 @@ import Data.Traversable (traverse)
10
10
import Data.Validation.Semigroup (V , unV , invalid )
11
11
import Partial.Unsafe (unsafePartial )
12
12
13
+ -- ---------------
14
+ -- Some simple early examples returning `Either` instead of `V`:
15
+
16
+ -- ANCHOR: nonEmpty1
17
+ nonEmpty1 :: String -> Either String Unit
18
+ nonEmpty1 " " = Left " Field cannot be empty"
19
+ nonEmpty1 _ = Right unit
20
+ -- ANCHOR_END: nonEmpty1
21
+
22
+ -- ANCHOR: validatePerson1
23
+ validatePerson1 :: Person -> Either String Person
24
+ validatePerson1 p =
25
+ person <$> (nonEmpty1 p.firstName *> pure p.firstName)
26
+ <*> (nonEmpty1 p.lastName *> pure p.lastName)
27
+ <*> pure p.homeAddress
28
+ <*> pure p.phones
29
+ -- ANCHOR_END: validatePerson1
30
+
31
+ -- ANCHOR: validatePerson1Ado
32
+ validatePerson1Ado :: Person -> Either String Person
33
+ validatePerson1Ado p = ado
34
+ f <- nonEmpty1 p.firstName *> pure p.firstName
35
+ l <- nonEmpty1 p.lastName *> pure p.firstName
36
+ in person f l p.homeAddress p.phones
37
+ -- ANCHOR_END: validatePerson1Ado
38
+
39
+ -- ---------------
40
+
41
+ -- ANCHOR: Errors
13
42
type Errors
14
43
= Array String
44
+ -- ANCHOR_END: Errors
15
45
46
+ -- ANCHOR: nonEmpty
16
47
nonEmpty :: String -> String -> V Errors Unit
17
48
nonEmpty field " " = invalid [ " Field '" <> field <> " ' cannot be empty" ]
49
+ nonEmpty _ _ = pure unit
50
+ -- ANCHOR_END: nonEmpty
18
51
19
- nonEmpty _ _ = pure unit
20
-
52
+ -- ANCHOR: arrayNonEmpty
21
53
arrayNonEmpty :: forall a . String -> Array a -> V Errors Unit
22
- arrayNonEmpty field [] = invalid [ " Field '" <> field <> " ' must contain at least one value" ]
23
-
24
- arrayNonEmpty _ _ = pure unit
54
+ arrayNonEmpty field [] =
55
+ invalid [ " Field '" <> field <> " ' must contain at least one value" ]
56
+ arrayNonEmpty _ _ =
57
+ pure unit
58
+ -- ANCHOR_END: arrayNonEmpty
25
59
60
+ -- ANCHOR: lengthIs
26
61
lengthIs :: String -> Int -> String -> V Errors Unit
27
- lengthIs field len value
28
- | length value /= len = invalid [ " Field '" <> field <> " ' must have length " <> show len ]
29
-
30
- lengthIs _ _ _ = pure unit
62
+ lengthIs field len value | length value /= len =
63
+ invalid [ " Field '" <> field <> " ' must have length " <> show len ]
64
+ lengthIs _ _ _ = pure unit
65
+ -- ANCHOR_END: lengthIs
31
66
67
+ -- ANCHOR: phoneNumberRegex
32
68
phoneNumberRegex :: Regex
33
69
phoneNumberRegex =
34
70
unsafePartial case regex " ^\\ d{3}-\\ d{3}-\\ d{4}$" noFlags of
35
71
Right r -> r
72
+ -- ANCHOR_END: phoneNumberRegex
36
73
74
+ -- ANCHOR: matches
37
75
matches :: String -> Regex -> String -> V Errors Unit
38
- matches _ regex value
39
- | test regex value = pure unit
40
-
41
- matches field _ _ = invalid [ " Field '" <> field <> " ' did not match the required format" ]
76
+ matches _ regex value | test regex value =
77
+ pure unit
78
+ matches field _ _ =
79
+ invalid [ " Field '" <> field <> " ' did not match the required format" ]
80
+ -- ANCHOR_END: matches
42
81
82
+ -- ANCHOR: validateAddress
43
83
validateAddress :: Address -> V Errors Address
44
84
validateAddress a =
45
- address <$> (nonEmpty " Street" a.street *> pure a.street)
46
- <*> (nonEmpty " City" a.city *> pure a.city)
47
- <*> (lengthIs " State" 2 a.state *> pure a.state)
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)
88
+ -- ANCHOR_END: validateAddress
48
89
90
+ -- ANCHOR: validateAddressAdo
49
91
validateAddressAdo :: Address -> V Errors Address
50
92
validateAddressAdo a = ado
51
- street <- (nonEmpty " Street" a.street *> pure a.street)
52
- city <- (nonEmpty " City" a.city *> pure a.city)
53
- state <- (lengthIs " State" 2 a.state *> pure a.state)
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)
54
96
in address street city state
97
+ -- ANCHOR_END: validateAddressAdo
55
98
99
+ -- ANCHOR: validatePhoneNumber
56
100
validatePhoneNumber :: PhoneNumber -> V Errors PhoneNumber
57
101
validatePhoneNumber pn =
58
102
phoneNumber <$> pure pn." type"
59
- <*> (matches " Number" phoneNumberRegex pn.number *> pure pn.number)
103
+ <*> (matches " Number" phoneNumberRegex pn.number *> pure pn.number)
104
+ -- ANCHOR_END: validatePhoneNumber
60
105
106
+ -- ANCHOR: validatePhoneNumberAdo
61
107
validatePhoneNumberAdo :: PhoneNumber -> V Errors PhoneNumber
62
108
validatePhoneNumberAdo pn = ado
63
- tpe <- pure pn." type"
109
+ tpe <- pure pn." type"
64
110
number <- (matches " Number" phoneNumberRegex pn.number *> pure pn.number)
65
111
in phoneNumber tpe number
112
+ -- ANCHOR_END: validatePhoneNumberAdo
66
113
114
+ -- ANCHOR: validatePerson
67
115
validatePerson :: Person -> V Errors Person
68
116
validatePerson p =
69
117
person <$> (nonEmpty " First Name" p.firstName *> pure p.firstName)
70
- <*> (nonEmpty " Last Name" p.lastName *> pure p.lastName)
71
- <*> validateAddress p.homeAddress
72
- <*> (arrayNonEmpty " Phone Numbers" p.phones *> traverse validatePhoneNumber p.phones)
118
+ <*> (nonEmpty " Last Name" p.lastName *> pure p.lastName)
119
+ <*> validateAddress p.homeAddress
120
+ <*> (arrayNonEmpty " Phone Numbers" p.phones *>
121
+ traverse validatePhoneNumber p.phones)
122
+ -- ANCHOR_END: validatePerson
73
123
124
+ -- ANCHOR: validatePersonAdo
74
125
validatePersonAdo :: Person -> V Errors Person
75
126
validatePersonAdo p = ado
76
127
firstName <- (nonEmpty " First Name" p.firstName *> pure p.firstName)
77
- lastName <- (nonEmpty " Last Name" p.lastName *> pure p.lastName)
78
- address <- validateAddress p.homeAddress
79
- numbers <- (arrayNonEmpty " Phone Numbers" p.phones *> traverse validatePhoneNumber p.phones)
128
+ lastName <- (nonEmpty " Last Name" p.lastName *> pure p.lastName)
129
+ address <- validateAddress p.homeAddress
130
+ numbers <- (arrayNonEmpty " Phone Numbers" p.phones *>
131
+ traverse validatePhoneNumber p.phones)
80
132
in person firstName lastName address numbers
81
-
82
- validatePerson' :: Person -> Either Errors Person
83
- validatePerson' p = unV Left Right $ validatePerson p
133
+ -- ANCHOR_END: validatePersonAdo
0 commit comments