Skip to content

Commit 58b5c9b

Browse files
committed
Added postal-zip code
Top 25 countries supported for postal code.
1 parent 46bb5f6 commit 58b5c9b

File tree

5 files changed

+349
-3
lines changed

5 files changed

+349
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ npm install validate-functions
1515
- **Email Validation**: Ensures that email addresses conform to standard formats.
1616
- **SSN Validation**: Validates U.S. Social Security Numbers based on predefined rules.
1717
- **Aadhaar Validation**: Checks the validity of Indian Aadhaar numbers using specific patterns.
18-
- **Credit Card Validation**: The credit card validation uses the Luhn algorithm to verify the validity of credit card numbers structurally.
19-
- **IP Validation**: Validates IPv4 and IPv6 specififcations.
20-
- **Hex Color Validation**: Validates string adheres to the standard format of a hexadecimal color code
18+
- **Credit Card Validation**: Validate using the Luhn algorithm.
19+
- **IP Validation**: Validate IPv4 and IPv6 formats.
20+
- **Hex Color Validation**: Validate #RRGGBB or #RGB color formats.
21+
- **Postal/ZIP Code Validation**: Validate based on country-specific formats. It supports top 25 countries based on ISO 3166-1 alpha-2 codes namely: US, CN, IN, ID, PK, BR, NG, BD, RU, MX, JP, ET, PH, EG, VN, CD, TR, IR, DE, TH, GB, FR, IT, TZ, ZA.
2122

2223

2324
Usage details are available [here](./USAGE_DETAILS.md).

USAGE_DETAILS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,14 @@ console.log(isValid_Hex_Color(hexColor)); // Output: true if valid, false otherw
6464

6565
const hexColor6 = '#abcdef'; // Example of a valid 6-digit hex color
6666
console.log(isValid_Hex_Color(hexColor6)); // Output: true if valid, false otherwise
67+
```
68+
69+
- **isValid_Postal_Code**
70+
71+
```typescript
72+
import { isValid_Postal_Code, CountryCode } from 'validate-functions/postal-zip';
73+
74+
// India
75+
const indianPostalCode = '110001';
76+
console.log(isValid_Postal_Code(indianPostalCode, CountryCode.IN)); // Output: true
6777
```

src/__tests__/postal-zip.test.ts

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
import { isValid_Postal_Code, CountryCode } from '../postal-zip';
2+
3+
describe('Postal Code Validation', () => {
4+
describe('United States', () => {
5+
it('should return true for a valid 5-digit US postal code', () => {
6+
expect(isValid_Postal_Code('12345', CountryCode.US)).toBe(true);
7+
});
8+
9+
it('should return true for a valid 9-digit US postal code', () => {
10+
expect(isValid_Postal_Code('12345-6789', CountryCode.US)).toBe(true);
11+
});
12+
13+
it('should return false for an invalid US postal code', () => {
14+
expect(isValid_Postal_Code('1234', CountryCode.US)).toBe(false);
15+
});
16+
});
17+
18+
describe('China', () => {
19+
it('should return true for a valid Chinese postal code', () => {
20+
expect(isValid_Postal_Code('100000', CountryCode.CN)).toBe(true);
21+
});
22+
23+
it('should return false for an invalid Chinese postal code', () => {
24+
expect(isValid_Postal_Code('10000', CountryCode.CN)).toBe(false);
25+
});
26+
});
27+
28+
describe('India', () => {
29+
it('should return true for a valid Indian postal code', () => {
30+
expect(isValid_Postal_Code('110001', CountryCode.IN)).toBe(true);
31+
});
32+
33+
it('should return false for an invalid Indian postal code', () => {
34+
expect(isValid_Postal_Code('11000', CountryCode.IN)).toBe(false);
35+
});
36+
});
37+
38+
describe('Indonesia', () => {
39+
it('should return true for a valid Indonesian postal code', () => {
40+
expect(isValid_Postal_Code('12345', CountryCode.ID)).toBe(true);
41+
});
42+
43+
it('should return false for an invalid Indonesian postal code', () => {
44+
expect(isValid_Postal_Code('1234', CountryCode.ID)).toBe(false);
45+
});
46+
});
47+
48+
describe('Pakistan', () => {
49+
it('should return true for a valid Pakistani postal code', () => {
50+
expect(isValid_Postal_Code('12345', CountryCode.PK)).toBe(true);
51+
});
52+
53+
it('should return false for an invalid Pakistani postal code', () => {
54+
expect(isValid_Postal_Code('1234', CountryCode.PK)).toBe(false);
55+
});
56+
});
57+
58+
describe('Brazil', () => {
59+
it('should return true for a valid Brazilian postal code', () => {
60+
expect(isValid_Postal_Code('01000-000', CountryCode.BR)).toBe(true);
61+
});
62+
63+
it('should return false for an invalid Brazilian postal code', () => {
64+
expect(isValid_Postal_Code('01000000', CountryCode.BR)).toBe(false);
65+
});
66+
});
67+
68+
describe('Nigeria', () => {
69+
it('should return true for a valid Nigerian postal code', () => {
70+
expect(isValid_Postal_Code('123456', CountryCode.NG)).toBe(true);
71+
});
72+
73+
it('should return false for an invalid Nigerian postal code', () => {
74+
expect(isValid_Postal_Code('12345', CountryCode.NG)).toBe(false);
75+
});
76+
});
77+
78+
describe('Bangladesh', () => {
79+
it('should return true for a valid Bangladeshi postal code', () => {
80+
expect(isValid_Postal_Code('1234', CountryCode.BD)).toBe(true);
81+
});
82+
83+
it('should return false for an invalid Bangladeshi postal code', () => {
84+
expect(isValid_Postal_Code('123', CountryCode.BD)).toBe(false);
85+
});
86+
});
87+
88+
describe('Russia', () => {
89+
it('should return true for a valid Russian postal code', () => {
90+
expect(isValid_Postal_Code('123456', CountryCode.RU)).toBe(true);
91+
});
92+
93+
it('should return false for an invalid Russian postal code', () => {
94+
expect(isValid_Postal_Code('12345', CountryCode.RU)).toBe(false);
95+
});
96+
});
97+
98+
describe('Mexico', () => {
99+
it('should return true for a valid Mexican postal code', () => {
100+
expect(isValid_Postal_Code('12345', CountryCode.MX)).toBe(true);
101+
});
102+
103+
it('should return false for an invalid Mexican postal code', () => {
104+
expect(isValid_Postal_Code('1234', CountryCode.MX)).toBe(false);
105+
});
106+
});
107+
108+
describe('Japan', () => {
109+
it('should return true for a valid Japanese postal code', () => {
110+
expect(isValid_Postal_Code('123-4567', CountryCode.JP)).toBe(true);
111+
});
112+
113+
it('should return false for an invalid Japanese postal code', () => {
114+
expect(isValid_Postal_Code('1234567', CountryCode.JP)).toBe(false);
115+
});
116+
});
117+
118+
describe('Ethiopia', () => {
119+
it('should return true for a valid Ethiopian postal code', () => {
120+
expect(isValid_Postal_Code('1234', CountryCode.ET)).toBe(true);
121+
});
122+
123+
it('should return false for an invalid Ethiopian postal code', () => {
124+
expect(isValid_Postal_Code('123', CountryCode.ET)).toBe(false);
125+
});
126+
});
127+
128+
describe('Philippines', () => {
129+
it('should return true for a valid Philippine postal code', () => {
130+
expect(isValid_Postal_Code('1234', CountryCode.PH)).toBe(true);
131+
});
132+
133+
it('should return false for an invalid Philippine postal code', () => {
134+
expect(isValid_Postal_Code('123', CountryCode.PH)).toBe(false);
135+
});
136+
});
137+
138+
describe('Egypt', () => {
139+
it('should return true for a valid Egyptian postal code', () => {
140+
expect(isValid_Postal_Code('12345', CountryCode.EG)).toBe(true);
141+
});
142+
143+
it('should return false for an invalid Egyptian postal code', () => {
144+
expect(isValid_Postal_Code('1234', CountryCode.EG)).toBe(false);
145+
});
146+
});
147+
148+
describe('Vietnam', () => {
149+
it('should return true for a valid Vietnamese postal code', () => {
150+
expect(isValid_Postal_Code('123456', CountryCode.VN)).toBe(true);
151+
});
152+
153+
it('should return false for an invalid Vietnamese postal code', () => {
154+
expect(isValid_Postal_Code('12345', CountryCode.VN)).toBe(false);
155+
});
156+
});
157+
158+
describe('Democratic Republic of the Congo', () => {
159+
it('should return true for a valid Congolese postal code', () => {
160+
expect(isValid_Postal_Code('12345', CountryCode.CD)).toBe(true);
161+
});
162+
163+
it('should return false for an invalid Congolese postal code', () => {
164+
expect(isValid_Postal_Code('1234', CountryCode.CD)).toBe(false);
165+
});
166+
});
167+
168+
describe('Turkey', () => {
169+
it('should return true for a valid Turkish postal code', () => {
170+
expect(isValid_Postal_Code('12345', CountryCode.TR)).toBe(true);
171+
});
172+
173+
it('should return false for an invalid Turkish postal code', () => {
174+
expect(isValid_Postal_Code('1234', CountryCode.TR)).toBe(false);
175+
});
176+
});
177+
178+
describe('Iran', () => {
179+
it('should return true for a valid Iranian postal code', () => {
180+
expect(isValid_Postal_Code('1234567890', CountryCode.IR)).toBe(true);
181+
});
182+
183+
it('should return false for an invalid Iranian postal code', () => {
184+
expect(isValid_Postal_Code('123456789', CountryCode.IR)).toBe(false);
185+
});
186+
});
187+
188+
describe('Germany', () => {
189+
it('should return true for a valid German postal code', () => {
190+
expect(isValid_Postal_Code('12345', CountryCode.DE)).toBe(true);
191+
});
192+
193+
it('should return false for an invalid German postal code', () => {
194+
expect(isValid_Postal_Code('1234', CountryCode.DE)).toBe(false);
195+
});
196+
});
197+
198+
describe('Thailand', () => {
199+
it('should return true for a valid Thai postal code', () => {
200+
expect(isValid_Postal_Code('12345', CountryCode.TH)).toBe(true);
201+
});
202+
203+
it('should return false for an invalid Thai postal code', () => {
204+
expect(isValid_Postal_Code('1234', CountryCode.TH)).toBe(false);
205+
});
206+
});
207+
208+
describe('United Kingdom', () => {
209+
it('should return true for a valid UK postal code', () => {
210+
expect(isValid_Postal_Code('W1A 1AA', CountryCode.GB)).toBe(true);
211+
});
212+
213+
it('should return false for an invalid UK postal code', () => {
214+
expect(isValid_Postal_Code('12345', CountryCode.GB)).toBe(false);
215+
});
216+
});
217+
218+
describe('France', () => {
219+
it('should return true for a valid French postal code', () => {
220+
expect(isValid_Postal_Code('75001', CountryCode.FR)).toBe(true);
221+
});
222+
223+
it('should return false for an invalid French postal code', () => {
224+
expect(isValid_Postal_Code('7500', CountryCode.FR)).toBe(false);
225+
});
226+
});
227+
228+
describe('Italy', () => {
229+
it('should return true for a valid Italian postal code', () => {
230+
expect(isValid_Postal_Code('00100', CountryCode.IT)).toBe(true);
231+
});
232+
233+
it('should return false for an invalid Italian postal code', () => {
234+
expect(isValid_Postal_Code('0010', CountryCode.IT)).toBe(false);
235+
});
236+
});
237+
238+
describe('Tanzania', () => {
239+
it('should return true for a valid Tanzanian postal code', () => {
240+
expect(isValid_Postal_Code('12345', CountryCode.TZ)).toBe(true);
241+
});
242+
243+
it('should return false for an invalid Tanzanian postal code', () => {
244+
expect(isValid_Postal_Code('1234', CountryCode.TZ)).toBe(false);
245+
});
246+
});
247+
248+
describe('South Africa', () => {
249+
it('should return true for a valid South African postal code', () => {
250+
expect(isValid_Postal_Code('1234', CountryCode.ZA)).toBe(true);
251+
});
252+
253+
it('should return false for an invalid South African postal code', () => {
254+
expect(isValid_Postal_Code('123', CountryCode.ZA)).toBe(false);
255+
});
256+
});
257+
258+
describe('Invalid Country Code', () => {
259+
it('should return false for an unsupported country code', () => {
260+
expect(isValid_Postal_Code('awea23s', 'UN' as CountryCode)).toBe(false);
261+
});
262+
});
263+
});

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './credit-card';
33
export * from './email';
44
export * from './ip';
55
export * from './hex-color';
6+
export * from './postal-zip'
67
export * from './ssn';

src/postal-zip.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Define the CountryCode enum
2+
export enum CountryCode {
3+
US = 'US', // United States
4+
CN = 'CN', // China
5+
IN = 'IN', // India
6+
ID = 'ID', // Indonesia
7+
PK = 'PK', // Pakistan
8+
BR = 'BR', // Brazil
9+
NG = 'NG', // Nigeria
10+
BD = 'BD', // Bangladesh
11+
RU = 'RU', // Russia
12+
MX = 'MX', // Mexico
13+
JP = 'JP', // Japan
14+
ET = 'ET', // Ethiopia
15+
PH = 'PH', // Philippines
16+
EG = 'EG', // Egypt
17+
VN = 'VN', // Vietnam
18+
CD = 'CD', // Democratic Republic of the Congo
19+
TR = 'TR', // Turkey
20+
IR = 'IR', // Iran
21+
DE = 'DE', // Germany
22+
TH = 'TH', // Thailand
23+
GB = 'GB', // United Kingdom
24+
FR = 'FR', // France
25+
IT = 'IT', // Italy
26+
TZ = 'TZ', // Tanzania
27+
ZA = 'ZA' // South Africa
28+
}
29+
30+
/**
31+
* Validates a postal code based on the country code.
32+
* @param postalCode - The postal code to validate.
33+
* @param countryCode - The country code as an enum.
34+
* @returns True if the postal code is valid for the given country, otherwise false.
35+
*/
36+
export const isValid_Postal_Code = (postalCode: string, countryCode: CountryCode): boolean => {
37+
const fiveDigitRegex = /^\d{5}$/;
38+
const sixDigitRegex = /^\d{6}$/;
39+
const fourDigitRegex = /^\d{4}$/;
40+
41+
const postalCodeRegexes: { [key in CountryCode]: RegExp } = {
42+
[CountryCode.US]: /^\d{5}(-\d{4})?$/,
43+
[CountryCode.CN]: sixDigitRegex,
44+
[CountryCode.IN]: sixDigitRegex,
45+
[CountryCode.ID]: fiveDigitRegex,
46+
[CountryCode.PK]: fiveDigitRegex,
47+
[CountryCode.BR]: /^\d{5}-\d{3}$/,
48+
[CountryCode.NG]: sixDigitRegex,
49+
[CountryCode.BD]: fourDigitRegex,
50+
[CountryCode.RU]: sixDigitRegex,
51+
[CountryCode.MX]: fiveDigitRegex,
52+
[CountryCode.JP]: /^\d{3}-\d{4}$/,
53+
[CountryCode.ET]: fourDigitRegex,
54+
[CountryCode.PH]: fourDigitRegex,
55+
[CountryCode.EG]: fiveDigitRegex,
56+
[CountryCode.VN]: sixDigitRegex,
57+
[CountryCode.CD]: fiveDigitRegex,
58+
[CountryCode.TR]: fiveDigitRegex,
59+
[CountryCode.IR]: /^\d{10}$/,
60+
[CountryCode.DE]: fiveDigitRegex,
61+
[CountryCode.TH]: fiveDigitRegex,
62+
[CountryCode.GB]: /^[A-Za-z]{1,2}\d[A-Za-z\d]? \d[A-Za-z]{2}$/,
63+
[CountryCode.FR]: fiveDigitRegex,
64+
[CountryCode.IT]: fiveDigitRegex,
65+
[CountryCode.TZ]: fiveDigitRegex,
66+
[CountryCode.ZA]: fourDigitRegex
67+
};
68+
69+
const regex = postalCodeRegexes[countryCode];
70+
return regex ? regex.test(postalCode) : false;
71+
};

0 commit comments

Comments
 (0)