Skip to content

Min length

minLength validates that a string contains at least a specified minimum number of characters.

It enforces strict structural string validation and rejects any non-string value or string whose length is smaller than the configured minimum. If the value is not a string or is too short, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.minLength(min: number)

And internally:

export const minLength = (min: number): ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
type.not.valid Value is not structurally a string
string.too.short String length is below the minimum threshold

Design rationale

  • Provides a strict, predictable string length validation.
  • Rejects non-string values with a clear structural-type diagnostic.
  • 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

minLength 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 string, emits type.not.valid.
  • If the value is a string but shorter than the minimum, emits string.too.short.
  • If the value is a string of sufficient length → returns an empty result set.

Examples

Valid string meeting minimum length

await minLength(3)("hello", "$");
// → []

String too short

await minLength(5)("hi", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.too.short",
//       path: "$",
//       ...
//     }
//   ]

Non-string value

await minLength(3)(42, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "type.not.valid",
//       path: "$",
//       ...
//     }
//   ]