phoneLast updated on
Last updated on
Build and validate phone numbers
Build and validate phone numbers. The Phone primitive captures phone numbers with optional type classification and extension.
Examples
Creating phone numbers
// Object pattern
const phone = open.phone({
number: '+14155552671'
})
// Builder pattern
const phone = open.phone()
.number('+14155552671')
.type('mobile')
.extension('123')
.build()
// Copy and modify
const updated = open.phone()
.from(phone)
.type('work')
.build()Parsing external data
// Parse unknown input (throws on error)
const phone = open.phone.parse(jsonData)
// Safe parsing (returns result object)
const result = open.phone.safeParse(jsonData)
if (result.success) {
console.log(`Phone: ${result.data.number}`)
} else {
console.error(result.error.message)
}Using with forms
const form = open.form({
name: 'contact',
fields: {
mobilePhone: { type: 'phone', label: 'Mobile Phone' },
workPhone: { type: 'phone', label: 'Work Phone' }
}
})
const filled = form.fill({
fields: {
mobilePhone: open.phone()
.number('+14155552671')
.type('mobile')
.build(),
workPhone: open.phone()
.number('+14155551234')
.type('work')
.extension('567')
.build()
}
})API
Object Pattern
open.phone(input: Phone): PhonePass a Phone object for validation. Returns the validated Phone or throws on error.
const phone = open.phone({ number: '+442071234567' })Builder Pattern
Chain methods to build a Phone value incrementally:
All methods return PhoneBuilder and are chainable.
open.phone()
number: (value: string) => PhoneBuilder
Set phone number (E.164 format recommended)
type: (value: string | undefined) => PhoneBuilder
Set phone type (mobile, home, work, etc.)
extension: (value: string | undefined) => PhoneBuilder
Set extension number
from: (value: Phone) => PhoneBuilder
Initialize from existing Phone
build: () => Phone
Build and validate
Static Methods
parse: (input: unknown) => Phone
Parse unknown input (throws on error)
safeParse: (input: unknown) => Result<Phone>
Parse unknown input (returns result object)
Properties
number: string
Phone number. E.164 format recommended (+[country][number]).
type: string?
Type of phone (mobile, home, work, fax).
extension: string?
Extension number for business lines.
E.164 Format
The recommended format for phone numbers is E.164:
+[country code][subscriber number]| Country | Format | Example |
|---|---|---|
| USA/Canada | +1XXXXXXXXXX | +14155552671 |
| UK | +44XXXXXXXXXX | +442071234567 |
| Germany | +49XXXXXXXXX | +4930123456 |
| Japan | +81XXXXXXXXX | +81312345678 |
| Australia | +61XXXXXXXXX | +61212345678 |
Phone Types
| Type | Description |
|---|---|
mobile | Mobile/cell phone |
home | Residential landline |
work | Business/office line |
fax | Fax machine |
other | Other phone type |
Validation
The Phone primitive validates:
numbermust be a non-empty stringtypeandextensionare optional
// Valid
open.phone({ number: '+14155552671' })
open.phone({
number: '+14155552671',
type: 'mobile'
})
open.phone({
number: '+14155551234',
type: 'work',
extension: '567'
})
// Invalid - throws Error
open.phone({})
// Error: Invalid Phone: number is required
open.phone({ number: '' })
// Error: Invalid Phone: number cannot be emptyRelated
- Primitives Overview - All available primitives
- Address - Physical address
- Phone Field - Phone field type
- Phone Schema - JSON Schema definition