Fix the strict date parser in a library migration

Review the AI's codeEasy10 min

Your task

An agent replaced Moment.js with a stricter date library by find and replace, and the build stayed green. Moment is forgiving: it accepts 2026-1-31 and guesses at 03/14/2026. Before shipping, your team pins down exactly which date strings the new parser accepts.

An assistant wrote normalize_dates_strict(raw_dates). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • raw_dates: the date strings to check.
  • Return one result per input, in order: the input unchanged if it is a valid date, otherwise Invalid Date.

Rules the tests check:

  • A valid date is exactly 10 characters in the form YYYY-MM-DD: digits from 0 to 9, with - as the 5th and the 8th character. Nothing is trimmed, so a trailing space makes it invalid.
  • The month is from 01 to 12.
  • The day is from 01 to the length of that month. February has 29 days in a leap year and 28 otherwise.
  • A year is a leap year when it is divisible by 4, except that a year divisible by 100 is a leap year only if it is also divisible by 400. So 2000 and 0000 are leap years, and 1900 is not.

Examples

normalize_dates_strict(raw_dates: list[str]) -> list[str]
Example 1
Inputnormalize_dates_strict(["2026-01-31", "2026-1-31", "03/14/2026"])
Output["2026-01-31", "Invalid Date", "Invalid Date"]
Example 2
Inputnormalize_dates_strict(["2024-02-29", "2026-02-29"])
Output["2024-02-29", "Invalid Date"]
Example 3
Inputnormalize_dates_strict([])
Output[]