Functional Weave
Code in Rust

form.iso-date@1.0.0

README.md

2,186 bytes · view raw

# form.iso-date

The server-side twin of a date picker (`<input type="date">`, or
`react.form.date-field`): the browser submits `YYYY-MM-DD`, and this checks it
again with the same rules and the GOV.UK Design System's wording, in
TypeScript, Python and Rust.

## What it checks, in order

1. The label and rules, whatever was typed: an empty label, a past/future rule
   without `today`, a malformed rule date, or `notBefore` after `notAfter`
   throws (these are the caller's mistakes, as in `form.date-parts`).
2. Empty (after trimming ASCII whitespace): `Enter <label>` when required,
   otherwise valid with `date: null`.
3. The shape: exactly `YYYY-MM-DD` in ASCII digits. Anything else (`05/01/2024`,
   `2024-1-5`, a time, other scripts' digits) is `<Label> must be a real date`.
4. Then `form.date-parts`' `validateDateParts` with the three parts: a date
   that does not exist (31 April, 29 February 2023, month 13, year 0000), then
   `timing` against `today`, then `notBefore` / `notAfter` (inclusive), with its
   messages ("must be between 1 January 2026 and 31 December 2026").

## Timezone-safe

No `Date`, `datetime` or clock is used: the value is a calendar date and is
compared as one, so no offset or daylight saving change can move it by a day.
The one date that depends on a time zone is *today*, and the caller passes it.
Work it out in the user's zone, not the server's: in a browser,
`new Date().toLocaleDateString("en-CA")` gives today's `YYYY-MM-DD` in the
browser's zone; on a server, convert the current instant with the user's IANA
zone (`Intl.DateTimeFormat("en-CA", { timeZone })`, Python's `zoneinfo`, or
`time.unix-to-iso` with their offset). A date of birth checked "in the past"
against the server's UTC date would refuse someone in Auckland born today.

The same `min`/`max` belong on the input as attributes (`min="2026-01-01"`), so
the picker greys out what this would refuse.

## Notices

Error message wording follows the GOV.UK Design System. Portions derived from GOV.UK Frontend (https://github.com/alphagov/govuk-frontend), Copyright (c) 2017 Crown Copyright (Government Digital Service), under the MIT License; the full notice is in NOTICE.