jweir / elm-iso8601

Elm library to parse and convert ISO8601 times and dates
https://package.elm-lang.org/packages/jweir/elm-iso8601/latest
MIT License
15 stars 7 forks source link

anyrandomstring9999 parsed as valid date #20

Open smucode opened 3 years ago

smucode commented 3 years ago
> ISO8601.fromString "9999"
Ok { day = 1, hour = 0, millisecond = 0, minute = 0, month = 1, offset = 0, second = 0, year = 9999 }
    : Result String ISO8601.Time
> ISO8601.fromString "anyrandomstring9999"
Ok { day = 1, hour = 0, millisecond = 0, minute = 0, month = 1, offset = 0, second = 0, year = 9999 }
    : Result String ISO8601.Time

Pretty sure neither of these should be valid.

jweir commented 3 years ago

Thank you. The first case is valid (surprisingly, right?) according to ISO-8601 which can have just a year, it can even be less precise and just include a century -> 99 would be 9900. Although this library does not support that, it doesn't support the full spec (and never will).

The second case is more interesting. Should this library allow parsing a time from a string such as "I was born on 1995-02-03"? Perhaps it should be very strict and disallow that, or perhaps it should be liberal and allow it.

I am going to be robust here and be liberal in what is accepted.

Have no fear though, there is a stricter library with many advantages over elm-iso8601. https://github.com/rtfeldman/elm-iso8601-date-strings. It will only take dates/times, and I believe it requires a minimum of a calendar day defined. And it generates a Posix type, which in many cases (but not all) is superior to the record which elm-iso8601 generates.

smucode commented 3 years ago

Alright, that makes sense! Digging into an issue today, I was a little surprised when a Json.Decode.oneOf ended up decoding device:///bfb4d9a6-d272-a9cb-b1d6-8b5cc3fc43c7/8d2d356c-650b-66f7-61d9-176c6aea2701 into Posix 23068108800000 rather than a string :)