module Model.Frequency exposing ( Frequency(..) , decoder , validate , fromString ) import Json.Decode as Decode exposing (Decoder) import Json.Decode.Extra as Decode import Form.Validate as Validate exposing (Validation) type Frequency = Punctual | Monthly decoder : Decoder Frequency decoder = let frequencyResult input = fromString input |> Result.fromMaybe ("Could not deduce Punctual nor Monthly from " ++ input) in Decode.string |> Decode.andThen (Decode.fromResult << frequencyResult) validate : Validation String Frequency validate = Validate.customValidation Validate.string (\str -> fromString str |> Result.fromMaybe (Validate.customError "InvalidFrequency") ) fromString : String -> Maybe Frequency fromString str = if str == toString Punctual then Just Punctual else if str == toString Monthly then Just Monthly else Nothing