aboutsummaryrefslogtreecommitdiff
path: root/src/client/Validation.elm
blob: 4781c3dc58aa657cd86dbe2fec7e21f7d1c6f340 (plain)
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
module Validation exposing
  ( cost
  , date
  , category
  )

import Date exposing (Date)
import Date.Extra.Core exposing (intToMonth)
import Date.Extra.Create exposing (dateFromFields)
import Dict
import String exposing (toInt, split)

import Form.Validate as Validate exposing (Validation)

import Model.Category exposing (Categories, CategoryId)

cost : Validation String Int
cost =
  Validate.customValidation Validate.int (\n ->
    if n == 0
      then Err (Validate.customError "CostMustNotBeNull")
      else Ok n
  )

date : Validation String Date
date =
  Validate.customValidation Validate.string (\str ->
    case split "/" str of
      [day, month, year] ->
        case (toInt day, toInt month, toInt year) of
          (Ok dayNum, Ok monthNum, Ok yearNum) ->
            Ok (dateFromFields yearNum (intToMonth monthNum) dayNum 0 0 0 0)
          _ -> Err (Validate.customError "InvalidDate")
      _ -> Err (Validate.customError "InvalidDate")
  )

category : Categories -> Validation String CategoryId
category categories =
  Validate.customValidation Validate.string (\str ->
    case toInt str of
      Ok category ->
        if List.member category (Dict.keys categories)
          then Ok category
          else Err (Validate.customError "InvalidCategory")
      Err _ ->
        Err (Validate.customError "InvalidCategory")
  )