aboutsummaryrefslogtreecommitdiff
path: root/src/client/Validation.elm
diff options
context:
space:
mode:
authorJoris2017-03-26 21:10:42 +0200
committerJoris2017-03-26 21:10:42 +0200
commit1e47a7754ca38bd1a6c74765d8378caf68ce4619 (patch)
treed0d9238479dc2529a1b558bbbcde346e7e8c2935 /src/client/Validation.elm
parentc0ac16a713c4e53cf6af8e72a6d5f6b8ac5d6456 (diff)
downloadbudget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.tar.gz
budget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.tar.bz2
budget-1e47a7754ca38bd1a6c74765d8378caf68ce4619.zip
Separate client and server watch
Diffstat (limited to 'src/client/Validation.elm')
-rw-r--r--src/client/Validation.elm47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/client/Validation.elm b/src/client/Validation.elm
new file mode 100644
index 0000000..4781c3d
--- /dev/null
+++ b/src/client/Validation.elm
@@ -0,0 +1,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")
+ )