module Validation exposing
  ( date
  )

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

import Form.Validate as Validate exposing (..)

date : Validation String Date
date =
  customValidation 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 (customError "InvalidDate")
      _ -> Err (customError "InvalidDate")
  )