Skip to content

fix: do not parse a date string that describes no real date - #12430

Merged
web-padawan merged 4 commits into
mainfrom
fix/reject-impossible-date-string
Aug 14, 2026
Merged

web-padawan merged 4 commits into
mainfrom
fix/reject-impossible-date-string

Conversation

@web-padawan

Copy link
Copy Markdown
Member

parseDate built a date from the month and day as given, and building one past the end of its month carries the surplus into the next one. So a date that does not exist was accepted as a different date. Measured on main:

set resulting value
'2026-02-30' '2026-03-02'
'2026-13-01' '2027-01-01'
'2026-01-32' '2026-02-01'
'2026-00-10' '2025-12-10'
'2026-01-00' '2025-12-31'

The property was rewritten to a date the application never asked for, and value-changed fired with it. A constraint shifted the same way: min = '2026-02-30' gave a minimum of 2 March, so 1 March was reported invalid.

Such a string is now not parsed. That routes it into the path an unparsable value already takes in _valueChanged — the previous value is kept — so it behaves like the non-ISO formats that are already rejected. Typing one reports bad input instead of jumping to another month, which is what <input type="date"> does. parseUTCDate matches, so the same holds for <vaadin-date-time-picker>.

Trade-off worth a look

A min or max that does not parse is not applied at all, so an impossible one now leaves the field unconstrained where it used to constrain the wrong day. Neither is good; this one is at least the same rule that already governs every other unparsable min. The remedy for both is the warning on an unsupported format asked for in #3942, which is not part of this change.

Flow

Nothing to change on the Flow side:

  • value, min and max come from LocalDate.toString(), and LocalDate cannot hold a date that does not exist, so the server can never send one.
  • The connector's i18n.parseDate runs date-fns parse and returns parts only when isValid passes, so the parts it hands to the web component always describe a real date.
  • correctFullYear calls the web component's parser on datepicker.value, which the component itself formatted, so it is always a real date.

One pre-existing connector bug is worth reporting separately: formatDate builds an unpadded ${year}-${month + 1}-${day} and feeds it to the same parser, which does not accept a one-digit unsigned year. For a year below 10 it therefore gets undefined and passes it to dateFnsFormat. That is the same unpadded round-trip behind the years 0-9 half of #1789, and it is unaffected by this change.

Related to #3942


🤖 Generated with Claude Code

Building a date from a month or day past its end carried the surplus into the
next month, so `value = '2026-02-30'` answered with `'2026-03-02'`: the property
was rewritten to a date that was never asked for, and `value-changed` fired with
it. A `min` or `max` shifted the same way, silently moving the constraint.

Such a string is now not parsed, which routes it into the path an unparsable
value already takes: the previous value is kept, and typing one reports bad input
rather than jumping to another month. `parseUTCDate` matches, so the same holds
for `<vaadin-date-time-picker>`.

Related to #3942

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Comment thread packages/date-picker/test/basic.test.js Resolved
Comment thread packages/date-picker/test/basic.test.js Resolved
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
@web-padawan
web-padawan requested a review from vursen August 13, 2026 13:40

@vaadin-review-bot vaadin-review-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changes — collected 1 finding below that could not be attached to the diff.

Finding
⚠️ An impossible i18n.referenceDate now makes getAdjustedYear throw instead of using a shifted date.

⚠️ An impossible i18n.referenceDate now makes getAdjustedYear throw instead of using a shifted date.
Set i18n.referenceDate to a well-formed but non-existent date, e.g. '2026-02-30' or '2023-02-29', then type a date with a 2-digit year such as 1/15/24. The default i18n.parseDate takes the short-year branch and runs usedReferenceDate = parseDate(this.referenceDate). parseDate now returns undefined for an impossible date (before this PR it returned a rolled-over Date like Mar 2 2026). It is passed straight into getAdjustedYear, whose first line referenceDate.getFullYear() then throws TypeError: Cannot read properties of undefined. There is no try/catch on the _onInput path, so the input handler throws on the keystroke.

The PR turns a silently-wrong reference date into a hard crash for this input class. Guard the fallback: const usedReferenceDate = parseDate(this.referenceDate) || new Date();. (A syntactically garbage referenceDate already crashed here before the PR; the newly-broken case is the well-formed-but-impossible date.)
vaadin-date-picker-mixin.js:66 · correctness · confirmed

@web-padawan

Copy link
Copy Markdown
Member Author

An impossible i18n.referenceDate now makes getAdjustedYear throw instead of using a shifted date.

Fixed.

`getAdjustedYear` reads the year off the reference date, so a `referenceDate`
that does not parse made typing a two-digit year throw from the input handler.
A garbage one already did; an impossible one such as `2026-02-30` joined it once
such a date stopped being parsed. Falling back to today is what the property
documents as its default, and is already what an empty one does.

Coercion of a non-string is left to throw where it always did: the parser
documents a string, and an entry from a `dateMetadataProvider` is the caller that
has to check its own input.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@web-padawan
web-padawan force-pushed the fix/reject-impossible-date-string branch from 29cc077 to 8c70708 Compare August 14, 2026 06:44
@web-padawan
web-padawan requested a review from vursen August 14, 2026 06:44
@sonarqubecloud

Copy link
Copy Markdown

@totally-not-ai

Copy link
Copy Markdown
Contributor

This PR has been cherry-picked to 24.9: #13284

@totally-not-ai

Copy link
Copy Markdown
Contributor

This PR has been cherry-picked to 24.10: #13285

web-padawan added a commit that referenced this pull request Oct 27, 2026
…13285)

Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
web-padawan added a commit that referenced this pull request Oct 27, 2026
…13284)

Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants