-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSemigroup.purs
205 lines (167 loc) · 8.1 KB
/
Semigroup.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
module Semigroup where
import Prelude
import Control.Monad.Eff as Eff
import Control.Monad.Eff.Console as Eff.Console
import Data.Array as Array
import Data.Bifunctor as Bifunctor
import Data.Either as Either
import Data.Generic.Rep as Generic
import Data.Generic.Rep.Show as Generic.Show
import Data.List.NonEmpty as NonEmptyList
import Data.String as String
import Data.String.Regex as Regex
import Data.String.Regex.Flags as Regex.Flags
import Data.Validation.Semigroup as Validation
import Global.Unsafe as Unsafe.Global
import Partial.Unsafe as Partial
--------------------------------------------------------------------------------
-- | Utility function to unsafely construct a regular expression from a pattern
-- | string.
-- |
-- | This will fail at runtime with an error if the pattern string is invalid.
unsafeRegexFromString :: String -> Regex.Regex
unsafeRegexFromString str =
let regex = Regex.regex str Regex.Flags.noFlags
in Partial.unsafePartial (Either.fromRight regex)
--------------------------------------------------------------------------------
-- | Regular expression for email address validation.
emailRegex :: Regex.Regex
emailRegex =
unsafeRegexFromString "^\\w+([.-]?\\w+)*@\\w+([.-]?\\w+)*(\\.\\w{2,3})+$"
-- | Regular expression for special symbols.
passwordRegex :: Regex.Regex
passwordRegex = unsafeRegexFromString "\\W"
-- | Minimum password length.
passwordMinLength :: Int
passwordMinLength = 8
--------------------------------------------------------------------------------
data ValidationError
= EmptyField
| InvalidEmailAddress
| NoSpecialCharacter
| LessThanMinLength
-- | Derive a `Generic` instance for `ValidationError` so we can get a
-- | `Show` instance to print to the console.
derive instance genericValidationError :: Generic.Generic ValidationError _
-- | Derive `show` for `ValidationError` using the `Generic` instance.
instance showValidationError :: Show ValidationError where
show = Generic.Show.genericShow
type ValidationErrors = NonEmptyList.NonEmptyList ValidationError
--------------------------------------------------------------------------------
-- | Validate that the field of a form is non-empty.
validateNonEmpty :: String -> Validation.V ValidationErrors String
validateNonEmpty str
| String.null str = Validation.invalid $ NonEmptyList.singleton EmptyField
| otherwise = pure str
-- | Validate that the field of a form is a valid email address.
validateEmailRegex :: String -> Validation.V ValidationErrors String
validateEmailRegex email
| Regex.test emailRegex email = pure email
| otherwise = Validation.invalid $ NonEmptyList.singleton InvalidEmailAddress
-- | Validate that the field of a form has at least one special character.
validatePasswordRegex :: String -> Validation.V ValidationErrors String
validatePasswordRegex password
| Regex.test passwordRegex password = pure password
| otherwise = Validation.invalid $ NonEmptyList.singleton NoSpecialCharacter
-- | Validate that the field of a form is longer than `passwordMinLength`.
validatePasswordMinLength :: String -> Validation.V ValidationErrors String
validatePasswordMinLength password
| String.length password > passwordMinLength = pure password
| otherwise = Validation.invalid $ NonEmptyList.singleton LessThanMinLength
--------------------------------------------------------------------------------
-- | Sum type containing errors we could potentially encounter while validating
-- | the form.
data FormErrorF a
= BadEmail a
| BadPassword a
-- | Derive a `Functor` instance for `FormErrorF` so we can `map` into it.
derive instance functorFormErrorF :: Functor FormErrorF
-- | Derive a `Generic` instance for `FormErrorF` so we can get a
-- | `Show` instance to print to the console.
derive instance genericFormErrorF :: Generic.Generic (FormErrorF a) _
-- | Derive `show` for `FormErrorF` using the `Generic` instance.
instance showFormErrorF :: Show a => Show (FormErrorF a) where
show = Generic.Show.genericShow
-- | Type alias for a simple `FormError`, containing only `ValidationErrors`.
type FormError = FormErrorF ValidationErrors
-- | Type alias for a non-empty list of `FormError`s.
type FormErrors = NonEmptyList.NonEmptyList FormError
--------------------------------------------------------------------------------
-- | Newtype wrapper for a form's email field
newtype Email = Email String
-- | Validate that the field of a form is non-empty and has a valid email
-- | address.
validateEmail :: String -> Validation.V FormError Email
validateEmail email =
Bifunctor.bimap BadEmail Email
$ validateNonEmpty email
*> validateEmailRegex email
-- | Newtype wrapper for a form's password field
newtype Password = Password String
-- | Validate that the field of a form is non-empty, has at least one special
-- | character, and is longer than `passwordMinLength`.
validatePassword :: String -> Validation.V FormError Password
validatePassword password =
Bifunctor.bimap BadPassword Password
$ validateNonEmpty password
*> validatePasswordRegex password
*> validatePasswordMinLength password
--------------------------------------------------------------------------------
-- | Type alias for an unvalidated version of our simple form, note how the
-- | email and password fields are simple strings.
type UnvalidatedForm =
{ email :: String
, password :: String
}
-- | Type alias for a validated version of our simple form, note how the email
-- | and password fields are wrapped in newtypes.
type ValidatedForm =
{ email :: Email
, password :: Password
}
-- | Validate that a form contains a valid email and a valid password.
validateForm :: UnvalidatedForm -> Validation.V FormErrors ValidatedForm
validateForm {email, password} = {email: _, password: _}
<$> (Bifunctor.lmap NonEmptyList.singleton $ validateEmail email)
<*> (Bifunctor.lmap NonEmptyList.singleton $ validatePassword password)
--------------------------------------------------------------------------------
-- | An empty form; this will parse as invalid.
testForm1 :: UnvalidatedForm
testForm1 = {email: "", password: ""}
-- | A form with a bad email and a bad password; invalid.
testForm2 :: UnvalidatedForm
testForm2 = {email: "bademail", password: "badpassword"}
-- | A form with a good email and a bad password; invalid.
testForm3 :: UnvalidatedForm
testForm3 = {email: "[email protected]", password: "badpassword"}
-- | A form with a good email and a password that is too short; invalid.
testForm4 :: UnvalidatedForm
testForm4 = {email: "[email protected]", password: "abc123+"}
-- | A form with a good email and a good password; valid.
testForm5 :: UnvalidatedForm
testForm5 = {email: "[email protected]", password: "abc123+-="}
--------------------------------------------------------------------------------
-- | Run a form validation against all of the test forms we created, formatting
-- | the output and printing it to the console.
main :: ∀ e. Eff.Eff (console :: Eff.Console.CONSOLE | e) Unit
main = do
Eff.Console.logShow $ formatValidationOutput $ validateForm testForm1
-- >(Invalid [(BadEmail [EmptyField,InvalidEmailAddress]),(BadPassword [EmptyField,NoSpecialCharacter,LessThanMinLength])])
Eff.Console.logShow $ formatValidationOutput $ validateForm testForm2
-- > (Invalid [(BadEmail [InvalidEmailAddress]),(BadPassword [NoSpecialCharacter])])
Eff.Console.logShow $ formatValidationOutput $ validateForm testForm3
-- > (Invalid [(BadPassword [NoSpecialCharacter])])
Eff.Console.logShow $ formatValidationOutput $ validateForm testForm4
-- > (Invalid [(BadPassword [LessThanMinLength])])
Eff.Console.logShow $ formatValidationOutput $ validateForm testForm5
-- > (Valid "{\"email\":\"[email protected]\",\"password\":\"abc123+-=\"}")
where
-- Format the output of our validator.
formatValidationOutput =
Bifunctor.bimap
-- Convert the `NonEmptyList` of `ValidationError` to an `Array`, eliminate any
-- duplicate validation errors, and convert the `NonEmptyList` of `FormError`s
-- to an `Array` too for easier printing
(Array.fromFoldable <<< ((map <<< map) (Array.fromFoldable)))
-- Unsafe stringify the record, in lieu of a `Show` instance.
(Unsafe.Global.unsafeStringify)