Invalid Date to undefined¶
invalidDateToUndefined is a built‑in normalization rule that converts invalid Date instances into undefined.
This prevents malformed or unparsable date values from propagating into downstream validators, scanners, or policy layers.
If the value is not a Date, or the Date is valid, no normalization step is emitted.
Signature¶
export const invalidDateToUndefined: NormalizationRule<unknown>
(value: unknown, mode: NormalizationMode, path: FieldPath) => NormalizationStep[]
Events¶
| Event code | Description |
|---|---|
date.now.undefined |
Invalid Date instance was converted. |
Design rationale¶
- Ensures that malformed Date objects never reach validators or business logic.
- Treats invalid Date instances (
getTime()andNaN) as semantically equivalent to "no value". - Emits a normalization step only when the input is an invalid Date.
- Classified as
lossynormalization — the original value is discarded. - Prevents subtle bugs caused by UI frameworks, serialization boundaries, or user input producing invalid Date objects.
Invoke¶
invalidDateToUndefined runs automatically when:
- The value is a Date instance.
- The normalization stage is active.
- The current mode allows
lossynormalization.
Mode behavior:
strict: Rule does not run (lossy).moderate: Rule does not run (lossy).lax: Rule runs.
Normalization cannot be invoked manually. It is always applied automatically based on structural type and mode.
Examples¶
Invalid Date converted to undefined¶
const value = new Date("not a real date"); // timestamp → NaN
const result = invalidDateToUndefined(value, "lax", "$");
// → [
// {
// path: "$",
// nextValue: undefined,
// lossy: "lossy",
// events: [
// JaneEvent{ kind: "info", code: "date.now.undefined", ... }
// ]
// }
// ]
Valid Date — no normalization¶
const result = invalidDateToUndefined(new Date(), "lax", "$");
// → []
Non‑Date value¶
const result = invalidDateToUndefined("2024-01-01", "lax", "$");
// → []