Require country context for a bare postcode
Use this guide when a user enters only a postcodepostcodeThe country-specific postcode (US ZIP, French code postal, etc.). Mailwoman handles postcode parsing entirely by rule classifier — a regex problem, not an ML one. and your application needs to decide whether it has enough context to attempt a geographic lookup. Keep the countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. supplied by your application or user beside the original input. A postcodepostcodeThe country-specific postcode (US ZIP, French code postal, etc.). Mailwoman handles postcode parsing entirely by rule classifier — a regex problem, not an ML one.'s shape can match several national formats, so the postcodepostcodeThe country-specific postcode (US ZIP, French code postal, etc.). Mailwoman handles postcode parsing entirely by rule classifier — a regex problem, not an ML one.'s countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. needs context beyond its shape.
Prerequisites
^24.22.3 || >=26.13.1"type": "module" in its package.jsonStart by installing the required packages:
- npm
- Yarn
- pnpm
- Bun
npm install mailwoman
yarn add mailwoman
pnpm add mailwoman
bun add mailwoman
This exercise parsesaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates. text and reads no geographic database. Save the following file as route-postcodes.ts.
Keep caller country separate from the parse
import { createParser, decodeAsJSON, flattenTreeNodes } from "mailwoman"
await using parser = await createParser({ locale: "en-US", machinePreferences: "none" })
const cases: Array<{ input: string; applicationCountry: string | null }> = [
{ input: "10118", applicationCountry: null },
{ input: "10118", applicationCountry: "US" },
{ input: "10118-1234", applicationCountry: null },
]
for (const example of cases) {
const resolverPreference = example.applicationCountry
? { country: example.applicationCountry, source: "caller" as const }
: null
const options = resolverPreference ? { resolveOpts: { defaultCountry: resolverPreference } } : undefined
const result = await parser.parse(example.input, options)
const { postcode } = decodeAsJSON(result.tree)
const route =
result.faults.length || result.kind.kind !== "postcode_only" || !postcode
? "review-parse"
: example.applicationCountry
? "country-scoped-lookup-pending-data"
: "request-country"
const formats = result.queryShape.knownFormats.map((match) => match.format).join(", ")
console.log(
`${example.input} (application country: ${example.applicationCountry ?? "none"})\n` +
`path: ${result.path}, kind: ${result.kind.kind}, formats: ${formats || "none"}, route: ${route}`
)
console.table(flattenTreeNodes(result.tree), ["tag", "value", "start", "end"])
}
The application owns applicationCountry. When it has US, the example also passes { country: "US", source: "caller" } through resolveOpts.defaultCountry, the resolverresolverThe component that converts parsed address components (locality, region, postcode) into coordinates by looking them up in the gazetteer. The resolver ranks candidates by name match, population, and proximity, and returns the best-matching place with its centroid or polygon.'s countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. preference. This exercise configures no resolverresolverThe component that converts parsed address components (locality, region, postcode) into coordinates by looking them up in the gazetteer. The resolver ranks candidates by name match, population, and proximity, and returns the best-matching place with its centroid or polygon., so the parseaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates. output can't show whether a lookup applied that preference. The policy reads the application field directly and marks a future countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.-scoped lookup as pending data. It requests a countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. when that field is absent, even when one format name looks specific to a countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head..
Run node route-postcodes.ts. The script prints this complete output:
10118 (application country: none)
path: full, kind: postcode_only, formats: us_zip, fr_postcode, de_postcode, route: request-country
┌─────────┬────────────┬─────────┬───────┬─────┐
│ (index) │ tag │ value │ start │ end │
├─────────┼────────────┼─────────┼───────┼─────┤
│ 0 │ 'postcode' │ '10118' │ 0 │ 5 │
└─────────┴────────────┴─────────┴───────┴─────┘
10118 (application country: US)
path: full, kind: postcode_only, formats: us_zip, fr_postcode, de_postcode, route: country-scoped-lookup-pending-data
┌─────────┬────────────┬─────────┬───────┬─────┐
│ (index) │ tag │ value │ start │ end │
├─────────┼────────────┼─────────┼───────┼─────┤
│ 0 │ 'postcode' │ '10118' │ 0 │ 5 │
└─────────┴────────────┴─────────┴───────┴─────┘
10118-1234 (application country: none)
path: fast-path, kind: postcode_only, formats: us_zip4, route: request-country
┌─────────┬────────────┬──────────────┬───────┬─────┐
│ (index) │ tag │ value │ start │ end │
├─────────┼────────────┼──────────────┼───────┼─────┤
│ 0 │ 'postcode' │ '10118-1234' │ 0 │ 10 │
└─────────┴────────────┴──────────────┴───────┴─────┘
Both 10118 runs returned kind postcode_only and the same postcode node. Both listed us_zip, fr_postcode, and de_postcode as format matches. The countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. US came from the application record, and the parser left the choice between those three shapes open. 10118-1234 matched only us_zip4 and took the fast path, and this policy still requests an application-supplied countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head..
The coordinate and deliverability of all three records stay unknown, because this exercise ran neither a geographic lookup nor a postal-authority check. With a verified data bundle, run the countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.-scoped lookup and inspect its candidate evidence, resolution_tier, and epistemic_status. A countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. preference narrows the lookup and supplies no coordinate itself. When a later lookup returns lat and lon as null, store them as null in your record. The result schema defines the fields, and the confidence and limits explanation separates format, place, and coordinate evidence.