Fix negative duration (#1132)

* Validate ends at

* Update bindata.go
This commit is contained in:
Andrey Kuzmin 2017-12-14 16:16:42 +01:00 committed by stuart nelson
parent 06f9a4ad1d
commit 50394569f5
5 changed files with 65 additions and 43 deletions

View File

@ -48,21 +48,25 @@ term =
|. Parser.ignore Parser.zeroOrMore ((==) ' ')
durationFormat : Time.Time -> String
durationFormat : Time.Time -> Maybe String
durationFormat time =
List.foldl
(\( unit, ms ) ( result, curr ) ->
( if curr // ms == 0 then
result
else
result ++ toString (curr // ms) ++ unit ++ " "
, curr % ms
if time >= 0 then
List.foldl
(\( unit, ms ) ( result, curr ) ->
( if curr // ms == 0 then
result
else
result ++ toString (curr // ms) ++ unit ++ " "
, curr % ms
)
)
)
( "", round time )
units
|> Tuple.first
|> String.trim
( "", round time )
units
|> Tuple.first
|> String.trim
|> Just
else
Nothing
dateFormat : Time.Time -> String
@ -86,10 +90,13 @@ encode =
timeFromString : String -> Result String Time.Time
timeFromString =
ISO8601.fromString
>> Result.map (ISO8601.toTime >> toFloat)
>> Result.mapError (always "Wrong ISO8601 format")
timeFromString string =
if string == "" then
Err "Should not be empty"
else
ISO8601.fromString string
|> Result.map (ISO8601.toTime >> toFloat)
|> Result.mapError (always "Wrong ISO8601 format")
fromTime : Time.Time -> Types.Time

View File

@ -11,6 +11,7 @@ module Views.SilenceForm.Types
, initSilenceForm
, emptyMatcher
, validateForm
, parseEndsAt
)
import Silences.Types exposing (Silence, SilenceId, nullSilence)
@ -70,11 +71,9 @@ type SilenceFormFieldMsg
= AddMatcher
| DeleteMatcher Int
| UpdateStartsAt String
| ValidateStartsAt
| UpdateEndsAt String
| ValidateEndsAt
| UpdateDuration String
| ValidateDuration
| ValidateTime
| UpdateCreatedBy String
| ValidateCreatedBy
| UpdateComment String
@ -111,7 +110,7 @@ toSilence { id, comment, matchers, createdBy, startsAt, endsAt } =
(List.foldr appendMatcher (Ok []) matchers)
(stringNotEmpty createdBy.value)
(timeFromString startsAt.value)
(timeFromString endsAt.value)
(parseEndsAt startsAt.value endsAt.value)
|> Result.toMaybe
@ -122,7 +121,7 @@ fromSilence { id, createdBy, comment, startsAt, endsAt, matchers } =
, comment = initialField comment
, startsAt = initialField (timeToString startsAt)
, endsAt = initialField (timeToString endsAt)
, duration = initialField (durationFormat (endsAt - startsAt))
, duration = initialField (durationFormat (endsAt - startsAt) |> Maybe.withDefault "")
, matchers = List.map fromMatcher matchers
}
@ -133,12 +132,25 @@ validateForm { id, createdBy, comment, startsAt, endsAt, duration, matchers } =
, createdBy = validate stringNotEmpty createdBy
, comment = validate stringNotEmpty comment
, startsAt = validate timeFromString startsAt
, endsAt = validate timeFromString endsAt
, endsAt = validate (parseEndsAt startsAt.value) endsAt
, duration = validate parseDuration duration
, matchers = List.map validateMatcherForm matchers
}
parseEndsAt : String -> String -> Result String Time.Time
parseEndsAt startsAt endsAt =
case ( timeFromString startsAt, timeFromString endsAt ) of
( Ok starts, Ok ends ) ->
if starts > ends then
Err "Can't be in the past"
else
Ok ends
( _, endsResult ) ->
endsResult
validateMatcherForm : MatcherForm -> MatcherForm
validateMatcherForm { name, value, isRegex } =
{ name = validate stringNotEmpty name
@ -177,7 +189,7 @@ fromMatchersAndTime defaultCreator matchers now =
{ empty
| startsAt = initialField (timeToString now)
, endsAt = initialField (timeToString (now + defaultDuration))
, duration = initialField (durationFormat defaultDuration)
, duration = initialField (durationFormat defaultDuration |> Maybe.withDefault "")
, createdBy = initialField defaultCreator
, matchers =
-- If no matchers were specified, add an empty row

View File

@ -19,6 +19,7 @@ import Views.SilenceForm.Types
, SilenceFormFieldMsg(..)
, fromMatchersAndTime
, fromSilence
, parseEndsAt
, validateForm
, toSilence
, emptyMatcher
@ -42,7 +43,12 @@ updateForm msg form =
durationValue =
case Result.map2 (-) endsAt startsAt of
Ok duration ->
Utils.Date.durationFormat duration
case Utils.Date.durationFormat duration of
Just value ->
value
Nothing ->
form.duration.value
Err _ ->
form.duration.value
@ -52,11 +58,6 @@ updateForm msg form =
, duration = updateValue durationValue form.duration
}
ValidateStartsAt ->
{ form
| startsAt = validate Utils.Date.timeFromString form.startsAt
}
UpdateEndsAt time ->
let
endsAt =
@ -68,7 +69,12 @@ updateForm msg form =
durationValue =
case Result.map2 (-) endsAt startsAt of
Ok duration ->
Utils.Date.durationFormat duration
case Utils.Date.durationFormat duration of
Just value ->
value
Nothing ->
form.duration.value
Err _ ->
form.duration.value
@ -78,11 +84,6 @@ updateForm msg form =
, duration = updateValue durationValue form.duration
}
ValidateEndsAt ->
{ form
| endsAt = validate Utils.Date.timeFromString form.endsAt
}
UpdateDuration time ->
let
duration =
@ -104,9 +105,11 @@ updateForm msg form =
, duration = updateValue time form.duration
}
ValidateDuration ->
ValidateTime ->
{ form
| duration = validate Utils.Date.parseDuration form.duration
| startsAt = validate Utils.Date.timeFromString form.startsAt
, endsAt = validate (parseEndsAt form.startsAt.value) form.endsAt
, duration = validate Utils.Date.parseDuration form.duration
}
UpdateCreatedBy createdBy ->

View File

@ -59,19 +59,19 @@ timeInput startsAt endsAt duration =
"Start"
"col-5"
(UpdateStartsAt >> UpdateField)
(ValidateStartsAt |> UpdateField)
(ValidateTime |> UpdateField)
startsAt
, validatedField input
"Duration"
"col-2"
(UpdateDuration >> UpdateField)
(ValidateDuration |> UpdateField)
(ValidateTime |> UpdateField)
duration
, validatedField input
"End"
"col-5"
(UpdateEndsAt >> UpdateField)
(ValidateEndsAt |> UpdateField)
(ValidateTime |> UpdateField)
endsAt
]

File diff suppressed because one or more lines are too long