Skip to content

Same day

sameDay validates that a Date instance represents the same calendar day (year, month, day) as the specified reference date.

It performs a strict type check ensuring the value is a valid Date instance, then compares the UTC year, month, and day components. If the value is not a Date, is invalid, or represents a different calendar day, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.sameDay(other: Date)

And internally:

export const sameDay = (other: Date): ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
date.not.date Value is not a Date instance
date.is.invalid Date instance represents an invalid date
date.not.same-day Date does not represent the same calendar day

Design rationale

  • Provides a strict, predictable same-calendar-day validation.
  • First ensures the value is a valid Date instance.
  • Then compares UTC year, month, and day components exactly.
  • Useful for date matching requirements where time-of-day is irrelevant.
  • Never coerces or normalizes — validation is explicit and opt-in.
  • Emits exactly one event per failure for clarity and composability.
  • Async-compatible and returns a readonly array of JaneEvent objects.

Invoke

sameDay runs only when explicitly included in a boundary or pipeline. It does not run automatically.

The rule activates when:

  • The value is any JavaScript value.
  • If the value is not a Date instance, emits date.not.date.
  • If the value is a Date but invalid, emits date.is.invalid.
  • If the value is a valid Date but different calendar day, emits date.not.same-day.
  • If the value is a valid Date on the same calendar day → returns an empty result set.

Examples

Same calendar day

const reference = new Date("2023-01-01T10:00:00Z");
await sameDay(reference)(new Date("2023-01-01T15:30:00Z"), "$");
// → []

Different calendar day

const reference = new Date("2023-01-01");
await sameDay(reference)(new Date("2023-01-02"), "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "date.not.same-day",
//       path: "$",
//       ...
//     }
//   ]

Non-Date value

const reference = new Date("2023-01-01");
await sameDay(reference)("2023-01-01", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "date.not.date",
//       path: "$",
//       ...
//     }
//   ]