Marshmallow: ISO8601 DateTimes ending with Z considered not valid in 2.19.4

Created on 17 Jun 2019  ·  6Comments  ·  Source: marshmallow-code/marshmallow

Probably related to #1247 and #1234 - in marshmallow 2.19.4, with python-dateutil _not_ installed, it seems that loading a datetime in ISO8601 that ends in Z (UTC time) results in an error:

class Foo(Schema):
    date = DateTime(required=True)


foo_schema = Foo(strict=True)

a_date_with_z = '2019-06-17T00:57:41.000Z'
foo_schema.load({'date': a_date_with_z})
marshmallow.exceptions.ValidationError: {'date': ['Not a valid datetime.']}

Digging a bit deeper, it seems from_iso_datetime is failing with a unconverted data remains: Z - my understanding of the spec is rather limited, but it seems that they are indeed valid ISO8601 dates (and in marshmallow==2.19.3 and earlier, the previous snippet seems to work without raising validation errors).

bug datetime

All 6 comments

@lafrech Would you mind looking into this?

Thanks for reporting.

This is definitely a side effect of https://github.com/marshmallow-code/marshmallow/pull/1249/files. Sorry about that.

I don't own a copy of the spec, so the work on this is based on examples... I assumed that microseconds always came as a six-pack. It seems only three digits (your example) is acceptable. From what I understand in the regex we copied from Django, we could even expect any number of digits in [1; 6].

I see two solutions to this:

  • Split around ".", then in the right part, get all numbers and ignore letters/symbols.
  • Split around ".", then split the right part around anything that delimitates a timezone ("Z", "+", "-", what else?).

Thanks both for the prompt reply! I don't have a copy of the spec myself either - for the timezone suffix, I have based my previous comment on the Wikipedia entry, which seems to hint at the following designators being allowed:

<time>Z
<time>±hh:mm
<time>±hhmm
<time>±hh

I also use this WP page, but it doesn't show much about milli/microseconds.

I just submitted a PR: https://github.com/marshmallow-code/marshmallow/pull/1252.

It might not be the most elegant/pythonic/efficient but I think it does what it says.

The fix is release in 2.19.5. Thanks @diego-plan9 for reporting and @lafrech for the quick fix.

Was this page helpful?
0 / 5 - 0 ratings