-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkotae1.elm
47 lines (35 loc) · 1.51 KB
/
kotae1.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
| value =
case String.toInt form.number of
Just number ->
if (not <| String.isEmpty form.text) && String.length form.text <= number && 1 <= number && number <= 10 then
Ok { text = form.text, number = number }
else
Err "textかnumberがちょっと問題"
Nothing ->
Err "整数に変換できない"
Decoder.identity
|> Decoder.assert (Decoder.minLength TextEmpty 1)
|> Decoder.assert (Decoder.maxLength TextOver10Length 10)
decoder : Decoder Form Error Value
decoder =
Decoder.top Value
|> Decoder.field (Decoder.lift .text textDecoder)
|> Decoder.field (Decoder.lift .number numberDecoder)
|> Decoder.assert
(Decoder.custom <|
\{ text, number } ->
if String.length text <= number then
Ok ()
else
Err [ TextTooLong ]
)
textDecoder : Decoder String Error String
textDecoder =
Decoder.identity
|> Decoder.assert (Decoder.minLength TextEmpty 1)
|> Decoder.assert (Decoder.maxLength TextTooLong 10)
numberDecoder : Decoder String Error Int
numberDecoder =
Decoder.int NumberInvalidInt
|> Decoder.assert (Decoder.minBound NumberBelow 1)
|> Decoder.assert (Decoder.maxBound NumberOver 10)