This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Validate.elm
502 lines (363 loc) · 12 KB
/
Validate.elm
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
module Form.Validate exposing
( Validation, field, map, succeed, andThen, andMap, customError, defaultValue, mapError, withCustomError, sequence
, map2, map3, map4, map5, map6, map7, map8
, list, string, int, float, bool, maybe, email, emptyString
, minInt, maxInt, minFloat, maxFloat, minLength, maxLength, nonEmpty, format, includedIn
, fail, customValidation, oneOf
)
{-| Form validation.
# Combinators
@docs Validation, field, map, succeed, andThen, andMap, customError, defaultValue, mapError, withCustomError, sequence
# Fixed-size forms
@docs map2, map3, map4, map5, map6, map7, map8
# Type extractors
@docs list, string, int, float, bool, date, maybe, email, emptyString
# Common filters
@docs minInt, maxInt, minFloat, maxFloat, minLength, maxLength, nonEmpty, format, includedIn
# Custom validations
@docs fail, customValidation, oneOf
-}
import Dict exposing (Dict)
import Form.Error as Error exposing (Error, ErrorValue(..))
import Form.Field as Field exposing (Field, FieldValue(..))
import Form.Tree as Tree
import Regex exposing (Regex)
import Result
import String
{-| A validation is a function that takes a form field and returns a result
being either a validation error or the expected object.
-}
type alias Validation customError output =
Field -> Result (Error customError) output
{-| Map over the result of the validation.
field "myfield" (string |> map String.trim)
-}
map : (a -> b) -> Validation e a -> Validation e b
map f validation validationField =
Result.map f (validation validationField)
{-| Apply a new validation to the result of the validation.
field "myfield" (int |> andThen (minInt 10))
-}
andThen : (a -> Validation e b) -> Validation e a -> Validation e b
andThen callback validation validationField =
validation validationField |> Result.andThen (\next -> callback next validationField)
{-| Incremental form validation for records with more that 8 fields.
Form.Validate.succeed SomeRecord
|> andMap (field "foo" string)
|> andMap (field "bar" string)
-}
andMap : Validation e a -> Validation e (a -> b) -> Validation e b
andMap aValidation partialValidation validationField =
case ( partialValidation validationField, aValidation validationField ) of
( Ok partial, Ok a ) ->
Ok (partial a)
( partialResult, aResult ) ->
Err (mergeMany [ errMaybe partialResult, errMaybe aResult ])
{-| Rescue a failed validation with the supplied value.
-}
defaultValue : a -> Validation e a -> Validation e a
defaultValue a validation validationField =
Ok (Result.withDefault a (validation validationField))
{-| Call Result.mapError on validation result.
-}
mapError : (Error e1 -> Error e2) -> Validation e1 a -> Validation e2 a
mapError f validation =
\validationField -> Result.mapError f (validation validationField)
{-| Arrange that if a validation fails, it has the given custom error.
field "customerId"
(V.int
|> andThen (minInt 1)
|> andThen (maxInt 9999)
|> withCustomError InvalidIdentity
)
-}
withCustomError : customErr -> Validation e a -> Validation customErr a
withCustomError =
mapError << always << customError
{-| Helper to create a CustomError.
-}
customError : e -> Error e
customError =
CustomError >> Error.value
{-| Access the given field in the group.
field "name" string
-}
field : String -> Validation e a -> Validation e a
field key validation validationField =
Tree.getAtName key validationField
|> Maybe.withDefault (Tree.Value EmptyField)
|> validation
|> Result.mapError
(\e -> Tree.group [ ( key, e ) ])
{-| Validation a form with two fields.
-}
map2 : (a -> b -> m) -> Validation e a -> Validation e b -> Validation e m
map2 func v1 v2 =
map func v1
|> andMap v2
-- apply (form1 func v1) v2
{-| Validation a form with three fields.
-}
map3 : (a -> b -> c -> m) -> Validation e a -> Validation e b -> Validation e c -> Validation e m
map3 func v1 v2 v3 =
map2 func v1 v2
|> andMap v3
{-| Validation a form with four fields.
-}
map4 : (a -> b -> c -> d -> m) -> Validation e a -> Validation e b -> Validation e c -> Validation e d -> Validation e m
map4 func v1 v2 v3 v4 =
map3 func v1 v2 v3
|> andMap v4
{-| Validation a form with five fields.
-}
map5 : (a -> b -> c -> d -> e -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err m
map5 func v1 v2 v3 v4 v5 =
map4 func v1 v2 v3 v4
|> andMap v5
{-| Validation a form with six fields.
-}
map6 : (a -> b -> c -> d -> e -> f -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err m
map6 func v1 v2 v3 v4 v5 v6 =
map5 func v1 v2 v3 v4 v5
|> andMap v6
{-| Validation a form with seven fields.
-}
map7 : (a -> b -> c -> d -> e -> f -> g -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err g -> Validation err m
map7 func v1 v2 v3 v4 v5 v6 v7 =
map6 func v1 v2 v3 v4 v5 v6
|> andMap v7
{-| Validation a form with eight fields.
-}
map8 : (a -> b -> c -> d -> e -> f -> g -> h -> m) -> Validation err a -> Validation err b -> Validation err c -> Validation err d -> Validation err e -> Validation err f -> Validation err g -> Validation err h -> Validation err m
map8 func v1 v2 v3 v4 v5 v6 v7 v8 =
map7 func v1 v2 v3 v4 v5 v6 v7
|> andMap v8
{-| Private
-}
mergeMany : List (Maybe (Error e)) -> Error e
mergeMany errors =
errors
|> List.filterMap identity
|> List.foldl groupErrorsUnion (Tree.group [])
{-| Private
-}
groupErrorsUnion : Error e -> Error e -> Error e
groupErrorsUnion e1 e2 =
case ( e1, e2 ) of
( Tree.Group g1, Tree.Group g2 ) ->
Tree.Group (Dict.union g1 g2)
_ ->
e2
{-| Private
-}
errMaybe : Result e a -> Maybe e
errMaybe res =
case res of
Ok _ ->
Nothing
Err e ->
Just e
{-| Validation an integer using `String.toInt`.
-}
int : Validation e Int
int v =
Field.asString v
|> Maybe.andThen String.toInt
|> Result.fromMaybe (Error.value InvalidInt)
{-| Validation a float using `String.toFloat`.
-}
float : Validation e Float
float v =
Field.asString v
|> Maybe.andThen String.toFloat
|> Result.fromMaybe (Error.value InvalidFloat)
{-| Validation a String.
-}
string : Validation e String
string v =
case Field.asString v of
Just s ->
if String.isEmpty s then
Err (Error.value Empty)
else
Ok s
Nothing ->
Err (Error.value InvalidString)
{-| Validate an empty string, otherwise failing with InvalidString.
Useful with `oneOf` for optional fields with format validation.
-}
emptyString : Validation e String
emptyString v =
case Field.asString v of
Just s ->
if String.isEmpty s then
Ok s
else
Err (Error.value InvalidString)
Nothing ->
Ok ""
{-| Validation a Bool.
-}
bool : Validation e Bool
bool v =
case Field.asBool v of
Just b ->
Ok b
Nothing ->
Ok False
{-| Transform validation result to `Maybe`, using `Result.toMaybe`.
-}
maybe : Validation e a -> Validation e (Maybe a)
maybe validation validationField =
Ok (Result.toMaybe (validation validationField))
{-| Fails if `String.isEmpty`.
-}
nonEmpty : String -> Validation e String
nonEmpty s validationField =
if String.isEmpty s then
Err (Error.value Empty)
else
Ok s
{-| Min length for String.
-}
minLength : Int -> String -> Validation e String
minLength min s validationField =
if String.length s >= min then
Ok s
else
Err (Error.value (ShorterStringThan min))
{-| Max length for String.
-}
maxLength : Int -> String -> Validation e String
maxLength max s validationField =
if String.length s <= max then
Ok s
else
Err (Error.value (LongerStringThan max))
{-| Min value for Int.
-}
minInt : Int -> Int -> Validation e Int
minInt min i validationField =
if i >= min then
Ok i
else
Err (Error.value (SmallerIntThan min))
{-| Max value for Int.
-}
maxInt : Int -> Int -> Validation e Int
maxInt max i validationField =
if i <= max then
Ok i
else
Err (Error.value (GreaterIntThan max))
{-| Min value for Float.
-}
minFloat : Float -> Float -> Validation e Float
minFloat min i validationField =
if i >= min then
Ok i
else
Err (Error.value (SmallerFloatThan min))
{-| Max value for Float.
-}
maxFloat : Float -> Float -> Validation e Float
maxFloat max i validationField =
if i <= max then
Ok i
else
Err (Error.value (GreaterFloatThan max))
{-| Validates format of the string.
-}
format : Regex -> String -> Validation e String
format regex s validationField =
if Regex.contains regex s then
Ok s
else
Err (Error.value InvalidFormat)
{-| Stolen to elm-validate.
-}
validEmailPattern : Regex
validEmailPattern =
"^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
|> Regex.fromStringWith { caseInsensitive = True, multiline = False }
|> Maybe.withDefault Regex.never
{-| Check if the string is a valid email address.
-}
email : Validation e String
email =
string
|> andThen
(\s ->
format validEmailPattern s
|> mapError (\_ -> Error.value InvalidEmail)
)
{-| Check if the string is included in the given list.
-}
includedIn : List String -> String -> Validation e String
includedIn items s validationField =
if List.member s items then
Ok s
else
Err (Error.value NotIncludedIn)
{-| A validation that always fails. Useful for contextual validation.
-}
fail : Error e -> Validation e a
fail error validationField =
Err error
{-| A validation that always succeeds. Useful for contextual validation.
-}
succeed : a -> Validation e a
succeed a validationField =
Ok a
{-| Custom validation for your special cases.
-}
customValidation : Validation e a -> (a -> Result (Error e) b) -> Validation e b
customValidation validation callback validationField =
validation validationField |> Result.andThen callback
{-| First successful validation wins, from left to right.
-}
oneOf : List (Validation e a) -> Validation e a
oneOf validations validationField =
let
results =
List.map (\v -> v validationField) validations
walkResults result combined =
case ( combined, result ) of
( Ok _, _ ) ->
combined
_ ->
result
in
List.foldl walkResults (Err (Error.value Empty)) results
{-| Combine a list of validations into a validation producing a list of all
results.
-}
sequence : List (Validation e a) -> Validation e (List a)
sequence validations =
List.foldr (map2 (::)) (succeed []) validations
{-| Validate a list of fields.
-}
list : Validation e a -> Validation e (List a)
list validation validationField =
case validationField of
Tree.List items ->
let
results =
List.map validation items
indexedErrMaybe index res =
case res of
Ok _ ->
Nothing
Err e ->
Just ( String.fromInt index, e )
errors =
results
|> List.indexedMap indexedErrMaybe
|> List.filterMap identity
in
if List.isEmpty errors then
Ok (List.filterMap Result.toMaybe results)
else
Err (Tree.group errors)
_ ->
Ok []