Skip to content

Matches

matches validates that a string matches a provided regular expression pattern.

It enforces strict structural string validation and rejects any non-string value or string that does not match the specified pattern. If the value is not a string or fails the pattern match, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.matches(pattern: RegExp)

And internally:

export const matches = (pattern: RegExp): ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
string.not.string Value is not structurally a string
string.is.not-match String does not match the required pattern

Design rationale

  • Provides flexible pattern-based string validation using regular expressions.
  • 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

matches 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 match the pattern, emits string.is.not-match.
  • If the value is a string that matches the pattern → returns an empty result set.

Examples

Valid string matching pattern

await matches(/^\d{3}-\d{2}-\d{4}$/)("123-45-6789", "$");
// → []

String not matching pattern

await matches(/^\d{3}-\d{2}-\d{4}$/)("invalid-ssn", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.is.not-match",
//       path: "$",
//       ...
//     }
//   ]

Non-string value

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