Skip to content

Trim

trim is a built‑in normalization rule that removes leading and trailing whitespace from string values.

It normalizes text edges while preserving the internal content exactly as provided. If the value is not a string, or trimming does not change the value, no normalization step is emitted.

Signature

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

Events

Event code Description
string.now.trimmed Leading and trailing whitespace was removed.

Design rationale

  • Normalizes only edges of the string; internal whitespace is handled by other rules.
  • Produces consistent, predictable text for downstream validators, scanners, and policy layers.
  • Eliminates accidental spacing introduced by user input, copy‑paste operations, or UI frameworks.
  • Emits a normalization step only when the trimmed string differs from the original.
  • Classified as lossless normalization — no semantic content is removed.
  • Ensures clean text boundaries without altering user intent.

Invoke

trim runs automatically when:

  • The value is structurally a string.
  • The normalization stage is active.
  • The current mode allows lossless normalization.

Mode behavior:

  • strict: Rule does not run.
  • moderate: Rule runs (this rule is lossless).
  • lax: Rule runs.

Normalization cannot be invoked manually. It is always applied automatically based on structural type and mode.

Examples

Leading and trailing whitespace removed

const value = "   hello world   ";

const result = trim(value, "strict", "$");
// → [
//     {
//       path: "$",
//       nextValue: "hello world",
//       lossy: "lossless",
//       events: [
//         JaneEvent{ kind: "info", code: "string.now.trimmed", ... }
//       ]
//     }
//   ]

No trimming needed — no normalization

const result = trim("hello world", "strict", "$");
// → []

Non‑string value

const result = trim(42, "strict", "$");
// → []