date
A Date object with a valid date.
Signature
ts
function date(msg?: Message): Schema<Date>Parameters
| Parameter | Type | Description |
|---|---|---|
msg | Message | Custom message. Replaces the default messages for all errors of this schema |
Accepts
A Date instance whose getTime() is not NaN. new Date('abc') is rejected.
undefined, null and '' are treated as empty — see Empty values.
Error codes
| Code | When | Default message |
|---|---|---|
required | The value is empty | Required |
date | Not a Date or an invalid date | Must be a valid date |
You can change the texts: see Messages & i18n.
Dates from JSON
JSON has no date type — dates come from the client as strings ("2026-10-02"). Use coerce.date, it creates a Date from a string or a number.
Example
Period
ts
const Period = object({
from: coerce.date(),
to: pipe(coerce.date(), check((v, { parent }) => v >= new Date(parent.from), 'End is before start')),
});
validate(Period, { from: '2026-01-10', to: '2026-01-01' }).errors; // { to: 'End is before start' }See also
- coerce.date — date from a string
- check — comparing dates