@hy_ong/zod-kit 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3 -15
- package/dist/index.js +3 -15
- package/package.json +1 -1
- package/src/validators/taiwan/fax.ts +3 -8
- package/src/validators/taiwan/mobile.ts +3 -8
- package/src/validators/taiwan/tel.ts +3 -8
- package/tests/taiwan/fax.test.ts +42 -23
- package/tests/taiwan/mobile.test.ts +33 -19
- package/tests/taiwan/tel.test.ts +42 -23
package/dist/index.cjs
CHANGED
|
@@ -2620,11 +2620,7 @@ function twMobile(required, options) {
|
|
|
2620
2620
|
}
|
|
2621
2621
|
if (val === null) return;
|
|
2622
2622
|
if (!isRequired && val === "") return;
|
|
2623
|
-
if (whitelist && whitelist.length > 0) {
|
|
2624
|
-
if (whitelist.includes(val)) {
|
|
2625
|
-
return;
|
|
2626
|
-
}
|
|
2627
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
2623
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
2628
2624
|
return;
|
|
2629
2625
|
}
|
|
2630
2626
|
if (!validateTaiwanMobile(val)) {
|
|
@@ -3425,11 +3421,7 @@ function twTel(required, options) {
|
|
|
3425
3421
|
}
|
|
3426
3422
|
if (val === null) return;
|
|
3427
3423
|
if (!isRequired && val === "") return;
|
|
3428
|
-
if (whitelist && whitelist.length > 0) {
|
|
3429
|
-
if (whitelist.includes(val)) {
|
|
3430
|
-
return;
|
|
3431
|
-
}
|
|
3432
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
3424
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
3433
3425
|
return;
|
|
3434
3426
|
}
|
|
3435
3427
|
if (!validateTaiwanTel(val)) {
|
|
@@ -3547,11 +3539,7 @@ function twFax(required, options) {
|
|
|
3547
3539
|
}
|
|
3548
3540
|
if (val === null) return;
|
|
3549
3541
|
if (!isRequired && val === "") return;
|
|
3550
|
-
if (whitelist && whitelist.length > 0) {
|
|
3551
|
-
if (whitelist.includes(val)) {
|
|
3552
|
-
return;
|
|
3553
|
-
}
|
|
3554
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
3542
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
3555
3543
|
return;
|
|
3556
3544
|
}
|
|
3557
3545
|
if (!validateTaiwanFax(val)) {
|
package/dist/index.js
CHANGED
|
@@ -2542,11 +2542,7 @@ function twMobile(required, options) {
|
|
|
2542
2542
|
}
|
|
2543
2543
|
if (val === null) return;
|
|
2544
2544
|
if (!isRequired && val === "") return;
|
|
2545
|
-
if (whitelist && whitelist.length > 0) {
|
|
2546
|
-
if (whitelist.includes(val)) {
|
|
2547
|
-
return;
|
|
2548
|
-
}
|
|
2549
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
2545
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
2550
2546
|
return;
|
|
2551
2547
|
}
|
|
2552
2548
|
if (!validateTaiwanMobile(val)) {
|
|
@@ -3347,11 +3343,7 @@ function twTel(required, options) {
|
|
|
3347
3343
|
}
|
|
3348
3344
|
if (val === null) return;
|
|
3349
3345
|
if (!isRequired && val === "") return;
|
|
3350
|
-
if (whitelist && whitelist.length > 0) {
|
|
3351
|
-
if (whitelist.includes(val)) {
|
|
3352
|
-
return;
|
|
3353
|
-
}
|
|
3354
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
3346
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
3355
3347
|
return;
|
|
3356
3348
|
}
|
|
3357
3349
|
if (!validateTaiwanTel(val)) {
|
|
@@ -3469,11 +3461,7 @@ function twFax(required, options) {
|
|
|
3469
3461
|
}
|
|
3470
3462
|
if (val === null) return;
|
|
3471
3463
|
if (!isRequired && val === "") return;
|
|
3472
|
-
if (whitelist && whitelist.length > 0) {
|
|
3473
|
-
if (whitelist.includes(val)) {
|
|
3474
|
-
return;
|
|
3475
|
-
}
|
|
3476
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
3464
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
3477
3465
|
return;
|
|
3478
3466
|
}
|
|
3479
3467
|
if (!validateTaiwanFax(val)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hy_ong/zod-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "A comprehensive TypeScript library providing pre-built Zod validation schemas with full internationalization support for common data types and Taiwan-specific formats",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zod",
|
|
@@ -323,17 +323,12 @@ export function twFax<IsRequired extends boolean = false>(required?: IsRequired,
|
|
|
323
323
|
if (val === null) return
|
|
324
324
|
if (!isRequired && val === "") return
|
|
325
325
|
|
|
326
|
-
// Allowlist check (if
|
|
327
|
-
if (whitelist && whitelist.length > 0) {
|
|
328
|
-
if (whitelist.includes(val)) {
|
|
329
|
-
return
|
|
330
|
-
}
|
|
331
|
-
// If not in the allowlist, reject regardless of format
|
|
332
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") })
|
|
326
|
+
// Allowlist check (if in allowlist, accept regardless of format)
|
|
327
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
333
328
|
return
|
|
334
329
|
}
|
|
335
330
|
|
|
336
|
-
// Taiwan fax format validation
|
|
331
|
+
// Taiwan fax format validation
|
|
337
332
|
if (!validateTaiwanFax(val)) {
|
|
338
333
|
ctx.addIssue({ code: "custom", message: getMessage("invalid") })
|
|
339
334
|
return
|
|
@@ -214,17 +214,12 @@ export function twMobile<IsRequired extends boolean = false>(required?: IsRequir
|
|
|
214
214
|
if (val === null) return
|
|
215
215
|
if (!isRequired && val === "") return
|
|
216
216
|
|
|
217
|
-
// Allowlist check (if
|
|
218
|
-
if (whitelist && whitelist.length > 0) {
|
|
219
|
-
if (whitelist.includes(val)) {
|
|
220
|
-
return
|
|
221
|
-
}
|
|
222
|
-
// If not in the allowlist, reject regardless of format
|
|
223
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") })
|
|
217
|
+
// Allowlist check (if in allowlist, accept regardless of format)
|
|
218
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
224
219
|
return
|
|
225
220
|
}
|
|
226
221
|
|
|
227
|
-
// Taiwan mobile phone format validation
|
|
222
|
+
// Taiwan mobile phone format validation
|
|
228
223
|
if (!validateTaiwanMobile(val)) {
|
|
229
224
|
ctx.addIssue({ code: "custom", message: getMessage("invalid") })
|
|
230
225
|
return
|
|
@@ -326,17 +326,12 @@ export function twTel<IsRequired extends boolean = false>(required?: IsRequired,
|
|
|
326
326
|
if (val === null) return
|
|
327
327
|
if (!isRequired && val === "") return
|
|
328
328
|
|
|
329
|
-
// Allowlist check (if
|
|
330
|
-
if (whitelist && whitelist.length > 0) {
|
|
331
|
-
if (whitelist.includes(val)) {
|
|
332
|
-
return
|
|
333
|
-
}
|
|
334
|
-
// If not in the allowlist, reject regardless of format
|
|
335
|
-
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") })
|
|
329
|
+
// Allowlist check (if in allowlist, accept regardless of format)
|
|
330
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
336
331
|
return
|
|
337
332
|
}
|
|
338
333
|
|
|
339
|
-
// Taiwan telephone format validation
|
|
334
|
+
// Taiwan telephone format validation
|
|
340
335
|
if (!validateTaiwanTel(val)) {
|
|
341
336
|
ctx.addIssue({ code: "custom", message: getMessage("invalid") })
|
|
342
337
|
return
|
package/tests/taiwan/fax.test.ts
CHANGED
|
@@ -102,18 +102,29 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
102
102
|
expect(schema.parse("emergency-fax")).toBe("emergency-fax")
|
|
103
103
|
expect(schema.parse("0912345678")).toBe("0912345678") // Mobile number but in allowlist
|
|
104
104
|
|
|
105
|
-
// Valid fax numbers not in the allowlist should be
|
|
106
|
-
expect(
|
|
107
|
-
expect(
|
|
105
|
+
// Valid fax numbers not in the allowlist should still be accepted
|
|
106
|
+
expect(schema.parse("0223456789")).toBe("0223456789") // Valid format, not in whitelist
|
|
107
|
+
expect(schema.parse("072345678")).toBe("072345678") // Valid format, not in whitelist
|
|
108
|
+
|
|
109
|
+
// Invalid fax numbers not in the allowlist should be rejected
|
|
110
|
+
expect(() => schema.parse("invalid-fax")).toThrow("Invalid Taiwan fax format")
|
|
108
111
|
})
|
|
109
112
|
|
|
110
|
-
it("should
|
|
113
|
+
it("should accept both whitelist values and valid fax numbers", () => {
|
|
111
114
|
const schema = twFax(true, {
|
|
112
115
|
whitelist: ["allowed-value", "0223456789"],
|
|
113
116
|
})
|
|
114
117
|
|
|
115
|
-
|
|
116
|
-
expect(
|
|
118
|
+
// In whitelist
|
|
119
|
+
expect(schema.parse("allowed-value")).toBe("allowed-value")
|
|
120
|
+
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
121
|
+
|
|
122
|
+
// Not in whitelist but valid format
|
|
123
|
+
expect(schema.parse("0312345678")).toBe("0312345678")
|
|
124
|
+
expect(schema.parse("072345678")).toBe("072345678")
|
|
125
|
+
|
|
126
|
+
// Not in whitelist and invalid format
|
|
127
|
+
expect(() => schema.parse("invalid-value")).toThrow("Invalid Taiwan fax format")
|
|
117
128
|
})
|
|
118
129
|
|
|
119
130
|
it("should work with empty whitelist", () => {
|
|
@@ -168,7 +179,7 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
168
179
|
expect(schema.parse("")).toBe(null)
|
|
169
180
|
expect(schema.parse("custom-value")).toBe("custom-value")
|
|
170
181
|
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
171
|
-
expect(
|
|
182
|
+
expect(schema.parse("0312345678")).toBe("0312345678") // Valid format, not in whitelist
|
|
172
183
|
})
|
|
173
184
|
})
|
|
174
185
|
|
|
@@ -198,7 +209,8 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
198
209
|
})
|
|
199
210
|
|
|
200
211
|
expect(schema.parse("customvalue")).toBe("customvalue")
|
|
201
|
-
expect(
|
|
212
|
+
expect(schema.parse("03-123-4567")).toBe("031234567") // Valid format after transform
|
|
213
|
+
expect(schema.parse("02-2345-6789")).toBe("0223456789") // In whitelist after transform
|
|
202
214
|
})
|
|
203
215
|
})
|
|
204
216
|
|
|
@@ -268,16 +280,20 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
268
280
|
expect(() => schema.parse("0912345678")).toThrow("無效的傳真號碼格式")
|
|
269
281
|
})
|
|
270
282
|
|
|
271
|
-
it("should support whitelist
|
|
283
|
+
it("should support whitelist with valid format fallback", () => {
|
|
272
284
|
setLocale("en")
|
|
273
285
|
const schema = twFax(true, {
|
|
274
|
-
whitelist: ["
|
|
286
|
+
whitelist: ["special-fax"],
|
|
275
287
|
})
|
|
276
288
|
|
|
277
|
-
|
|
289
|
+
// In whitelist
|
|
290
|
+
expect(schema.parse("special-fax")).toBe("special-fax")
|
|
278
291
|
|
|
279
|
-
|
|
280
|
-
expect(
|
|
292
|
+
// Not in whitelist but valid format - should pass
|
|
293
|
+
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
294
|
+
|
|
295
|
+
// Not in whitelist and invalid format - should fail
|
|
296
|
+
expect(() => schema.parse("invalid")).toThrow("Invalid Taiwan fax format")
|
|
281
297
|
})
|
|
282
298
|
|
|
283
299
|
it("should support custom i18n messages", () => {
|
|
@@ -305,24 +321,24 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
305
321
|
expect(() => schema.parse("0912345678")).toThrow("傳真號碼格式錯誤")
|
|
306
322
|
})
|
|
307
323
|
|
|
308
|
-
it("should support custom
|
|
324
|
+
it("should support custom i18n messages for invalid format", () => {
|
|
309
325
|
const schema = twFax(true, {
|
|
310
|
-
whitelist: ["
|
|
326
|
+
whitelist: ["special-value"],
|
|
311
327
|
i18n: {
|
|
312
328
|
en: {
|
|
313
|
-
|
|
329
|
+
invalid: "This fax format is not valid",
|
|
314
330
|
},
|
|
315
331
|
"zh-TW": {
|
|
316
|
-
|
|
332
|
+
invalid: "此傳真格式無效",
|
|
317
333
|
},
|
|
318
334
|
},
|
|
319
335
|
})
|
|
320
336
|
|
|
321
337
|
setLocale("en")
|
|
322
|
-
expect(() => schema.parse("
|
|
338
|
+
expect(() => schema.parse("invalid-format")).toThrow("This fax format is not valid")
|
|
323
339
|
|
|
324
340
|
setLocale("zh-TW")
|
|
325
|
-
expect(() => schema.parse("
|
|
341
|
+
expect(() => schema.parse("invalid-format")).toThrow("此傳真格式無效")
|
|
326
342
|
})
|
|
327
343
|
})
|
|
328
344
|
|
|
@@ -420,15 +436,18 @@ describe("Taiwan twFax(true) validator", () => {
|
|
|
420
436
|
it("should work with complex whitelist scenarios", () => {
|
|
421
437
|
const schema = twFax(false, { whitelist: ["0223456789", "emergency", "custom-fax-123", ""] })
|
|
422
438
|
|
|
423
|
-
//
|
|
439
|
+
// In whitelist
|
|
424
440
|
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
425
441
|
expect(schema.parse("emergency")).toBe("emergency")
|
|
426
442
|
expect(schema.parse("custom-fax-123")).toBe("custom-fax-123")
|
|
427
443
|
expect(schema.parse("")).toBe("")
|
|
428
444
|
|
|
429
|
-
// Not in
|
|
430
|
-
expect(
|
|
431
|
-
expect(
|
|
445
|
+
// Not in whitelist but valid format
|
|
446
|
+
expect(schema.parse("0312345678")).toBe("0312345678")
|
|
447
|
+
expect(schema.parse("072345678")).toBe("072345678")
|
|
448
|
+
|
|
449
|
+
// Not in whitelist and invalid format
|
|
450
|
+
expect(() => schema.parse("other-value")).toThrow("Invalid Taiwan fax format")
|
|
432
451
|
})
|
|
433
452
|
|
|
434
453
|
it("should handle boundary cases for area codes", () => {
|
|
@@ -54,18 +54,29 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
54
54
|
expect(schema.parse("emergency-contact")).toBe("emergency-contact")
|
|
55
55
|
expect(schema.parse("0801234567")).toBe("0801234567") // Invalid mobile but in allowlist
|
|
56
56
|
|
|
57
|
-
// Valid mobile phones not in the allowlist should be
|
|
58
|
-
expect(
|
|
59
|
-
expect(
|
|
57
|
+
// Valid mobile phones not in the allowlist should still be accepted
|
|
58
|
+
expect(schema.parse("0901234567")).toBe("0901234567") // Valid format, not in whitelist
|
|
59
|
+
expect(schema.parse("0911234567")).toBe("0911234567") // Valid format, not in whitelist
|
|
60
|
+
|
|
61
|
+
// Invalid mobile numbers not in the allowlist should be rejected
|
|
62
|
+
expect(() => schema.parse("invalid-phone")).toThrow("Invalid Taiwan mobile phone format")
|
|
60
63
|
})
|
|
61
64
|
|
|
62
|
-
it("should
|
|
65
|
+
it("should accept both whitelist values and valid mobile numbers", () => {
|
|
63
66
|
const schema = twMobile(true, {
|
|
64
67
|
whitelist: ["allowed-value", "0901234567"],
|
|
65
68
|
})
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
expect(
|
|
70
|
+
// In whitelist
|
|
71
|
+
expect(schema.parse("allowed-value")).toBe("allowed-value")
|
|
72
|
+
expect(schema.parse("0901234567")).toBe("0901234567")
|
|
73
|
+
|
|
74
|
+
// Not in whitelist but valid format
|
|
75
|
+
expect(schema.parse("0911234567")).toBe("0911234567")
|
|
76
|
+
expect(schema.parse("0921234567")).toBe("0921234567")
|
|
77
|
+
|
|
78
|
+
// Not in whitelist and invalid format
|
|
79
|
+
expect(() => schema.parse("invalid-value")).toThrow("Invalid Taiwan mobile phone format")
|
|
69
80
|
})
|
|
70
81
|
|
|
71
82
|
it("should work with empty whitelist", () => {
|
|
@@ -120,7 +131,7 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
120
131
|
expect(schema.parse("")).toBe(null)
|
|
121
132
|
expect(schema.parse("custom-value")).toBe("custom-value")
|
|
122
133
|
expect(schema.parse("0901234567")).toBe("0901234567")
|
|
123
|
-
expect(
|
|
134
|
+
expect(schema.parse("0911234567")).toBe("0911234567") // Valid format, not in whitelist
|
|
124
135
|
})
|
|
125
136
|
})
|
|
126
137
|
|
|
@@ -157,7 +168,7 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
157
168
|
})
|
|
158
169
|
|
|
159
170
|
expect(whitelistSchema.parse("customvalue")).toBe("customvalue")
|
|
160
|
-
expect(
|
|
171
|
+
expect(whitelistSchema.parse("091-123-4567")).toBe("0911234567") // Valid format after transform
|
|
161
172
|
})
|
|
162
173
|
})
|
|
163
174
|
|
|
@@ -221,16 +232,17 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
221
232
|
expect(() => schema.parse("0801234567")).toThrow("無效的手機號碼格式")
|
|
222
233
|
})
|
|
223
234
|
|
|
224
|
-
it("should
|
|
235
|
+
it("should accept valid format even when not in whitelist", () => {
|
|
225
236
|
setLocale("en")
|
|
226
237
|
const schema = twMobile(true, {
|
|
227
238
|
whitelist: ["0901234567"],
|
|
228
239
|
})
|
|
229
240
|
|
|
230
|
-
|
|
241
|
+
// Valid format, not in whitelist - should pass
|
|
242
|
+
expect(schema.parse("0911234567")).toBe("0911234567")
|
|
231
243
|
|
|
232
244
|
setLocale("zh-TW")
|
|
233
|
-
expect(
|
|
245
|
+
expect(schema.parse("0921234567")).toBe("0921234567")
|
|
234
246
|
})
|
|
235
247
|
|
|
236
248
|
it("should support custom i18n messages", () => {
|
|
@@ -258,24 +270,24 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
258
270
|
expect(() => schema.parse("0801234567")).toThrow("手機號碼格式錯誤")
|
|
259
271
|
})
|
|
260
272
|
|
|
261
|
-
it("should support custom
|
|
273
|
+
it("should support custom invalid messages", () => {
|
|
262
274
|
const schema = twMobile(true, {
|
|
263
275
|
whitelist: ["0901234567"],
|
|
264
276
|
i18n: {
|
|
265
277
|
en: {
|
|
266
|
-
|
|
278
|
+
invalid: "This mobile phone format is not valid",
|
|
267
279
|
},
|
|
268
280
|
"zh-TW": {
|
|
269
|
-
|
|
281
|
+
invalid: "此手機號碼格式無效",
|
|
270
282
|
},
|
|
271
283
|
},
|
|
272
284
|
})
|
|
273
285
|
|
|
274
286
|
setLocale("en")
|
|
275
|
-
expect(() => schema.parse("
|
|
287
|
+
expect(() => schema.parse("0801234567")).toThrow("This mobile phone format is not valid")
|
|
276
288
|
|
|
277
289
|
setLocale("zh-TW")
|
|
278
|
-
expect(() => schema.parse("
|
|
290
|
+
expect(() => schema.parse("0801234567")).toThrow("此手機號碼格式無效")
|
|
279
291
|
})
|
|
280
292
|
})
|
|
281
293
|
|
|
@@ -356,9 +368,11 @@ describe("Taiwan twMobile(true) validator", () => {
|
|
|
356
368
|
expect(schema.parse("custom-contact-123")).toBe("custom-contact-123")
|
|
357
369
|
expect(schema.parse("")).toBe("")
|
|
358
370
|
|
|
359
|
-
//
|
|
360
|
-
expect(
|
|
361
|
-
|
|
371
|
+
// Valid format but not in the allowlist - should pass
|
|
372
|
+
expect(schema.parse("0911234567")).toBe("0911234567")
|
|
373
|
+
|
|
374
|
+
// Invalid format and not in allowlist - should fail
|
|
375
|
+
expect(() => schema.parse("other-value")).toThrow("Invalid Taiwan mobile phone format")
|
|
362
376
|
})
|
|
363
377
|
})
|
|
364
378
|
})
|
package/tests/taiwan/tel.test.ts
CHANGED
|
@@ -103,18 +103,29 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
103
103
|
expect(schema.parse("emergency-line")).toBe("emergency-line")
|
|
104
104
|
expect(schema.parse("0912345678")).toBe("0912345678") // Mobile number but in allowlist
|
|
105
105
|
|
|
106
|
-
// Valid telephone numbers not in the allowlist should be
|
|
107
|
-
expect(
|
|
108
|
-
expect(
|
|
106
|
+
// Valid telephone numbers not in the allowlist should still be accepted
|
|
107
|
+
expect(schema.parse("0223456789")).toBe("0223456789") // Valid format, not in whitelist
|
|
108
|
+
expect(schema.parse("072345678")).toBe("072345678") // Valid format, not in whitelist
|
|
109
|
+
|
|
110
|
+
// Invalid telephone numbers not in the allowlist should be rejected
|
|
111
|
+
expect(() => schema.parse("invalid-phone")).toThrow("Invalid Taiwan telephone format")
|
|
109
112
|
})
|
|
110
113
|
|
|
111
|
-
it("should
|
|
114
|
+
it("should accept both whitelist values and valid telephone numbers", () => {
|
|
112
115
|
const schema = twTel(true, {
|
|
113
116
|
whitelist: ["allowed-value", "0223456789"],
|
|
114
117
|
})
|
|
115
118
|
|
|
116
|
-
|
|
117
|
-
expect(
|
|
119
|
+
// In whitelist
|
|
120
|
+
expect(schema.parse("allowed-value")).toBe("allowed-value")
|
|
121
|
+
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
122
|
+
|
|
123
|
+
// Not in whitelist but valid format
|
|
124
|
+
expect(schema.parse("0312345678")).toBe("0312345678")
|
|
125
|
+
expect(schema.parse("072345678")).toBe("072345678")
|
|
126
|
+
|
|
127
|
+
// Not in whitelist and invalid format
|
|
128
|
+
expect(() => schema.parse("invalid-value")).toThrow("Invalid Taiwan telephone format")
|
|
118
129
|
})
|
|
119
130
|
|
|
120
131
|
it("should work with empty whitelist", () => {
|
|
@@ -169,7 +180,7 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
169
180
|
expect(schema.parse("")).toBe(null)
|
|
170
181
|
expect(schema.parse("custom-value")).toBe("custom-value")
|
|
171
182
|
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
172
|
-
expect(
|
|
183
|
+
expect(schema.parse("0312345678")).toBe("0312345678") // Valid format, not in whitelist
|
|
173
184
|
})
|
|
174
185
|
})
|
|
175
186
|
|
|
@@ -199,7 +210,8 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
199
210
|
})
|
|
200
211
|
|
|
201
212
|
expect(schema.parse("customvalue")).toBe("customvalue")
|
|
202
|
-
expect(
|
|
213
|
+
expect(schema.parse("03-123-4567")).toBe("031234567") // Valid format after transform
|
|
214
|
+
expect(schema.parse("02-2345-6789")).toBe("0223456789") // In whitelist after transform
|
|
203
215
|
})
|
|
204
216
|
})
|
|
205
217
|
|
|
@@ -273,16 +285,20 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
273
285
|
expect(() => schema.parse("0912345678")).toThrow("無效的市話號碼格式")
|
|
274
286
|
})
|
|
275
287
|
|
|
276
|
-
it("should support whitelist
|
|
288
|
+
it("should support whitelist with valid format fallback", () => {
|
|
277
289
|
setLocale("en")
|
|
278
290
|
const schema = twTel(true, {
|
|
279
|
-
whitelist: ["
|
|
291
|
+
whitelist: ["special-number"],
|
|
280
292
|
})
|
|
281
293
|
|
|
282
|
-
|
|
294
|
+
// In whitelist
|
|
295
|
+
expect(schema.parse("special-number")).toBe("special-number")
|
|
283
296
|
|
|
284
|
-
|
|
285
|
-
expect(
|
|
297
|
+
// Not in whitelist but valid format - should pass
|
|
298
|
+
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
299
|
+
|
|
300
|
+
// Not in whitelist and invalid format - should fail
|
|
301
|
+
expect(() => schema.parse("invalid")).toThrow("Invalid Taiwan telephone format")
|
|
286
302
|
})
|
|
287
303
|
|
|
288
304
|
it("should support custom i18n messages", () => {
|
|
@@ -310,24 +326,24 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
310
326
|
expect(() => schema.parse("0912345678")).toThrow("電話號碼格式錯誤")
|
|
311
327
|
})
|
|
312
328
|
|
|
313
|
-
it("should support custom
|
|
329
|
+
it("should support custom i18n messages for invalid format", () => {
|
|
314
330
|
const schema = twTel(true, {
|
|
315
|
-
whitelist: ["
|
|
331
|
+
whitelist: ["special-value"],
|
|
316
332
|
i18n: {
|
|
317
333
|
en: {
|
|
318
|
-
|
|
334
|
+
invalid: "This telephone format is not valid",
|
|
319
335
|
},
|
|
320
336
|
"zh-TW": {
|
|
321
|
-
|
|
337
|
+
invalid: "此電話格式無效",
|
|
322
338
|
},
|
|
323
339
|
},
|
|
324
340
|
})
|
|
325
341
|
|
|
326
342
|
setLocale("en")
|
|
327
|
-
expect(() => schema.parse("
|
|
343
|
+
expect(() => schema.parse("invalid-format")).toThrow("This telephone format is not valid")
|
|
328
344
|
|
|
329
345
|
setLocale("zh-TW")
|
|
330
|
-
expect(() => schema.parse("
|
|
346
|
+
expect(() => schema.parse("invalid-format")).toThrow("此電話格式無效")
|
|
331
347
|
})
|
|
332
348
|
})
|
|
333
349
|
|
|
@@ -425,15 +441,18 @@ describe("Taiwan twTel(true) validator", () => {
|
|
|
425
441
|
it("should work with complex whitelist scenarios", () => {
|
|
426
442
|
const schema = twTel(false, { whitelist: ["0223456789", "emergency", "custom-contact-123", ""] })
|
|
427
443
|
|
|
428
|
-
//
|
|
444
|
+
// In whitelist
|
|
429
445
|
expect(schema.parse("0223456789")).toBe("0223456789")
|
|
430
446
|
expect(schema.parse("emergency")).toBe("emergency")
|
|
431
447
|
expect(schema.parse("custom-contact-123")).toBe("custom-contact-123")
|
|
432
448
|
expect(schema.parse("")).toBe("")
|
|
433
449
|
|
|
434
|
-
// Not in
|
|
435
|
-
expect(
|
|
436
|
-
expect(
|
|
450
|
+
// Not in whitelist but valid format
|
|
451
|
+
expect(schema.parse("0312345678")).toBe("0312345678")
|
|
452
|
+
expect(schema.parse("072345678")).toBe("072345678")
|
|
453
|
+
|
|
454
|
+
// Not in whitelist and invalid format
|
|
455
|
+
expect(() => schema.parse("other-value")).toThrow("Invalid Taiwan telephone format")
|
|
437
456
|
})
|
|
438
457
|
|
|
439
458
|
it("should handle boundary cases for area codes", () => {
|