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.
pub fn 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
/// The verdict on a date picker's value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IsoDateCheck {
pub valid: bool,
/// the ISO date, when valid and given
pub date: Option<String>,
/// the error message to show, when not valid
pub message: Option<String>,
}
Your code names it in one line, in the file that uses it
fune!(form.iso-date@^1); // then call validate_iso_date(…)
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
use super::form_date_parts::{validate_date_parts, DateParts, DateRules}; ← from form.date-parts ^1.0.0 · built alongside by fune
use super::funejson::Value; ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
// ASCII whitespace only, so all three languages trim the same characters.
fn trim_ascii(value: &str) -> &str {
value.trim_matches(|c: char| matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0c' | '\x0b'))
}
// YYYY-MM-DD with ASCII digits: what <input type="date"> submits, and nothing
// else (no time, no offset, no other script's digits).
fn iso_shape(value: &str) -> bool {
let bytes = value.as_bytes();
bytes.len() == 10
&& bytes.iter().enumerate().all(|(i, &b)| if i == 4 || i == 7 { b == b'-' } else { b.is_ascii_digit() })
}
/// A date picker's value, checked with form.date-parts' rules and messages.
/// Pure calendar arithmetic on the string, so no time zone can move the day;
/// "today" for past/future rules is passed in by the caller.
///
/// # Panics
/// As form.date-parts: an empty label, a timing rule without today, a
/// malformed rule date, or notBefore after notAfter.
pub fn validate_iso_date(value: &str, label: &str, required: bool, rules: &DateRules) -> IsoDateCheck {
// Called with empty boxes first, so a bad label or bad rules panic
// whatever was typed, and its answer is the "Enter ..." message.
let empty = DateParts { day: String::new(), month: String::new(), year: String::new() };
let blank = validate_date_parts(&empty, label, rules);
let typed = trim_ascii(value);
if typed.is_empty() {
return if required {
IsoDateCheck { valid: false, date: None, message: blank.message }
} else {
IsoDateCheck { valid: true, date: None, message: None }
};
}
if !iso_shape(typed) {
return IsoDateCheck { valid: false, date: None, message: Some(format!("{} must be a real date", trim_ascii(label))) };
}
let parts = DateParts { day: typed[8..10].to_string(), month: typed[5..7].to_string(), year: typed[0..4].to_string() };
let check = validate_date_parts(&parts, label, rules);
if check.valid {
IsoDateCheck { valid: true, date: check.date, message: None }
} else {
IsoDateCheck { valid: false, date: None, message: check.message }
}
}
pub fn iso_date_check_to_value(check: &IsoDateCheck) -> Value {
let text = |v: &Option<String>| v.as_deref().map(Value::str).unwrap_or(Value::Null);
Value::obj(vec![("valid", Value::Bool(check.valid)), ("date", text(&check.date)), ("message", text(&check.message))])
}
pub fn fune_vector(args: &[Value]) -> Value {
let optional = |v: &Value, key: &str| match v.get(key) {
Value::Str(s) => Some(s.clone()),
_ => None,
};
let rules = DateRules {
today: optional(&args[3], "today"),
timing: optional(&args[3], "timing"),
not_before: optional(&args[3], "notBefore"),
not_after: optional(&args[3], "notAfter"),
};
iso_date_check_to_value(&validate_iso_date(args[0].as_str(), args[1].as_str(), args[2].as_bool(), &rules))
}Install
fune build
With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and its 1 dependency, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. 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 Rust implementation. Install it without the registry with fune add ./form.iso-date-1.0.0-rust.fune, or fetch it from a terminal with fune pull form.iso-date@1.0.0:rust.
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 |