Dates and times
Date schemas parse strings into the types of a date library. Each library is an additive feature with its own module:
| Feature | Module | date() output | datetime() output | time() output |
|---|---|---|---|---|
chrono | vld::chrono | chrono::NaiveDate | chrono::DateTime<Utc> | chrono::NaiveTime |
jiff | vld::jiff | jiff::civil::Date | jiff::Timestamp | jiff::civil::Time |
time | vld::time | time::Date | time::OffsetDateTime (UTC) | time::Time |
Each module exports date(), datetime(), time() and the ZDate, ZDateTime, ZTime aliases.
// features = ["chrono"]
use vld::prelude::*;
let birthday = vld::chrono::date().min("1900-01-01").past();
let d = birthday.parse(r#""1990-06-15""#).unwrap();
assert_eq!(d, chrono::NaiveDate::from_ymd_opt(1990, 6, 15).unwrap());
Shortcuts with one backend
When exactly one date feature is enabled, vld::date(), vld::datetime(), vld::time_of_day() and the prelude types ZDate, ZDateTime, ZTime point to it. They disappear as soon as a second backend is enabled, for example by another crate in your dependency tree through feature unification.
vld::chrono::date()) so they keep compiling when an application enables another backend.Grammar
All backends share one strict grammar:
| Schema | Accepts | Examples |
|---|---|---|
date() | YYYY-MM-DD with a real calendar day | 2024-02-29 |
datetime() | YYYY-MM-DDTHH:MM:SS[.fraction] plus Z or ±HH:MM; T / t and Z / z | 2024-01-15T10:30:00Z, 2024-01-15T10:30:00.123+03:00 |
time() | HH:MM:SS[.fraction], no offset | 09:30:00, 23:59:59.5 |
Seconds are required in datetimes and times. The output of datetime() is always normalized to UTC.
Naive datetimes and offsets
By default datetime() also accepts naive input without an offset (2024-01-15T10:30:00) and reads it as UTC.
| Method | Effect |
|---|---|
naive_allowed(false) / with_timezone_only() | require an offset (RFC 3339) |
naive_timezone_offset(secs) | read naive input at this offset instead of UTC; panics if abs(secs) >= 86400 |
timezone_offset_only(secs) | require an explicit offset equal to secs (naive input is rejected too); invalid_timezone_offset otherwise |
// Moscow clocks: naive input is UTC+3, output is UTC.
let local = vld::chrono::datetime().naive_timezone_offset(3 * 3600);
let utc = local.parse(r#""2024-01-15T10:30:00""#).unwrap();
assert_eq!(utc.to_rfc3339(), "2024-01-15T07:30:00+00:00");
let strict = vld::chrono::datetime().with_timezone_only();
assert!(strict.parse(r#""2024-01-15T10:30:00""#).is_err());
Ranges, past and future
| Method | date() | datetime() | time() |
|---|---|---|---|
min(&str) / max(&str) | "YYYY-MM-DD" | RFC 3339 with offset | "HH:MM:SS" |
| typed bounds | min_date / max_date | min_datetime / max_datetime | min_time / max_time |
past() / future() | before / after today (UTC) | before / after now | — |
past_msg(..) / future_msg(..) | with a custom message | with a custom message | — |
The string forms of min / max panic on an invalid literal, the same way a bad regex would: they are meant for constants. Out-of-range values report too_small / too_big with origin date, datetime or time; past/future failures report not_in_past / not_in_future.
let opening = vld::chrono::time().min("09:00:00").max("18:00:00");
assert!(opening.parse(r#""12:15:00""#).is_ok());
assert!(opening.parse(r#""20:00:00""#).is_err());
Unix timestamps
datetime().unix_seconds() / .unix_millis() read a Unix timestamp (a JSON integer) instead of an RFC 3339 string, into the same backend type (DateTime<Utc>, jiff::Timestamp, OffsetDateTime), on every backend.
unix_seconds() | unix_millis() | |
|---|---|---|
| range | -62167219200 ..= 253402207200 | -62167219200000 ..= 253402207200999 |
| beyond the range | invalid_date (datetime) | invalid_date |
a fraction (1.5) | not_int | not_int |
| a string | invalid_type (expected integer); with .coerce() digits with an optional - are accepted | same |
| JSON Schema | {"type": "integer", "format": "unix-time", "minimum", "maximum"} | format: unix-time-ms |
The range is the common one of all backends (0000-01-01T00:00:00Z ..= 9999-12-30T22:00:00Z). min / max (still RFC 3339 literals), past() and future() work as for strings; the JSON Schema's minimum / maximum are the range intersected with min / max (rounded inward), plus x-past / x-future. The offset options (with_timezone_only, …) do not apply. A with_default value prints as the integer.
// features = ["jiff"]
let created = vld::jiff::datetime().unix_seconds().past();
assert_eq!(created.parse("1700000000").unwrap().to_string(), "2023-11-14T22:13:20Z");
assert!(vld::jiff::datetime().unix_millis().coerce().parse(r#""1700000000123""#).is_ok());
Date ranges
vld::range(item) parses {"start": a, "end": b} with both ends parsed by item and a <= b, into std::ops::RangeInclusive<T>. It works with any schema whose output is ordered: every backend's date() / datetime() / time() (also Unix timestamps), numbers, decimals, durations. Ends are compared as parsed values, so 2024-01-01T10:00:00+02:00 is before 2024-01-01T09:00:00Z. Equal ends pass.
| Failure | Issue |
|---|---|
| end before start | range_order at the end field (params start, end: the field names) |
| a field missing / invalid | missing_field / the item's issue at that field (both fields are reported) |
| not an object | invalid_type |
// features = ["chrono"]
let stay = vld::range(vld::chrono::date()).keys("check_in", "check_out");
let r = stay.parse(r#"{"check_in": "2024-07-01", "check_out": "2024-07-08"}"#).unwrap();
assert_eq!(r.end().to_string(), "2024-07-08");
let err = stay.parse(r#"{"check_in": "2024-07-08", "check_out": "2024-07-01"}"#).unwrap_err();
assert_eq!(err.issues[0].path_string(), ".check_out");
assert_eq!(err.issues[0].code.key(), "range_order");
.keys(start, end) renames the fields (default start / end); with_messages takes the key range_order. Inside a larger object, use the range as a field (vld::object().field("stay", stay)). JSON Schema: an object with both fields required and "x-rangeOrder": [start, end] (the order is not expressible in JSON Schema; vld::fake generates ordered ends).
Dates as plain strings
If you want to keep the value a String and only check its shape, use the string formats iso_date(), iso_datetime() and iso_time(). iso_datetime() uses the same grammar as datetime().
JSON Schema
date() emits {"type": "string", "format": "date"}. datetime() emits format: date-time when an offset is required; by default (naive input allowed) it emits an anyOf of date-time and a pattern for the naive form. time() emits a pattern (format: time would require an offset). min / max become formatMinimum / formatMaximum; past() / future() become x-past / x-future.