@hy_ong/zod-kit 0.1.8 → 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 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)) {
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)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hy_ong/zod-kit",
3
- "version": "0.1.8",
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",
@@ -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 an allowlist is provided, only allow values in the allowlist)
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 (only if no allowlist or allowlist is empty)
222
+ // Taiwan mobile phone format validation
228
223
  if (!validateTaiwanMobile(val)) {
229
224
  ctx.addIssue({ code: "custom", message: getMessage("invalid") })
230
225
  return
@@ -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 rejected
58
- expect(() => schema.parse("0901234567")).toThrow("Not in allowed mobile phone list")
59
- expect(() => schema.parse("0801111111")).toThrow("Not in allowed mobile phone list")
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 reject values not in whitelist when whitelist is provided", () => {
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
- expect(() => schema.parse("0911234567")).toThrow("Not in allowed mobile phone list")
68
- expect(() => schema.parse("invalid-value")).toThrow("Not in allowed mobile phone list")
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(() => schema.parse("0911234567")).toThrow("Not in allowed mobile phone list")
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(() => whitelistSchema.parse("091-123-4567")).toThrow("Not in allowed mobile phone list")
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 support whitelist error messages", () => {
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
- expect(() => schema.parse("0911234567")).toThrow("Not in allowed mobile phone list")
241
+ // Valid format, not in whitelist - should pass
242
+ expect(schema.parse("0911234567")).toBe("0911234567")
231
243
 
232
244
  setLocale("zh-TW")
233
- expect(() => schema.parse("0911234567")).toThrow("不在允許的手機號碼清單中")
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 whitelist messages", () => {
273
+ it("should support custom invalid messages", () => {
262
274
  const schema = twMobile(true, {
263
275
  whitelist: ["0901234567"],
264
276
  i18n: {
265
277
  en: {
266
- notInWhitelist: "This mobile phone number is not allowed",
278
+ invalid: "This mobile phone format is not valid",
267
279
  },
268
280
  "zh-TW": {
269
- notInWhitelist: "此手機號碼不被允許",
281
+ invalid: "此手機號碼格式無效",
270
282
  },
271
283
  },
272
284
  })
273
285
 
274
286
  setLocale("en")
275
- expect(() => schema.parse("0911234567")).toThrow("This mobile phone number is not allowed")
287
+ expect(() => schema.parse("0801234567")).toThrow("This mobile phone format is not valid")
276
288
 
277
289
  setLocale("zh-TW")
278
- expect(() => schema.parse("0911234567")).toThrow("此手機號碼不被允許")
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
- // Not in the allowlist
360
- expect(() => schema.parse("0911234567")).toThrow("Not in allowed mobile phone list")
361
- expect(() => schema.parse("other-value")).toThrow("Not in allowed mobile phone list")
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
  })