Skip to content

Starts with

startsWith validates that a string begins with a specified prefix.

It enforces strict structural string validation and rejects any non-string value or string that does not begin with the required prefix. If the value is not a string or does not start with the prefix, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.startsWith(prefix: string)

And internally:

export const startsWith = (prefix: string): ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
string.not.string Value is not structurally a string
string.must.start-with String does not begin with the required prefix

Design rationale

  • Provides a strict, predictable string prefix 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

startsWith 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 string.not.string.
  • If the value is a string but does not start with the prefix, emits string.must.start-with.
  • If the value is a string that starts with the prefix → returns an empty result set.

Examples

Valid string with correct prefix

await startsWith("http")("https://example.com", "$");
// → []

String with wrong prefix

await startsWith("https")("http://example.com", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.must.start-with",
//       path: "$",
//       ...
//     }
//   ]

Non-string value

await startsWith("test")(42, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.string",
//       path: "$",
//       ...
//     }
//   ]