Skip to content

Is country code

isCountryCode validates that a value is an ISO‑3166‑1 alpha‑2 country code.

It enforces strict structural and format requirements: the value must be a string, and it must match the canonical two‑letter uppercase pattern. Any non‑string value or incorrectly formatted code results in a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.isCountryCode();

And internally:

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

Events

Event code Description
type.not.valid Value is not a string.
string.not.country-code String does not match ISO‑3166‑1 alpha‑2 format.

Design rationale

  • Enforces strict ISO‑3166‑1 alpha‑2 formatting: exactly two uppercase letters.
  • Rejects non‑string values early with a structural‑type diagnostic.
  • Avoids normalization or case‑folding — contributors must provide canonical codes.
  • Emits exactly one event per failure for clarity and composability.
  • Pure, total, async‑compatible, and returns a readonly array of JaneEvent objects.
  • Preserves the provided path and supports pipeline‑level userMessage overrides.

Invoke

isCountryCode 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 string does not match /^[A-Z]{2}$/, emits string.not.country-code.
  • Otherwise, returns an empty result set.

Examples

Valid — canonical ISO‑3166‑1 alpha‑2 codes

await isCountryCode("US", "$");
// → []

await isCountryCode("DE", "$");
// → []

Invalid — non‑string values

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

Invalid — incorrect format

await isCountryCode("usa", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.country-code",
//       path: "$",
//       ...
//     }
//   ]


await isCountryCode("U1", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.country-code",
//       path: "$",
//       ...
//     }
//   ]