Skip to content

Same month

sameMonth validates that a Date instance represents a date in the same calendar month (year and month) as the specified reference date.

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

Signature

Through the API:

.sameMonth(other: Date)

And internally:

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

Design rationale

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

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

Examples

Same calendar month

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

Different calendar month

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

Non-Date value

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