Skip to content

Commit 032ea1f

Browse files
author
Divyansh Singh
authored
feat(geo): add validateZipCode api in geo module [ROW-575] (#233)
* feat(geo): add validateZipCode function to geo module Introduce validateZipCode function to validate zip codes against supported countries. * docs: replace add validateZipCode API documentation * Create slimy-gifts-melt.md * fix(geo): export validateZipCode function from geo module
1 parent 98d1f5e commit 032ea1f

File tree

5 files changed

+1184
-2
lines changed

5 files changed

+1184
-2
lines changed

.changeset/slimy-gifts-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@razorpay/i18nify-js": patch
3+
---
4+
5+
feat(geo): add validateZipCode api in geo module [ROW-575]

packages/i18nify-js/README.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ console.log(
265265
locale: 'fr-FR',
266266
}),
267267
); /* {
268-
"integer": "12345",
268+
"integer": "12 345",
269269
"decimal": ",",
270270
"fraction": "67",
271271
"currency": "€",
@@ -277,7 +277,7 @@ console.log(
277277
},
278278
{
279279
"type": "group",
280-
"value": ""
280+
"value": " "
281281
},
282282
{
283283
"type": "integer",
@@ -1116,6 +1116,64 @@ try {
11161116
}
11171117
```
11181118

1119+
#### validateZipCode(zipcode, countryCode?) 🔍
1120+
1121+
The `validateZipCode` API helps you verify if a zipcode exists in a specific country or across all supported countries. This function is particularly useful for address validation, form verification, and ensuring postal codes are valid before processing.
1122+
1123+
##### Parameters
1124+
1125+
- `zipcode` (string): The zipcode to validate
1126+
- `countryCode` (optional, string): The country code to validate against. If not provided, searches across all supported countries.
1127+
1128+
##### Returns
1129+
1130+
Promise that resolves to:
1131+
1132+
- `true` if the zipcode exists in the specified country (or any country if no country code is provided)
1133+
- `false` if the zipcode is not found
1134+
1135+
##### Examples
1136+
1137+
```javascript
1138+
// Validating a zipcode in a specific country
1139+
const isValid = await validateZipCode('110001', 'IN');
1140+
console.log(isValid); // true (for Delhi, India)
1141+
1142+
// Validating a zipcode across all supported countries
1143+
const existsAnywhere = await validateZipCode('90001');
1144+
console.log(existsAnywhere); // true (if found in any country)
1145+
1146+
// Handling invalid zipcodes
1147+
const invalidZip = await validateZipCode('00000', 'IN');
1148+
console.log(invalidZip); // false
1149+
1150+
// Handling invalid country codes
1151+
try {
1152+
await validateZipCode('110001', 'XYZ');
1153+
} catch (error) {
1154+
console.error(error.message);
1155+
// Outputs: Invalid country code: XYZ. Please ensure you provide a valid country code.
1156+
}
1157+
1158+
// Handling empty zipcodes
1159+
try {
1160+
await validateZipCode('');
1161+
} catch (error) {
1162+
console.error(error.message);
1163+
// Outputs: Zipcode is required. Please provide a valid zipcode.
1164+
}
1165+
```
1166+
1167+
##### Error Handling
1168+
1169+
The API throws errors in the following cases:
1170+
1171+
- When the zipcode is empty or contains only whitespace
1172+
- When an invalid country code is provided
1173+
- When the API request fails while searching in a specific country
1174+
1175+
For multi-country searches (when no country code is provided), errors are silently handled by returning `false`, allowing the search to continue with other countries.
1176+
11191177
#### getFlagOfCountry(countryCode) 🏁
11201178

11211179
Source for flag images: [FlagCDN](https://flagcdn.com/).

0 commit comments

Comments
 (0)