Is phone¶
isPhone validates that a value is a loosely formatted phone number.
It provides a flexible, user‑friendly check suitable for general input fields: the value must be a string, and after removing all non‑digit characters, at least seven digits must remain. Any non‑string value or digit‑stripped string shorter than seven digits results in a single validation event. Otherwise, it produces no validation output.
Signature¶
Through the API:
.isPhone();
And internally:
export const isPhone: ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>
Events¶
| Event code | Description |
|---|---|
type.not.valid |
Value is not a string. |
string.not.phone |
String does not contain at least seven digits. |
Design rationale¶
- Provides a permissive, user‑friendly phone validator appropriate for common form fields.
- Rejects non‑string values early with a structural‑type diagnostic.
- Strips non‑digit characters to accommodate formatting variations (
(555) 123‑4567,555‑1234, etc.). - Enforces a minimal digit count (7) to avoid obviously invalid numbers.
- Emits exactly one event per failure for clarity and composability.
- Pure, total, async‑compatible, and returns a readonly array of
JaneEventobjects. - Preserves the provided path and supports pipeline‑level
userMessageoverrides.
Invoke¶
isPhone 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 digit‑stripped string has fewer than seven digits, emits
string.not.phone. - Otherwise, returns an empty result set.
Examples¶
Valid — loosely formatted phone numbers¶
await isPhone("(555) 123‑4567", "$");
// → []
await isPhone("555‑1234", "$");
// → []
Invalid — non‑string values¶
await isPhone(1234567, "$");
// → [
// JaneEvent{
// kind: "error",
// code: "type.not.valid",
// path: "$",
// ...
// }
// ]
Invalid — insufficient digits¶
await isPhone("123‑45", "$");
// → [
// JaneEvent{
// kind: "error",
// code: "string.not.phone",
// path: "$",
// ...
// }
// ]