Skip to content

Remove undefined keys

removeUndefinedKeys is a built‑in normalization rule that removes object keys whose values are undefined.

This produces a cleaner and more predictable object shape by stripping absent or uninitialized fields. If the value is not a plain object, or no keys contain undefined, no normalization step is emitted.

Signature

export const removeUndefinedKeys: NormalizationRule<unknown>
(value: unknown, mode: NormalizationMode, path: FieldPath) => NormalizationStep[]

Events

Event code Description
object.without.undefined-keys Keys with undefined values were removed.

Design rationale

  • Removes only keys whose values are exactly undefined.
  • Preserves all defined values, including null, empty strings, empty arrays, and empty objects (handled by other rules).
  • Eliminates uninitialized or absent fields commonly produced by partial form submissions, spread operations, or API hydration.
  • Emits a normalization step only when at least one undefined key is removed.
  • Classified as lossy normalization — the original undefined values are discarded.
  • Produces a stable, predictable object shape for downstream validators, scanners, and policy layers.

Invoke

removeUndefinedKeys runs automatically when:

  • The value is structurally a plain object (not an array).
  • The normalization stage is active.
  • The current mode allows lossy normalization.

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

Undefined keys removed

const value = {
  name: "Alice",
  age: undefined,
  meta: undefined
};

const result = removeUndefinedKeys(value, "lax", "$");
// → [
//     {
//       path: "$",
//       nextValue: { name: "Alice" },
//       lossy: "lossy",
//       events: [
//         JaneEvent{ kind: "info", code: "object.without.undefined-keys", ... }
//       ]
//     }
//   ]

No undefined values — no normalization

const result = removeUndefinedKeys({ a: 1, b: undefined }, "strict", "$");
// → []

Non‑object value

const result = removeUndefinedKeys("not an object", "lax", "$");
// → []