Skip to content

Same year

sameYear validates that a Date instance represents a date in the same calendar year as the specified reference date.

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

Signature

Through the API:

.sameYear(other: Date)

And internally:

export const sameYear = (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-year Date does not represent the same calendar year

Design rationale

  • Provides a strict, predictable same-calendar-year validation.
  • First ensures the value is a valid Date instance.
  • Then compares UTC year component exactly.
  • Useful for year-based grouping or validation requirements.
  • 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

sameYear 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 year, emits date.not.same-year.
  • If the value is a valid Date in the same calendar year → returns an empty result set.

Examples

Same calendar year

const reference = new Date("2023-06-15");
await sameYear(reference)(new Date("2023-01-01"), "$");
// → []

Different calendar year

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

Non-Date value

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