-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
yup-phone.test.ts
166 lines (147 loc) · 6.28 KB
/
yup-phone.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// eslint-disable-next-line import/no-unresolved
import * as yup from 'yup';
// const yup = require('yup');
// import './yup-phone';
require('../dist/yup-phone.cjs');
describe('yup-phone validation', () => {
it('validate all types of phone numbers', () => {
const phoneSchema = yup.string()
.phone()
.required();
expect(phoneSchema.isValidSync('9876543210')).toBe(true);
expect(phoneSchema.isValidSync('1-541-754-3010')).toBe(false);
expect(phoneSchema.isValidSync('19-49-89-636-48018')).toBe(false);
expect(phoneSchema.isValidSync('754-3010')).toBe(false);
expect(phoneSchema.isValidSync('+1-541-754-3010')).toBe(true);
expect(phoneSchema.isValidSync('+49-89-636-48018')).toBe(true);
expect(phoneSchema.isValidSync('+55 11 99999-5555')).toBe(true);
expect(phoneSchema.isValidSync('+593 7 282-3889')).toBe(true);
expect(phoneSchema.isValidSync('+44 871 222 1156')).toBe(true);
expect(phoneSchema.isValidSync('+447911123456')).toBe(true);
expect(phoneSchema.isValidSync('+1 345 9490088')).toBe(true);
expect(phoneSchema.isValidSync('+1 284 852 5500')).toBe(true);
expect(phoneSchema.isValidSync('+32 2 702-9200')).toBe(true);
expect(phoneSchema.isValidSync('+86 21 2230 1000')).toBe(true);
expect(phoneSchema.isValidSync('+821012345678')).toBe(true);
expect(phoneSchema.isValidSync('+65 6511 9266')).toBe(true);
});
it('validate phone number with IN (India) region', () => {
const phoneSchema = yup.string()
.phone('IN')
.required();
expect(phoneSchema.isValidSync('+19876543210')).toBe(false);
expect(phoneSchema.isValidSync('+919876543210')).toBe(true);
expect(phoneSchema.isValidSync('9876543210')).toBe(true);
expect(phoneSchema.isValidSync('+9124 4723300')).toBe(false);
expect(phoneSchema.isValidSync('+1 345 9490088')).toBe(true);
});
it('validate phone number strictly with IN (India) region', () => {
const phoneSchema = yup.string()
.phone('IN', true)
.required();
expect(phoneSchema.isValidSync('+1 345 9490088')).toBe(false);
});
it('validate phone number loosely with IN (India) region', () => {
const phoneSchema = yup.string()
.phone('IN', false)
.required();
expect(phoneSchema.isValidSync('+1 345 9490088')).toBe(true);
});
it('validate phone number with US (USA) region', () => {
const phoneSchema = yup.string()
.phone('US')
.required();
expect(phoneSchema.isValidSync('9876543210')).toBe(false);
// expect(phoneSchema.isValidSync('754-3010')).toBe(true); // Local
expect(phoneSchema.isValidSync('(541) 754-3010')).toBe(true); // Domestic
expect(phoneSchema.isValidSync('(999) 974–2042')).toBe(false);
expect(phoneSchema.isValidSync('+1-541-754-3010')).toBe(true); // International
expect(phoneSchema.isValidSync('1-541-754-3010')).toBe(true); // Dialed in the US
expect(phoneSchema.isValidSync('(212) 345-4567')).toBe(true);
// expect(phoneSchema.isValidSync('001-541-754-3010')).toBe(true); // Dialed from Germany
// expect(phoneSchema.isValidSync('191 541 754 3010')).toBe(true); // Dialed from France
});
it('validate phone number with AU (Australia) region', () => {
const phoneSchema = yup.string()
.phone('AU')
.required();
expect(phoneSchema.isValidSync('0404 999 999')).toBe(true);
expect(phoneSchema.isValidSync('(02) 9999 9999')).toBe(true);
expect(phoneSchema.isValidSync('(09) 9999 9999')).toBe(false);
});
it('validate phone number with DE (Germany) region', () => {
const phoneSchema = yup.string()
.phone('DE')
.required();
expect(phoneSchema.isValidSync('636-48018')).toBe(true); // Local
expect(phoneSchema.isValidSync('(089) / 636-48018')).toBe(true); // Domestic
expect(phoneSchema.isValidSync('+49-89-636-48018')).toBe(true); // International
// expect(phoneSchema.isValidSync('19-49-89-636-48018')).toBe(true); // Dialed from France
});
it('validate phone number with BR (Brazil) region', () => {
const phoneSchema = yup.string()
.phone('BR')
.required();
expect(phoneSchema.isValidSync('+55 11 99999-5555')).toBe(true);
});
it('validate phone number with EC (Ecuador) region', () => {
const phoneSchema = yup.string()
.phone('EC')
.required();
expect(phoneSchema.isValidSync('+593 7 282-3889')).toBe(true);
});
it('validate phone number with GB (United Kingdom) region', () => {
const phoneSchema = yup.string()
.phone('GB')
.required();
expect(phoneSchema.isValidSync('+44 871 222 1156')).toBe(true);
});
it('validate phone number with GG (Guernsey) region', () => {
const phoneSchema = yup.string()
.phone('GG')
.required();
expect(phoneSchema.isValidSync('+447911123456')).toBe(true);
});
it('validate phone number with KY (Cayman Islands) region', () => {
const phoneSchema = yup.string()
.phone('KY')
.required();
expect(phoneSchema.isValidSync('+1 345 9490088')).toBe(true);
});
it('validate phone number with VG (British Virgin Islands) region', () => {
const phoneSchema = yup.string()
.phone('VG')
.required();
expect(phoneSchema.isValidSync('+1 284 852 5500')).toBe(true);
});
it('validate phone number with BE (Belgium) region', () => {
const phoneSchema = yup.string()
.phone('BE')
.required();
expect(phoneSchema.isValidSync('+32 2 702-9200')).toBe(true);
});
it('validate phone number with CH (China) region', () => {
const phoneSchema = yup.string()
.phone('CH')
.required();
expect(phoneSchema.isValidSync('+86 21 2230 1000')).toBe(true);
});
it('validate phone number with KR (South Korea) region', () => {
const phoneSchema = yup.string()
.phone('KR')
.required();
expect(phoneSchema.isValidSync('+821012345678')).toBe(true);
});
it('validate phone number with SG (Singapore) region', () => {
const phoneSchema = yup.string()
.phone('SG')
.required();
expect(phoneSchema.isValidSync('+65 6511 9266')).toBe(true);
});
it('validate phone number strictly with IN (India) region with custom error message', () => {
const phoneSchema = yup.string().phone('IN', true, '${path} is invalid').required();
expect(() => {
phoneSchema.validateSync('+1 345 9490088');
}).toThrow('is invalid');
});
});