@hy_ong/zod-kit 0.1.7 → 0.1.8

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 CHANGED
@@ -3425,11 +3425,7 @@ function twTel(required, options) {
3425
3425
  }
3426
3426
  if (val === null) return;
3427
3427
  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") });
3428
+ if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
3433
3429
  return;
3434
3430
  }
3435
3431
  if (!validateTaiwanTel(val)) {
@@ -3547,11 +3543,7 @@ function twFax(required, options) {
3547
3543
  }
3548
3544
  if (val === null) return;
3549
3545
  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") });
3546
+ if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
3555
3547
  return;
3556
3548
  }
3557
3549
  if (!validateTaiwanFax(val)) {
package/dist/index.js CHANGED
@@ -3347,11 +3347,7 @@ function twTel(required, options) {
3347
3347
  }
3348
3348
  if (val === null) return;
3349
3349
  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") });
3350
+ if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
3355
3351
  return;
3356
3352
  }
3357
3353
  if (!validateTaiwanTel(val)) {
@@ -3469,11 +3465,7 @@ function twFax(required, options) {
3469
3465
  }
3470
3466
  if (val === null) return;
3471
3467
  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") });
3468
+ if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
3477
3469
  return;
3478
3470
  }
3479
3471
  if (!validateTaiwanFax(val)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hy_ong/zod-kit",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
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 an allowlist is provided, only allow values in the allowlist)
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 (only if no allowlist or allowlist is empty)
331
+ // Taiwan fax format validation
337
332
  if (!validateTaiwanFax(val)) {
338
333
  ctx.addIssue({ code: "custom", message: getMessage("invalid") })
339
334
  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 an allowlist is provided, only allow values in the allowlist)
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 (only if no allowlist or allowlist is empty)
334
+ // Taiwan telephone format validation
340
335
  if (!validateTaiwanTel(val)) {
341
336
  ctx.addIssue({ code: "custom", message: getMessage("invalid") })
342
337
  return
@@ -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 rejected
106
- expect(() => schema.parse("0223456789")).toThrow("Not in allowed fax list")
107
- expect(() => schema.parse("0711111111")).toThrow("Not in allowed fax list")
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 reject values not in whitelist when whitelist is provided", () => {
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
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed fax list")
116
- expect(() => schema.parse("invalid-value")).toThrow("Not in allowed fax list")
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(() => schema.parse("0312345678")).toThrow("Not in allowed fax list")
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(() => schema.parse("03-123-4567")).toThrow("Not in allowed fax list")
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 error messages", () => {
283
+ it("should support whitelist with valid format fallback", () => {
272
284
  setLocale("en")
273
285
  const schema = twFax(true, {
274
- whitelist: ["0223456789"],
286
+ whitelist: ["special-fax"],
275
287
  })
276
288
 
277
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed fax list")
289
+ // In whitelist
290
+ expect(schema.parse("special-fax")).toBe("special-fax")
278
291
 
279
- setLocale("zh-TW")
280
- expect(() => schema.parse("0312345678")).toThrow("不在允許的傳真號碼清單中")
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 whitelist messages", () => {
324
+ it("should support custom i18n messages for invalid format", () => {
309
325
  const schema = twFax(true, {
310
- whitelist: ["0223456789"],
326
+ whitelist: ["special-value"],
311
327
  i18n: {
312
328
  en: {
313
- notInWhitelist: "This fax number is not allowed",
329
+ invalid: "This fax format is not valid",
314
330
  },
315
331
  "zh-TW": {
316
- notInWhitelist: "此傳真號碼不被允許",
332
+ invalid: "此傳真格式無效",
317
333
  },
318
334
  },
319
335
  })
320
336
 
321
337
  setLocale("en")
322
- expect(() => schema.parse("0312345678")).toThrow("This fax number is not allowed")
338
+ expect(() => schema.parse("invalid-format")).toThrow("This fax format is not valid")
323
339
 
324
340
  setLocale("zh-TW")
325
- expect(() => schema.parse("0312345678")).toThrow("此傳真號碼不被允許")
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
- // Allowlist scenarios
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 the allowlist
430
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed fax list")
431
- expect(() => schema.parse("other-value")).toThrow("Not in allowed fax list")
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", () => {
@@ -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 rejected
107
- expect(() => schema.parse("0212345678")).toThrow("Not in allowed telephone list")
108
- expect(() => schema.parse("0711111111")).toThrow("Not in allowed telephone list")
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 reject values not in whitelist when whitelist is provided", () => {
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
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed telephone list")
117
- expect(() => schema.parse("invalid-value")).toThrow("Not in allowed telephone list")
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(() => schema.parse("0312345678")).toThrow("Not in allowed telephone list")
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(() => schema.parse("03-123-4567")).toThrow("Not in allowed telephone list")
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 error messages", () => {
288
+ it("should support whitelist with valid format fallback", () => {
277
289
  setLocale("en")
278
290
  const schema = twTel(true, {
279
- whitelist: ["0212345678"],
291
+ whitelist: ["special-number"],
280
292
  })
281
293
 
282
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed telephone list")
294
+ // In whitelist
295
+ expect(schema.parse("special-number")).toBe("special-number")
283
296
 
284
- setLocale("zh-TW")
285
- expect(() => schema.parse("0312345678")).toThrow("不在允許的市話號碼清單中")
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 whitelist messages", () => {
329
+ it("should support custom i18n messages for invalid format", () => {
314
330
  const schema = twTel(true, {
315
- whitelist: ["0212345678"],
331
+ whitelist: ["special-value"],
316
332
  i18n: {
317
333
  en: {
318
- notInWhitelist: "This telephone number is not allowed",
334
+ invalid: "This telephone format is not valid",
319
335
  },
320
336
  "zh-TW": {
321
- notInWhitelist: "此電話號碼不被允許",
337
+ invalid: "此電話格式無效",
322
338
  },
323
339
  },
324
340
  })
325
341
 
326
342
  setLocale("en")
327
- expect(() => schema.parse("0312345678")).toThrow("This telephone number is not allowed")
343
+ expect(() => schema.parse("invalid-format")).toThrow("This telephone format is not valid")
328
344
 
329
345
  setLocale("zh-TW")
330
- expect(() => schema.parse("0312345678")).toThrow("此電話號碼不被允許")
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
- // Allowlist scenarios
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 the allowlist
435
- expect(() => schema.parse("0312345678")).toThrow("Not in allowed telephone list")
436
- expect(() => schema.parse("other-value")).toThrow("Not in allowed telephone list")
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", () => {