Skip to content

Raise ValidationError for overflowing datetime inputs - #3107

Open
keshtib07-ghost wants to merge 2 commits into
marshmallow-code:devfrom
keshtib07-ghost:fix/datetime-overflow-validation
Open

keshtib07-ghost wants to merge 2 commits into
marshmallow-code:devfrom
keshtib07-ghost:fix/datetime-overflow-validation

Conversation

@keshtib07-ghost

Copy link
Copy Markdown

Malformed datetime inputs can raise OverflowError instead of ValidationError. This affects RFC dates with oversized components and integer POSIX timestamps too large to convert to a float.

For example:

from marshmallow import fields

fields.DateTime(format="rfc").deserialize(
    "Fri, 31 Dec 999999999999999999 23:59:59 +0000"
)
# OverflowError: signed integer is greater than maximum

The error also escapes through Schema.load() and Schema.validate(), bypassing normal validation-error handling.

Expected behavior:

  • Schema.load() raises ValidationError with the affected field’s error message.
  • Schema.validate() returns the field’s error dictionary.
  • Direct field deserialization raises ValidationError with "Not a valid datetime.".

The existing exception handler in _TemporalField._deserialize translates several parsing exceptions into validation errors but does not catch OverflowError. This change includes it in that handler.

Adds 18 regression cases covering:

  • Oversized RFC years and hours with both rfc and rfc822.
  • DateTime, NaiveDateTime, and AwareDateTime.
  • Oversized positive and negative integer timestamps with timestamp and timestamp_ms.
  • Error reporting through Schema.load() and Schema.validate().

Also adds a changelog entry.

Validation on Python 3.12:

  • Full test suite: 1,210 passed.
  • Mypy: no issues found in 36 source files.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant