Check parsed records against an application policy
Use this procedure when an application needs a stated completeness check before it accepts a parsed US streetstreetThe named linear feature along which house numbers are ordered. Decomposes into a name plus street affixes; one of the Tier 2 fine labels. address. The example requires one house_number, street, locality, region, and postcode component. It then checks 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 US ZIP Code shape. These are application requirements for this example, separate from the rules the postal service applies.
The check returns pass when all required components appear once and 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. has ZIP Code shape. It returns fail when those components appear and 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. has the wrong shape. It returns unknown when the input, 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., 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. evidence, or a required component is missing or ambiguous. A parsed component can be wrong, so pass establishes only that this policy's presence and shape checks succeeded.
Prerequisites
^24.22.3 || >=26.13.1"type": "module" in its package.jsonInstall the parser and the codex package, which holds the ZIP Code shape check:
- npm
- Yarn
- pnpm
- Bun
npm install mailwoman @mailwoman/codex
yarn add mailwoman @mailwoman/codex
pnpm add mailwoman @mailwoman/codex
bun add mailwoman @mailwoman/codex
The parser and ZIP Code shape check run without a geocodinggeocodingThe process of converting an address into geographic coordinates (latitude and longitude). Mailwoman geocodes in a multi-tier cascade: exact address-point match → street interpolation → locality centroid. Each tier is progressively coarser but more widely available. database. Take countryCode: "US" from your application's own 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. field before using this policy, because 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. inferred from 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. shape would make the policy circular. Adapt the required tags and 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. check for other address types or countries.
1. Save the policy and examples
Save this complete file as validate-records.ts:
import { isZipCode } from "@mailwoman/codex/us"
import { createParser, groupTuplesByTag, type Parser } from "mailwoman"
interface AddressRecord {
id: string
raw: string | null
countryCode: string | null
}
interface PolicyResult {
status: "pass" | "fail" | "unknown"
reason: string
}
const required = ["house_number", "street", "locality", "region", "postcode"]
async function check(parser: Parser, record: AddressRecord): Promise<PolicyResult> {
if (record.countryCode !== "US")
return { status: "unknown", reason: "US policy requires an established US country code" }
if (typeof record.raw !== "string" || record.raw.trim() === "") {
return { status: "unknown", reason: "address input is missing" }
}
const result = await parser.parse(record.raw, { locale: "en-US" })
if (result.faults.length) return { status: "unknown", reason: "parser reported a stage fault" }
const components = groupTuplesByTag(result.tree)
const missing = required.filter((tag) => (components.get(tag)?.length ?? 0) !== 1)
if (missing.length) {
return { status: "unknown", reason: `required component is missing or repeated: ${missing.join(", ")}` }
}
const [postcode] = components.get("postcode") ?? []
if (!isZipCode(postcode)) return { status: "fail", reason: `postcode ${postcode} fails US ZIP Code shape` }
return { status: "pass", reason: "required components are present and postcode has US ZIP Code shape" }
}
const records: AddressRecord[] = [
{ id: "complete", raw: "350 5th Ave, New York, NY 10118", countryCode: "US" },
{ id: "bad-shape", raw: "350 5th Ave, New York, NY 1011", countryCode: "US" },
{ id: "no-postcode", raw: "350 5th Ave, New York, NY", countryCode: "US" },
]
await using parser = await createParser()
for (const record of records) {
const { status, reason } = await check(parser, record)
console.log(`${record.id}: ${status} — ${reason}`)
}
groupTuplesByTag collects every value under its tag, nested nodes and repeated tags included. The policy treats a missing or repeated required tag as unknown, because 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. then holds no single value for that requirement. A parser stagestageOne of the dataflow stages in the runtime pipeline (normalize, locale check, kind classify, phrase group, token classify, sequence correct, reconcile, resolve). Distinct from tier (model vocabulary) and phase (plan milestone). fault also produces unknown. An exception while loading or running the parser stops the script, so an operational failure surfaces as an error instead of a policy result.
2. Run the check
node validate-records.ts
The script prints this output:
complete: pass — required components are present and postcode has US ZIP Code shape
bad-shape: fail — postcode 1011 fails US ZIP Code shape
no-postcode: unknown — required component is missing or repeated: postcode
The parser returned all five required tags for 350 5th Ave, New York, NY 10118. It returned postcode: "1011" for 350 5th Ave, New York, NY 1011, and isZipCode("1011") returned false. It returned no postcode node for 350 5th Ave, New York, NY, so the policy reported that requirement as unknown.
3. Apply the result to your workflow
Accept pass only for the stated presence and 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.-shape policy. Send fail to correction with the recorded reason. Request the missing field or review 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. when the result is unknown. Keep the original input with the outcome so a reviewer can check what the parser read.
A ZIP Code can have valid syntax and still be unassigned. Whether a building exists or receives mail is a question for postal and geographic data. A geocode uses separate geographic data and reports its own candidate and coordinate evidence. Read confidence and limits before adding geocode evidence to an acceptance policy, and use the result schema to inspect component tagscomponent tagOne of the 25 labels in Mailwoman's address schema — street, locality, region, postcode, house_number, unit, po_box, country, venue, intersection, and others. Each parsed span carries exactly one component tag. and faults.