# Address

> Postal address validation with US zip code, ISO country code, and partial variant.

- Name: `address`
- Categories: common
- Detail page: https://open-types.dev/types/address

## Install

```bash
# Types only
npx shadcn add @open-types/address

# Types + Zod v4 validators
npx shadcn add @open-types/address-zod

# Types + real-world examples
npx shadcn add @open-types/address-examples
```

## Types

```typescript
export interface Address {
  street: string;
  street2?: string;
  city: string;
  state: string;
  postalCode: string;
  country: string;
}

export type PartialAddress = Partial<Address>;
```

## Zod validator

```typescript
import * as z from "zod";
import type { Address, PartialAddress } from "../types/address";

const US_ZIP_REGEX = /^\d{5}(-\d{4})?$/;
const ISO_COUNTRY_REGEX = /^[A-Za-z]{2}$/;

export const addressSchema = z.object({
  street: z.string().min(1, { error: "Street is required" }),
  street2: z.string().optional(),
  city: z.string().min(1, { error: "City is required" }),
  state: z.string().min(1, { error: "State is required" }),
  postalCode: z
    .string()
    .regex(US_ZIP_REGEX, { error: "Invalid US zip code (e.g. 12345 or 12345-6789)" }),
  country: z
    .string()
    .regex(ISO_COUNTRY_REGEX, { error: "Country must be a 2-letter ISO code" })
    .transform((val) => val.toUpperCase()),
}) satisfies z.ZodType<Address>;

export const partialAddressSchema = addressSchema.partial();

export type { Address, PartialAddress } from "../types/address";
```

## Examples

```typescript
// Source: hand-crafted
import type { Address, PartialAddress } from "../types/address";

const minimal = {
  street: "123 Main St",
  city: "Springfield",
  state: "IL",
  postalCode: "62704",
  country: "US",
} as const satisfies Address;

export const addressExamples = {
  minimal,
  full: {
    street: "1600 Pennsylvania Avenue NW",
    street2: "West Wing, Suite 100",
    city: "Washington",
    state: "DC",
    postalCode: "20500-0001",
    country: "US",
  } satisfies Address,
  zipPlusFour: { ...minimal, postalCode: "62704-1234" } satisfies Address,
  lowercaseCountry: { ...minimal, country: "us" } satisfies Address,
} as const;

export const partialAddressExamples = {
  empty: {} satisfies PartialAddress,
  cityOnly: { city: "Springfield" } satisfies PartialAddress,
  full: addressExamples.full satisfies PartialAddress,
} as const;
```
