npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@victorfernandesraton/zod-phonenumber

v0.1.2

Published

Zod extension for phone number validation using libphonenumber-js

Readme

@victorfernandesraton/zod-phonenumber

npm CI License

Zod extension for phone number validation using libphonenumber-js.

Why this library?

Most phone validation libraries treat phone numbers as plain strings — you validate them, maybe format them, and move on. They don't leverage zod's composable, chainable API.

z.phone() is native to zod's ecosystem. You chain constraints directly on the schema — .country(), .ddi(), .ddd() — the same way you chain .min(), .max(), .email() on z.string(). And because it uses zod's refine + transform, the output is a fully typed PhoneNumber object from libphonenumber-js, ready to use.

  • Chainable zod schema.country('BR').ddd('11') feels like zod, not a wrapper
  • Returns PhoneNumber — not a boolean, not a reformatted string. Full object with .country, .nationalNumber, .formatInternational(), etc.
  • DDD support — Brazilian area code filtering, auto-sets country to BR
  • More chainables coming.type(), national-only validation, and other constraints planned
  • Strict TypeScript — zero any, zero @ts-ignore, fully type-safe
  • Zero confignpm install @victorfernandesraton/zod-phonenumber installs everything
import { z } from '@victorfernandesraton/zod-phonenumber'

const schema = z.object({
  name: z.string(),
  phone: z.phone().country('BR').ddd('11'),
})

const result = schema.parse({ name: 'Joao', phone: '+5511999988888' })
result.phone.country  // 'BR'
result.phone.number   // '+5511999988888'

Install

npm install @victorfernandesraton/zod-phonenumber

Usage

Import patterns

Pattern A — Re-export (recommended):

import { z } from '@victorfernandesraton/zod-phonenumber'

const phone = z.phone().country('BR').parse('+5511999988888')

Pattern B — Named import:

import { z } from 'zod'
import { phone } from '@victorfernandesraton/zod-phonenumber'

const schema = z.object({
  phone: phone().country('BR').ddd('11'),
})

Basic validation

z.phone() validates any international phone number and returns a PhoneNumber object:

const phone = z.phone().parse('+12133734253')
phone.country              // 'US'
phone.number               // '+12133734253'
phone.nationalNumber       // '2133734253'
phone.countryCallingCode   // '1'
phone.isPossible()         // true
phone.formatInternational() // '+1 213 373 4253'
phone.formatNational()     // '(213) 373-4253'

Invalid numbers throw ZodError:

z.phone().parse('not-a-phone')  // throws ZodError

Country filtering

Restrict to a specific country:

z.phone().country('BR').parse('+5511999988888')  // ok
z.phone().country('BR').parse('+12133734253')    // throws

Or pass as constructor argument:

z.phone('BR').parse('+5511999988888')  // ok
z.phone('BR').parse('+12133734253')    // throws

DDI (international calling code)

Filter by country calling code:

z.phone().ddi('+55').parse('+5511999988888')   // ok
z.phone().ddi('55').parse('+5511999988888')    // ok (without +)
z.phone().ddi('+55').parse('+12133734253')     // throws

DDD (Brazilian area code)

Filter by Brazilian DDD (2-digit area code). Automatically sets country to 'BR':

z.phone().ddd('11')   // Sao Paulo
z.phone().ddd('21')   // Rio de Janeiro
z.phone().ddd('31')   // Belo Horizonte

z.phone().ddd('11').parse('+5511999988888')  // ok
z.phone().ddd('11').parse('+5521999988888')  // throws (wrong area code)

Chaining

Methods are chainable:

z.phone().country('BR').ddd('11')
z.phone().ddi('+55').ddd('11')
z.phone('BR').ddd('11')

Integration with z.object()

const userSchema = z.object({
  name: z.string(),
  phone: z.phone().country('BR').ddd('11'),
})

const user = userSchema.parse({
  name: 'Maria',
  phone: '+5511999988888',
})
// user.phone is a PhoneNumber object

API

z.phone(country?)

Creates a phone number schema. Returns a schema with methods:

| Method | Description | |--------|-------------| | .parse(data) | Validates and returns PhoneNumber. Throws on invalid | | .safeParse(data) | Returns { success, data } or { success, error } | | .country(code) | Filters by country ISO code | | .ddi(code) | Filters by international calling code | | .ddd(code) | Filters by Brazilian area code (auto-sets BR) |

Output: PhoneNumber

schema.parse() returns a PhoneNumber instance from libphonenumber-js:

| Property/Method | Description | |-----------------|-------------| | .country | ISO country code | | .number | E.164 format | | .nationalNumber | National significant number | | .countryCallingCode | Country calling code | | .isPossible() | Validates length | | .isValid() | Full validation (needs max metadata) | | .getType() | Phone type (needs max metadata) | | .formatInternational() | e.g. '+55 11 99999 8888' | | .formatNational() | e.g. '(11) 99999-8888' | | .getURI() | e.g. 'tel:+5511999988888' |

License

BSD-3-Clause