Skip to content
API Now! is currently in closed beta. We are constantly updating these guides as we release updates!

PostalCode (Semantic Module)

← Back to Modules Reference

The PostalCode semantic module validates, formats, and sanitizes postal codes and ZIP codes (such as "94105" or "EC1A 1BB").

Postal code structures vary dramatically across the globe. Rather than applying a single rigid pattern, the PostalCode semantic connects dynamically with the sibling Country field on the same entity to validate the exact formatting required by that nation.


AttributeSpecification
ScopeProperty (Applied to a text property)
Data TypeString
Default Max Length255 characters
Dynamic Country CheckSupported (Validates format based on sibling Country property)
Supported Countries25+ built-in country patterns (US, CA, GB, DE, FR, AU, JP, etc.) plus flexible alphanumeric fallback

1. Dynamic Geographic Validation (validate: true)

Section titled “1. Dynamic Geographic Validation (validate: true)”

When enabled, the Serverless Engine inspects the sibling property annotated with Country on the same record:

  • US (US): Requires 5 digits or 9 digits ZIP+4 format (e.g. 94105 or 94105-1234).
  • Canada (CA): Validates Canadian alphanumeric formatting (e.g. K1A 0B1).
  • United Kingdom (GB): Validates official Royal Mail alphanumeric formats (e.g. SW1A 1AA or EC1A 1BB).
  • European Union (DE, FR, IT, ES): Enforces exact 5-digit patterns.
  • Other Countries: Supports comprehensive built-in patterns for Australia, Japan, Brazil, India, Mexico, China, Poland, and more, falling back to standard 3-to-10 character alphanumeric codes.

During PUT or PATCH updates, if an API client updates only the postalCode without resending the country, the engine automatically references the existing database record to retrieve the country and validate the new code safely.

3. Allowed & Disallowed Lists (allowedPostalCodes, disallowedPostalCodes)

Section titled “3. Allowed & Disallowed Lists (allowedPostalCodes, disallowedPostalCodes)”

For hyperlocal delivery zones or service-area restrictions:

  • Allow specific ZIP codes to restrict checkout to supported neighborhoods.
  • Disallow remote or non-deliverable territory postal codes.

When configuring the PostalCode semantic in the Domain Modeler, you can adjust:

OptionTypeDefaultDescription
validatebooleantrueEnables dynamic validation against the sibling Country property.
allowedPostalCodesstring[][] (all allowed)Specific list of permitted postal codes.
disallowedPostalCodesstring[][] (none disallowed)Specific list of prohibited postal codes.

A record created with country "US" and a valid 5-digit ZIP code:

POST /addresses
Content-Type: application/json
{
"street": "1 Market St",
"city": "San Francisco",
"country": "US",
"postalCode": "94105"
}

Server Response (201 Created):

{
"data": {
"id": "addr_501",
"street": "1 Market St",
"city": "San Francisco",
"country": "US",
"postalCode": "94105"
}
}

2. Validation Failure (Mismatched Country Format)

Section titled “2. Validation Failure (Mismatched Country Format)”

A client provides a US ZIP code format, but specifies the country as "GB" (United Kingdom):

{
"country": "GB",
"postalCode": "94105"
}

Server Response (400 Bad Request):

{
"type": "https://docs.apinow.app/errors/validation",
"title": "Validation Error",
"status": 400,
"code": "E_VALIDATION_ERROR",
"detail": "Invalid postal code format for country: GB. Value: '94105'"
}

  • Pair with Country: For dynamic validation to function, the entity model must include a sibling property tagged with the Country semantic module.
  • Applicable Property Types: Apply to a String property in the Domain Modeler.
  • Length Constraints: The default Maximum Length of 255 characters provides ample headroom for international alphanumeric postal strings with spaces and hyphens.