@hy_ong/zod-kit 0.0.6 → 0.1.1

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.
Files changed (43) hide show
  1. package/.claude/settings.local.json +4 -1
  2. package/README.md +134 -68
  3. package/dist/index.cjs +93 -89
  4. package/dist/index.d.cts +235 -169
  5. package/dist/index.d.ts +235 -169
  6. package/dist/index.js +93 -89
  7. package/package.json +15 -5
  8. package/src/validators/common/boolean.ts +17 -14
  9. package/src/validators/common/date.ts +21 -14
  10. package/src/validators/common/datetime.ts +21 -14
  11. package/src/validators/common/email.ts +18 -15
  12. package/src/validators/common/file.ts +20 -13
  13. package/src/validators/common/id.ts +14 -14
  14. package/src/validators/common/number.ts +18 -15
  15. package/src/validators/common/password.ts +21 -14
  16. package/src/validators/common/text.ts +21 -17
  17. package/src/validators/common/time.ts +21 -14
  18. package/src/validators/common/url.ts +22 -15
  19. package/src/validators/taiwan/business-id.ts +18 -11
  20. package/src/validators/taiwan/fax.ts +23 -14
  21. package/src/validators/taiwan/mobile.ts +23 -14
  22. package/src/validators/taiwan/national-id.ts +11 -12
  23. package/src/validators/taiwan/postal-code.ts +16 -17
  24. package/src/validators/taiwan/tel.ts +23 -14
  25. package/tests/common/boolean.test.ts +38 -38
  26. package/tests/common/date.test.ts +65 -65
  27. package/tests/common/datetime.test.ts +100 -118
  28. package/tests/common/email.test.ts +24 -28
  29. package/tests/common/file.test.ts +47 -51
  30. package/tests/common/id.test.ts +80 -113
  31. package/tests/common/number.test.ts +24 -25
  32. package/tests/common/password.test.ts +28 -35
  33. package/tests/common/text.test.ts +36 -37
  34. package/tests/common/time.test.ts +64 -82
  35. package/tests/common/url.test.ts +67 -67
  36. package/tests/taiwan/business-id.test.ts +22 -22
  37. package/tests/taiwan/fax.test.ts +33 -42
  38. package/tests/taiwan/mobile.test.ts +32 -41
  39. package/tests/taiwan/national-id.test.ts +31 -31
  40. package/tests/taiwan/postal-code.test.ts +142 -96
  41. package/tests/taiwan/tel.test.ts +33 -42
  42. package/debug.js +0 -21
  43. package/debug.ts +0 -16
@@ -1,12 +1,12 @@
1
1
  import { describe, it, expect, beforeEach } from "vitest"
2
2
  import { tel, setLocale, validateTaiwanTel } from "../../src"
3
3
 
4
- describe("Taiwan tel() validator", () => {
4
+ describe("Taiwan tel(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()
9
+ const schema = tel(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() validator", () => {
41
41
  })
42
42
 
43
43
  it("should validate numbers with separators", () => {
44
- const schema = tel()
44
+ const schema = tel(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() validator", () => {
59
59
  })
60
60
 
61
61
  it("should reject invalid Taiwan telephone numbers", () => {
62
- const schema = tel()
62
+ const schema = tel(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() validator", () => {
84
84
  })
85
85
 
86
86
  it("should handle whitespace trimming", () => {
87
- const schema = tel()
87
+ const schema = tel(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() validator", () => {
93
93
 
94
94
  describe("whitelist functionality", () => {
95
95
  it("should accept any string in whitelist regardless of format", () => {
96
- const schema = tel({
96
+ const schema = tel(true, {
97
97
  whitelist: ["custom-tel", "emergency-line", "0912345678"],
98
98
  })
99
99
 
@@ -108,7 +108,7 @@ describe("Taiwan tel() validator", () => {
108
108
  })
109
109
 
110
110
  it("should reject values not in whitelist when whitelist is provided", () => {
111
- const schema = tel({
111
+ const schema = tel(true, {
112
112
  whitelist: ["allowed-value", "0223456789"],
113
113
  })
114
114
 
@@ -117,7 +117,7 @@ describe("Taiwan tel() validator", () => {
117
117
  })
118
118
 
119
119
  it("should work with empty whitelist", () => {
120
- const schema = tel({
120
+ const schema = tel(true, {
121
121
  whitelist: [],
122
122
  })
123
123
 
@@ -127,10 +127,7 @@ describe("Taiwan tel() validator", () => {
127
127
  })
128
128
 
129
129
  it("should prioritize whitelist over format validation", () => {
130
- const schema = tel({
131
- required: false,
132
- whitelist: ["not-a-phone", "123", ""],
133
- })
130
+ const schema = tel(false, { whitelist: ["not-a-phone", "123", ""] })
134
131
 
135
132
  // These should be accepted despite being invalid telephone formats
136
133
  expect(schema.parse("not-a-phone")).toBe("not-a-phone")
@@ -141,7 +138,7 @@ describe("Taiwan tel() validator", () => {
141
138
 
142
139
  describe("required/optional behavior", () => {
143
140
  it("should handle required=true (default)", () => {
144
- const schema = tel()
141
+ const schema = tel(true)
145
142
 
146
143
  expect(() => schema.parse("")).toThrow("Required")
147
144
  expect(() => schema.parse(null)).toThrow()
@@ -149,7 +146,7 @@ describe("Taiwan tel() validator", () => {
149
146
  })
150
147
 
151
148
  it("should handle required=false", () => {
152
- const schema = tel({ required: false })
149
+ const schema = tel(false)
153
150
 
154
151
  expect(schema.parse("")).toBe(null)
155
152
  expect(schema.parse(null)).toBe(null)
@@ -158,18 +155,15 @@ describe("Taiwan tel() validator", () => {
158
155
  })
159
156
 
160
157
  it("should use default values", () => {
161
- const requiredSchema = tel({ defaultValue: "0223456789" })
162
- const optionalSchema = tel({ required: false, defaultValue: "0223456789" })
158
+ const requiredSchema = tel(true, { defaultValue: "0223456789" })
159
+ const optionalSchema = tel(false, { defaultValue: "0223456789" })
163
160
 
164
161
  expect(requiredSchema.parse("")).toBe("0223456789")
165
162
  expect(optionalSchema.parse("")).toBe("0223456789")
166
163
  })
167
164
 
168
165
  it("should handle whitelist with optional fields", () => {
169
- const schema = tel({
170
- required: false,
171
- whitelist: ["custom-value", "0223456789"],
172
- })
166
+ const schema = tel(false, { whitelist: ["custom-value", "0223456789"] })
173
167
 
174
168
  expect(schema.parse("")).toBe(null)
175
169
  expect(schema.parse("custom-value")).toBe("custom-value")
@@ -180,7 +174,7 @@ describe("Taiwan tel() validator", () => {
180
174
 
181
175
  describe("transform function", () => {
182
176
  it("should apply custom transform", () => {
183
- const schema = tel({
177
+ const schema = tel(true, {
184
178
  transform: (val) => val.replace(/[-\s]/g, ""),
185
179
  })
186
180
 
@@ -189,7 +183,7 @@ describe("Taiwan tel() validator", () => {
189
183
  })
190
184
 
191
185
  it("should apply transform before validation", () => {
192
- const schema = tel({
186
+ const schema = tel(true, {
193
187
  transform: (val) => val.replace(/\s+/g, ""),
194
188
  })
195
189
 
@@ -198,7 +192,7 @@ describe("Taiwan tel() validator", () => {
198
192
  })
199
193
 
200
194
  it("should work with whitelist after transform", () => {
201
- const schema = tel({
195
+ const schema = tel(true, {
202
196
  transform: (val) => val.replace(/[-\s]/g, ""),
203
197
  whitelist: ["0223456789", "customvalue"],
204
198
  })
@@ -210,14 +204,14 @@ describe("Taiwan tel() validator", () => {
210
204
 
211
205
  describe("input preprocessing", () => {
212
206
  it("should handle string conversion", () => {
213
- const schema = tel()
207
+ const schema = tel(true)
214
208
 
215
209
  // Test string conversion of numbers
216
210
  expect(() => schema.parse(212345678)).toThrow("Invalid Taiwan telephone format") // Invalid because missing leading 0
217
211
  })
218
212
 
219
213
  it("should trim whitespace", () => {
220
- const schema = tel()
214
+ const schema = tel(true)
221
215
 
222
216
  expect(schema.parse(" 0223456789 ")).toBe("0223456789")
223
217
  expect(schema.parse("\t072345678\n")).toBe("072345678")
@@ -264,7 +258,7 @@ describe("Taiwan tel() validator", () => {
264
258
  describe("i18n support", () => {
265
259
  it("should use English messages by default", () => {
266
260
  setLocale("en")
267
- const schema = tel()
261
+ const schema = tel(true)
268
262
 
269
263
  expect(() => schema.parse("")).toThrow("Required")
270
264
  expect(() => schema.parse("0912345678")).toThrow("Invalid Taiwan telephone format")
@@ -272,7 +266,7 @@ describe("Taiwan tel() validator", () => {
272
266
 
273
267
  it("should use Chinese messages when locale is zh-TW", () => {
274
268
  setLocale("zh-TW")
275
- const schema = tel()
269
+ const schema = tel(true)
276
270
 
277
271
  expect(() => schema.parse("")).toThrow("必填")
278
272
  expect(() => schema.parse("0912345678")).toThrow("無效的市話號碼格式")
@@ -280,7 +274,7 @@ describe("Taiwan tel() validator", () => {
280
274
 
281
275
  it("should support whitelist error messages", () => {
282
276
  setLocale("en")
283
- const schema = tel({
277
+ const schema = tel(true, {
284
278
  whitelist: ["0212345678"],
285
279
  })
286
280
 
@@ -291,7 +285,7 @@ describe("Taiwan tel() validator", () => {
291
285
  })
292
286
 
293
287
  it("should support custom i18n messages", () => {
294
- const schema = tel({
288
+ const schema = tel(true, {
295
289
  i18n: {
296
290
  en: {
297
291
  required: "Telephone number is required",
@@ -316,7 +310,7 @@ describe("Taiwan tel() validator", () => {
316
310
  })
317
311
 
318
312
  it("should support custom whitelist messages", () => {
319
- const schema = tel({
313
+ const schema = tel(true, {
320
314
  whitelist: ["0212345678"],
321
315
  i18n: {
322
316
  en: {
@@ -338,7 +332,7 @@ describe("Taiwan tel() validator", () => {
338
332
 
339
333
  describe("real world Taiwan telephone numbers", () => {
340
334
  it("should validate all major area codes", () => {
341
- const schema = tel()
335
+ const schema = tel(true)
342
336
 
343
337
  // Test Taiwan landline area codes using exact working numbers from a utility test
344
338
  const validAreaCodes = [
@@ -362,7 +356,7 @@ describe("Taiwan tel() validator", () => {
362
356
  })
363
357
 
364
358
  it("should reject mobile phone prefixes", () => {
365
- const schema = tel()
359
+ const schema = tel(true)
366
360
 
367
361
  // Test invalid mobile prefixes (090-099)
368
362
  const mobilePrefixes = ["090", "091", "092", "093", "094", "095", "096", "097", "098", "099"]
@@ -374,7 +368,7 @@ describe("Taiwan tel() validator", () => {
374
368
  })
375
369
 
376
370
  it("should validate realistic landline number patterns", () => {
377
- const schema = tel()
371
+ const schema = tel(true)
378
372
 
379
373
  const realLandlineNumbers = [
380
374
  "0223456789", // Taipei 10-digit (valid first digit 2) - from utility test ✓
@@ -397,15 +391,15 @@ describe("Taiwan tel() validator", () => {
397
391
 
398
392
  describe("edge cases", () => {
399
393
  it("should handle various input types", () => {
400
- const schema = tel()
394
+ const schema = tel(true)
401
395
 
402
396
  // Test different input types that should be converted to string
403
397
  expect(schema.parse("0223456789")).toBe("0223456789")
404
398
  })
405
399
 
406
400
  it("should handle empty and whitespace inputs", () => {
407
- const schema = tel()
408
- const optionalSchema = tel({ required: false })
401
+ const schema = tel(true)
402
+ const optionalSchema = tel(false)
409
403
 
410
404
  expect(() => schema.parse("")).toThrow("Required")
411
405
  expect(() => schema.parse(" ")).toThrow("Required")
@@ -417,7 +411,7 @@ describe("Taiwan tel() validator", () => {
417
411
  })
418
412
 
419
413
  it("should preserve valid format after transformation", () => {
420
- const schema = tel({
414
+ const schema = tel(true, {
421
415
  transform: (val) => val.replace(/[^0-9]/g, ""),
422
416
  })
423
417
 
@@ -428,10 +422,7 @@ describe("Taiwan tel() validator", () => {
428
422
  })
429
423
 
430
424
  it("should work with complex whitelist scenarios", () => {
431
- const schema = tel({
432
- whitelist: ["0223456789", "emergency", "custom-contact-123", ""],
433
- required: false,
434
- })
425
+ const schema = tel(false, { whitelist: ["0223456789", "emergency", "custom-contact-123", ""] })
435
426
 
436
427
  // Allowlist scenarios
437
428
  expect(schema.parse("0223456789")).toBe("0223456789")
@@ -445,7 +436,7 @@ describe("Taiwan tel() validator", () => {
445
436
  })
446
437
 
447
438
  it("should handle boundary cases for area codes", () => {
448
- const schema = tel()
439
+ const schema = tel(true)
449
440
 
450
441
  // Test minimum and maximum valid lengths for different area codes
451
442
  // 2-digit area codes: 9-10 digits total
package/debug.js DELETED
@@ -1,21 +0,0 @@
1
- // Debug test
2
- import { tel } from './src/index.js'
3
-
4
- const schema = tel()
5
-
6
- // Test the exact numbers that work in utility test
7
- const testNumbers = [
8
- "0223456789", // Should work ✓
9
- "072345678", // Should work ✓
10
- "041234567", // Should work ✓
11
- "031234567", // Testing this one
12
- ]
13
-
14
- for (const num of testNumbers) {
15
- try {
16
- const result = schema.parse(num)
17
- console.log(`✓ ${num} -> ${result}`)
18
- } catch (error) {
19
- console.log(`✗ ${num} -> ${error.message}`)
20
- }
21
- }
package/debug.ts DELETED
@@ -1,16 +0,0 @@
1
- import { validateTaiwanTel } from './src/validators/taiwan/tel.js'
2
-
3
- // Test the exact numbers from my area codes test
4
- const testNumbers = [
5
- "0223456789", // From my test - should work ✓
6
- "0256789012", // From my test - testing this
7
- "031234567", // From my test - testing this
8
- "039876543", // From my test - testing this
9
- "037234567", // From my test - testing this
10
- "037987654", // From my test - testing this
11
- ]
12
-
13
- for (const num of testNumbers) {
14
- const result = validateTaiwanTel(num)
15
- console.log(`${result ? '✓' : '✗'} ${num}`)
16
- }