@hy_ong/zod-kit 0.1.1 → 0.1.3

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.
@@ -1,12 +1,12 @@
1
1
  import { describe, it, expect, beforeEach } from "vitest"
2
- import { tel, setLocale, validateTaiwanTel } from "../../src"
2
+ import { twTel, setLocale, validateTaiwanTel } from "../../src"
3
3
 
4
- describe("Taiwan tel(true) validator", () => {
4
+ describe("Taiwan twTel(true) validator", () => {
5
5
  beforeEach(() => setLocale("en"))
6
6
 
7
7
  describe("basic functionality", () => {
8
8
  it("should validate correct Taiwan landline telephone numbers", () => {
9
- const schema = tel(true)
9
+ const schema = twTel(true)
10
10
 
11
11
  // Valid Taiwan landline numbers (various formats)
12
12
  // Taipei (02) - requires 10 digits total, first digit after 02 must be 2,3,5-8
@@ -41,7 +41,7 @@ describe("Taiwan tel(true) validator", () => {
41
41
  })
42
42
 
43
43
  it("should validate numbers with separators", () => {
44
- const schema = tel(true)
44
+ const schema = twTel(true)
45
45
 
46
46
  // Numbers with dashes
47
47
  expect(schema.parse("02-2345-6789")).toBe("02-2345-6789")
@@ -59,7 +59,7 @@ describe("Taiwan tel(true) validator", () => {
59
59
  })
60
60
 
61
61
  it("should reject invalid Taiwan telephone numbers", () => {
62
- const schema = tel(true)
62
+ const schema = twTel(true)
63
63
 
64
64
  // Invalid formats
65
65
  expect(() => schema.parse("123456789")).toThrow("Invalid Taiwan telephone format") // Missing leading 0
@@ -84,7 +84,7 @@ describe("Taiwan tel(true) validator", () => {
84
84
  })
85
85
 
86
86
  it("should handle whitespace trimming", () => {
87
- const schema = tel(true)
87
+ const schema = twTel(true)
88
88
 
89
89
  expect(schema.parse(" 0223456789 ")).toBe("0223456789")
90
90
  expect(schema.parse("\t072345678\n")).toBe("072345678")
@@ -93,7 +93,7 @@ describe("Taiwan tel(true) validator", () => {
93
93
 
94
94
  describe("whitelist functionality", () => {
95
95
  it("should accept any string in whitelist regardless of format", () => {
96
- const schema = tel(true, {
96
+ const schema = twTel(true, {
97
97
  whitelist: ["custom-tel", "emergency-line", "0912345678"],
98
98
  })
99
99
 
@@ -108,7 +108,7 @@ describe("Taiwan tel(true) validator", () => {
108
108
  })
109
109
 
110
110
  it("should reject values not in whitelist when whitelist is provided", () => {
111
- const schema = tel(true, {
111
+ const schema = twTel(true, {
112
112
  whitelist: ["allowed-value", "0223456789"],
113
113
  })
114
114
 
@@ -117,7 +117,7 @@ describe("Taiwan tel(true) validator", () => {
117
117
  })
118
118
 
119
119
  it("should work with empty whitelist", () => {
120
- const schema = tel(true, {
120
+ const schema = twTel(true, {
121
121
  whitelist: [],
122
122
  })
123
123
 
@@ -127,7 +127,7 @@ describe("Taiwan tel(true) validator", () => {
127
127
  })
128
128
 
129
129
  it("should prioritize whitelist over format validation", () => {
130
- const schema = tel(false, { whitelist: ["not-a-phone", "123", ""] })
130
+ const schema = twTel(false, { whitelist: ["not-a-phone", "123", ""] })
131
131
 
132
132
  // These should be accepted despite being invalid telephone formats
133
133
  expect(schema.parse("not-a-phone")).toBe("not-a-phone")
@@ -138,7 +138,7 @@ describe("Taiwan tel(true) validator", () => {
138
138
 
139
139
  describe("required/optional behavior", () => {
140
140
  it("should handle required=true (default)", () => {
141
- const schema = tel(true)
141
+ const schema = twTel(true)
142
142
 
143
143
  expect(() => schema.parse("")).toThrow("Required")
144
144
  expect(() => schema.parse(null)).toThrow()
@@ -146,7 +146,7 @@ describe("Taiwan tel(true) validator", () => {
146
146
  })
147
147
 
148
148
  it("should handle required=false", () => {
149
- const schema = tel(false)
149
+ const schema = twTel(false)
150
150
 
151
151
  expect(schema.parse("")).toBe(null)
152
152
  expect(schema.parse(null)).toBe(null)
@@ -155,15 +155,15 @@ describe("Taiwan tel(true) validator", () => {
155
155
  })
156
156
 
157
157
  it("should use default values", () => {
158
- const requiredSchema = tel(true, { defaultValue: "0223456789" })
159
- const optionalSchema = tel(false, { defaultValue: "0223456789" })
158
+ const requiredSchema = twTel(true, { defaultValue: "0223456789" })
159
+ const optionalSchema = twTel(false, { defaultValue: "0223456789" })
160
160
 
161
161
  expect(requiredSchema.parse("")).toBe("0223456789")
162
162
  expect(optionalSchema.parse("")).toBe("0223456789")
163
163
  })
164
164
 
165
165
  it("should handle whitelist with optional fields", () => {
166
- const schema = tel(false, { whitelist: ["custom-value", "0223456789"] })
166
+ const schema = twTel(false, { whitelist: ["custom-value", "0223456789"] })
167
167
 
168
168
  expect(schema.parse("")).toBe(null)
169
169
  expect(schema.parse("custom-value")).toBe("custom-value")
@@ -174,7 +174,7 @@ describe("Taiwan tel(true) validator", () => {
174
174
 
175
175
  describe("transform function", () => {
176
176
  it("should apply custom transform", () => {
177
- const schema = tel(true, {
177
+ const schema = twTel(true, {
178
178
  transform: (val) => val.replace(/[-\s]/g, ""),
179
179
  })
180
180
 
@@ -183,7 +183,7 @@ describe("Taiwan tel(true) validator", () => {
183
183
  })
184
184
 
185
185
  it("should apply transform before validation", () => {
186
- const schema = tel(true, {
186
+ const schema = twTel(true, {
187
187
  transform: (val) => val.replace(/\s+/g, ""),
188
188
  })
189
189
 
@@ -192,7 +192,7 @@ describe("Taiwan tel(true) validator", () => {
192
192
  })
193
193
 
194
194
  it("should work with whitelist after transform", () => {
195
- const schema = tel(true, {
195
+ const schema = twTel(true, {
196
196
  transform: (val) => val.replace(/[-\s]/g, ""),
197
197
  whitelist: ["0223456789", "customvalue"],
198
198
  })
@@ -204,14 +204,14 @@ describe("Taiwan tel(true) validator", () => {
204
204
 
205
205
  describe("input preprocessing", () => {
206
206
  it("should handle string conversion", () => {
207
- const schema = tel(true)
207
+ const schema = twTel(true)
208
208
 
209
209
  // Test string conversion of numbers
210
210
  expect(() => schema.parse(212345678)).toThrow("Invalid Taiwan telephone format") // Invalid because missing leading 0
211
211
  })
212
212
 
213
213
  it("should trim whitespace", () => {
214
- const schema = tel(true)
214
+ const schema = twTel(true)
215
215
 
216
216
  expect(schema.parse(" 0223456789 ")).toBe("0223456789")
217
217
  expect(schema.parse("\t072345678\n")).toBe("072345678")
@@ -258,7 +258,7 @@ describe("Taiwan tel(true) validator", () => {
258
258
  describe("i18n support", () => {
259
259
  it("should use English messages by default", () => {
260
260
  setLocale("en")
261
- const schema = tel(true)
261
+ const schema = twTel(true)
262
262
 
263
263
  expect(() => schema.parse("")).toThrow("Required")
264
264
  expect(() => schema.parse("0912345678")).toThrow("Invalid Taiwan telephone format")
@@ -266,7 +266,7 @@ describe("Taiwan tel(true) validator", () => {
266
266
 
267
267
  it("should use Chinese messages when locale is zh-TW", () => {
268
268
  setLocale("zh-TW")
269
- const schema = tel(true)
269
+ const schema = twTel(true)
270
270
 
271
271
  expect(() => schema.parse("")).toThrow("必填")
272
272
  expect(() => schema.parse("0912345678")).toThrow("無效的市話號碼格式")
@@ -274,7 +274,7 @@ describe("Taiwan tel(true) validator", () => {
274
274
 
275
275
  it("should support whitelist error messages", () => {
276
276
  setLocale("en")
277
- const schema = tel(true, {
277
+ const schema = twTel(true, {
278
278
  whitelist: ["0212345678"],
279
279
  })
280
280
 
@@ -285,7 +285,7 @@ describe("Taiwan tel(true) validator", () => {
285
285
  })
286
286
 
287
287
  it("should support custom i18n messages", () => {
288
- const schema = tel(true, {
288
+ const schema = twTel(true, {
289
289
  i18n: {
290
290
  en: {
291
291
  required: "Telephone number is required",
@@ -310,7 +310,7 @@ describe("Taiwan tel(true) validator", () => {
310
310
  })
311
311
 
312
312
  it("should support custom whitelist messages", () => {
313
- const schema = tel(true, {
313
+ const schema = twTel(true, {
314
314
  whitelist: ["0212345678"],
315
315
  i18n: {
316
316
  en: {
@@ -332,7 +332,7 @@ describe("Taiwan tel(true) validator", () => {
332
332
 
333
333
  describe("real world Taiwan telephone numbers", () => {
334
334
  it("should validate all major area codes", () => {
335
- const schema = tel(true)
335
+ const schema = twTel(true)
336
336
 
337
337
  // Test Taiwan landline area codes using exact working numbers from a utility test
338
338
  const validAreaCodes = [
@@ -356,7 +356,7 @@ describe("Taiwan tel(true) validator", () => {
356
356
  })
357
357
 
358
358
  it("should reject mobile phone prefixes", () => {
359
- const schema = tel(true)
359
+ const schema = twTel(true)
360
360
 
361
361
  // Test invalid mobile prefixes (090-099)
362
362
  const mobilePrefixes = ["090", "091", "092", "093", "094", "095", "096", "097", "098", "099"]
@@ -368,7 +368,7 @@ describe("Taiwan tel(true) validator", () => {
368
368
  })
369
369
 
370
370
  it("should validate realistic landline number patterns", () => {
371
- const schema = tel(true)
371
+ const schema = twTel(true)
372
372
 
373
373
  const realLandlineNumbers = [
374
374
  "0223456789", // Taipei 10-digit (valid first digit 2) - from utility test ✓
@@ -391,15 +391,15 @@ describe("Taiwan tel(true) validator", () => {
391
391
 
392
392
  describe("edge cases", () => {
393
393
  it("should handle various input types", () => {
394
- const schema = tel(true)
394
+ const schema = twTel(true)
395
395
 
396
396
  // Test different input types that should be converted to string
397
397
  expect(schema.parse("0223456789")).toBe("0223456789")
398
398
  })
399
399
 
400
400
  it("should handle empty and whitespace inputs", () => {
401
- const schema = tel(true)
402
- const optionalSchema = tel(false)
401
+ const schema = twTel(true)
402
+ const optionalSchema = twTel(false)
403
403
 
404
404
  expect(() => schema.parse("")).toThrow("Required")
405
405
  expect(() => schema.parse(" ")).toThrow("Required")
@@ -411,7 +411,7 @@ describe("Taiwan tel(true) validator", () => {
411
411
  })
412
412
 
413
413
  it("should preserve valid format after transformation", () => {
414
- const schema = tel(true, {
414
+ const schema = twTel(true, {
415
415
  transform: (val) => val.replace(/[^0-9]/g, ""),
416
416
  })
417
417
 
@@ -422,7 +422,7 @@ describe("Taiwan tel(true) validator", () => {
422
422
  })
423
423
 
424
424
  it("should work with complex whitelist scenarios", () => {
425
- const schema = tel(false, { whitelist: ["0223456789", "emergency", "custom-contact-123", ""] })
425
+ const schema = twTel(false, { whitelist: ["0223456789", "emergency", "custom-contact-123", ""] })
426
426
 
427
427
  // Allowlist scenarios
428
428
  expect(schema.parse("0223456789")).toBe("0223456789")
@@ -436,7 +436,7 @@ describe("Taiwan tel(true) validator", () => {
436
436
  })
437
437
 
438
438
  it("should handle boundary cases for area codes", () => {
439
- const schema = tel(true)
439
+ const schema = twTel(true)
440
440
 
441
441
  // Test minimum and maximum valid lengths for different area codes
442
442
  // 2-digit area codes: 9-10 digits total