Skip to content

One of

oneOf validates that a string is included in a provided list of allowed values.

It enforces strict structural string validation and rejects any non-string value or string not present in the allowed list. If the value is not a string or is not in the allowed values, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.oneOf(allowed: readonly string[])

And internally:

export const oneOf = (allowed: readonly string[]): ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
type.not.valid Value is not structurally a string
string.not.allowed String is not in the allowed values list

Design rationale

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

oneOf 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 not in the allowed list, emits string.not.allowed.
  • If the value is a string present in the allowed values → returns an empty result set.

Examples

Valid string in allowed list

await oneOf(["red", "blue", "green"])("blue", "$");
// → []

String not in allowed list

await oneOf(["red", "blue", "green"])("yellow", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.allowed",
//       path: "$",
//       ...
//     }
//   ]

Non-string value

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