form.iso-date
Check a date picker value (YYYY-MM-DD) against required, past/future and min/max rules, timezone-safe.
1.0.0 · published 2026-10-10 by charlie · Anterra
Pinned by 24 tests, run in TypeScript, Python and Rust.
What it does
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
For example
validate_iso_date(2024-02-29, Start date, true, today —, timing —, not before —, not after —)→ valid true, date 2024-02-29, message — a leap day from the pickervalidate_iso_date(2023-02-29, Start date, true, today —, timing —, not before —, not after —)→ valid false, date —, message Start date must be a real date 29 February 2023 does not existvalidate_iso_date(, Start date, true, today —, timing —, not before —, not after —)→ valid false, date —, message Enter start date empty and required: GOV.UK's Enter message, label lower-cased
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def validate_iso_date(value: str, label: str, required: bool, rules: DateRules) -> IsoDateCheck
| value | string | what the date picker submitted, YYYY-MM-DD, or what was typed where the browser has no picker |
| label | string | what the date is, as it starts a sentence: "Date of birth", "Start date" |
| required | bool | an empty value is an error |
| rules | DateRules | form.date-parts' rules: past/future against today (passed in), earliest and latest |
| returns | IsoDateCheck | valid with the ISO date (null when empty and not required), or the message |
The type it declares, generated into your project
@dataclass(frozen=True)
class IsoDateCheck:
"""The verdict on a date picker's value."""
valid: bool
#: the ISO date, when valid and given
date: Optional[str]
#: the error message to show, when not valid
message: Optional[str]
Your code names it in one line, in the file that uses it
from fune.form.iso_date import validate_iso_date # form.iso-date@^1
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from .form_date_parts import DateParts, DateRules, validate_date_parts ← from form.date-parts ^1.0.0 · built alongside by fune
from .form_iso_date_types import IsoDateCheck
# ASCII whitespace only, so all three languages trim the same characters.
_SPACE = " \t\n\r\f\v"
def _iso_shape(value: str) -> bool:
# YYYY-MM-DD with ASCII digits: what <input type="date"> submits, and
# nothing else (no time, no offset, no other script's digits).
if len(value) != 10:
return False
for i, ch in enumerate(value):
if i in (4, 7):
if ch != "-":
return False
elif not ("0" <= ch <= "9"):
return False
return True
def validate_iso_date(value: str, label: str, required: bool, rules: DateRules) -> IsoDateCheck:
"""A date picker's value, checked with form.date-parts' rules and
messages. Pure calendar arithmetic on the string: no datetime, so no time
zone can move the day; "today" is passed in by the caller."""
# Called with empty boxes first, so a bad label or bad rules raise
# whatever was typed, and its answer is the "Enter ..." message.
empty = validate_date_parts(DateParts(day="", month="", year=""), label, rules)
typed = value.strip(_SPACE)
if typed == "":
if required:
return IsoDateCheck(valid=False, date=None, message=empty.message)
return IsoDateCheck(valid=True, date=None, message=None)
if not _iso_shape(typed):
return IsoDateCheck(valid=False, date=None, message="%s must be a real date" % label.strip(_SPACE))
check = validate_date_parts(DateParts(day=typed[8:10], month=typed[5:7], year=typed[0:4]), label, rules)
if check.valid:
return IsoDateCheck(valid=True, date=check.date, message=None)
return IsoDateCheck(valid=False, date=None, message=check.message)Install
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add form.iso-date
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./form.iso-date-1.0.0-python.fune, or fetch it from a terminal with fune pull form.iso-date@1.0.0:python.
The whole function, every language, is one file too: form.iso-date-1.0.0.fune, 19,756 bytes, sha256 5756e6138018dc6894f09e20db8f9e15661a733aaacce402d106eafbe29a2f82. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before form.iso-date
after — your function gets the result and the arguments, and returns the final result.
# fune: after form.iso-date
replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.
# fune: replace form.date-parts in form.iso-date
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show form.iso-date --steps.
# fune: step form.iso-date after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| a leap day from the picker | 2024-02-29, Start date, true, today —, timing —, not before —, not after — | → | valid true, date 2024-02-29, message — |
| 29 February 2023 does not exist | 2023-02-29, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| empty and required: GOV.UK's Enter message, label lower-cased | , Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Enter start date |
| empty and not required is valid with no date | , Start date, false, today —, timing —, not before —, not after — | → | valid true, date —, message — |
| only whitespace counts as empty | , Start date, false, today —, timing —, not before —, not after — | → | valid true, date —, message — |
| surrounding spaces and a trailing newline are trimmed | 2024-01-05 , Start date, true, today —, timing —, not before —, not after — | → | valid true, date 2024-01-05, message — |
| a UK-style date is not the picker's format | 05/01/2024, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| unpadded month and day are not the picker's format | 2024-1-5, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| Arabic-Indic digits are not a date | ٢٠٢٤-01-05, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| a date and time is not a date | 2024-02-29T00:00:00Z, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
Show the other 14 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| month 13 | 2024-13-01, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| year 0000 | 0000-01-01, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| 31 April | 2025-04-31, Start date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Start date must be a real date |
| an acronym label keeps its capitals | , UK arrival date, true, today —, timing —, not before —, not after — | → | valid false, date —, message Enter UK arrival date |
| past: today itself is not in the past | 2026-10-10, Date of birth, true, today 2026-10-10, timing past, not before —, not after — | → | valid false, date —, message Date of birth must be in the past |
| past: yesterday is | 2026-10-09, Date of birth, true, today 2026-10-10, timing past, not before —, not after — | → | valid true, date 2026-10-09, message — |
| future-or-today accepts today | 2026-10-10, Start date, true, today 2026-10-10, timing future-or-today, not before —, not after — | → | valid true, date 2026-10-10, message — |
| min and max: after the range | 2027-01-01, Start date, true, today —, timing —, not before 2026-01-01, not after 2026-12-31 | → | valid false, date —, message Start date must be between 1 January 2026 and 31 December 2026 |
| min and max are inclusive | 2026-12-31, Start date, true, today —, timing —, not before 2026-01-01, not after 2026-12-31 | → | valid true, date 2026-12-31, message — |
| min only: the day before | 2025-12-31, Start date, true, today —, timing —, not before 2026-01-01, not after — | → | valid false, date —, message Start date must be the same as or after 1 January 2026 |
| max only: the day after | 2026-02-01, End date, true, today —, timing —, not before —, not after 2026-01-31 | → | valid false, date —, message End date must be the same as or before 31 January 2026 |
| a label is needed even for an empty optional date | , , false, today —, timing —, not before —, not after — | → | error: a date needs a label |
| a timing rule needs today | 2026-01-01, Start date, true, today —, timing past, not before —, not after — | → | error: rules.today is needed to check that a date is past |
| min after max is the caller's mistake, whatever was typed | , Start date, false, today —, timing —, not before 2026-02-01, not after 2026-01-01 | → | error: must not be after rules.notAfter |
Verification
Verified by the operator, not the sandbox 24 test vectors declared; none run by the registry.
Verified locally by the registry's operator, not by the sandbox. This version was published with FUNE_VERIFY_MODE=inline-dev (the operator's import of the first-party registry, or a development server): the server validated the package - its manifest, signatures, imports, safety lint and test rules - but the registry did not run its test vectors in its sandbox. The operator runs them with `fune verify --all` before importing.
Signed by the registry with key fw-9dc887227da47575 (ed25519), over its integrity and this report's digest. The full report · JSON
More from the author
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.
Files
| Path | Bytes |
|---|---|
| NOTICE | 1,211 |
| README.md | 2,186 |
| impl/python.py | 1,795 |
| impl/rust.rs | 3,002 |
| impl/typescript.ts | 2,150 |
| vectors.json | 5,853 |