@hy_ong/zod-kit 0.0.6 → 0.1.0

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 (42) hide show
  1. package/.claude/settings.local.json +4 -1
  2. package/dist/index.cjs +93 -89
  3. package/dist/index.d.cts +235 -169
  4. package/dist/index.d.ts +235 -169
  5. package/dist/index.js +93 -89
  6. package/package.json +2 -1
  7. package/src/validators/common/boolean.ts +17 -14
  8. package/src/validators/common/date.ts +21 -14
  9. package/src/validators/common/datetime.ts +21 -14
  10. package/src/validators/common/email.ts +18 -15
  11. package/src/validators/common/file.ts +20 -13
  12. package/src/validators/common/id.ts +14 -14
  13. package/src/validators/common/number.ts +18 -15
  14. package/src/validators/common/password.ts +21 -14
  15. package/src/validators/common/text.ts +21 -17
  16. package/src/validators/common/time.ts +21 -14
  17. package/src/validators/common/url.ts +22 -15
  18. package/src/validators/taiwan/business-id.ts +18 -11
  19. package/src/validators/taiwan/fax.ts +23 -14
  20. package/src/validators/taiwan/mobile.ts +23 -14
  21. package/src/validators/taiwan/national-id.ts +11 -12
  22. package/src/validators/taiwan/postal-code.ts +16 -17
  23. package/src/validators/taiwan/tel.ts +23 -14
  24. package/tests/common/boolean.test.ts +38 -38
  25. package/tests/common/date.test.ts +65 -65
  26. package/tests/common/datetime.test.ts +100 -118
  27. package/tests/common/email.test.ts +24 -28
  28. package/tests/common/file.test.ts +47 -51
  29. package/tests/common/id.test.ts +80 -113
  30. package/tests/common/number.test.ts +24 -25
  31. package/tests/common/password.test.ts +28 -35
  32. package/tests/common/text.test.ts +36 -37
  33. package/tests/common/time.test.ts +64 -82
  34. package/tests/common/url.test.ts +67 -67
  35. package/tests/taiwan/business-id.test.ts +22 -22
  36. package/tests/taiwan/fax.test.ts +33 -42
  37. package/tests/taiwan/mobile.test.ts +32 -41
  38. package/tests/taiwan/national-id.test.ts +31 -31
  39. package/tests/taiwan/postal-code.test.ts +142 -96
  40. package/tests/taiwan/tel.test.ts +33 -42
  41. package/debug.js +0 -21
  42. package/debug.ts +0 -16
@@ -4,153 +4,153 @@ import { setLocale, date } from "../../src"
4
4
  describe("date", () => {
5
5
  describe("required (default)", () => {
6
6
  it("should validate valid date string", () => {
7
- const schema = date()
7
+ const schema = date(true)
8
8
  expect(schema.parse("2023-12-25")).toBe("2023-12-25")
9
9
  })
10
10
 
11
11
  it("should reject empty string", () => {
12
- const schema = date()
12
+ const schema = date(true)
13
13
  expect(() => schema.parse("")).toThrow()
14
14
  })
15
15
 
16
16
  it("should reject null", () => {
17
- const schema = date()
17
+ const schema = date(true)
18
18
  expect(() => schema.parse(null)).toThrow()
19
19
  })
20
20
 
21
21
  it("should reject undefined", () => {
22
- const schema = date()
22
+ const schema = date(true)
23
23
  expect(() => schema.parse(undefined)).toThrow()
24
24
  })
25
25
 
26
26
  it("should reject invalid date format", () => {
27
- const schema = date()
27
+ const schema = date(true)
28
28
  expect(() => schema.parse("invalid-date")).toThrow()
29
29
  })
30
30
 
31
31
  it("should reject date with wrong format", () => {
32
- const schema = date()
32
+ const schema = date(true)
33
33
  expect(() => schema.parse("25/12/2023")).toThrow()
34
34
  })
35
35
  })
36
36
 
37
37
  describe("optional", () => {
38
38
  it("should allow null when not required", () => {
39
- const schema = date({ required: false })
39
+ const schema = date(false)
40
40
  expect(schema.parse(null)).toBe(null)
41
41
  })
42
42
 
43
43
  it("should allow empty string when not required", () => {
44
- const schema = date({ required: false })
44
+ const schema = date(false)
45
45
  expect(schema.parse("")).toBe(null)
46
46
  })
47
47
 
48
48
  it("should allow undefined when not required", () => {
49
- const schema = date({ required: false })
49
+ const schema = date(false)
50
50
  expect(schema.parse(undefined)).toBe(null)
51
51
  })
52
52
 
53
53
  it("should validate valid date when not required", () => {
54
- const schema = date({ required: false })
54
+ const schema = date(false)
55
55
  expect(schema.parse("2023-12-25")).toBe("2023-12-25")
56
56
  })
57
57
  })
58
58
 
59
59
  describe("custom format", () => {
60
60
  it("should validate date with DD/MM/YYYY format", () => {
61
- const schema = date({ format: "DD/MM/YYYY" })
61
+ const schema = date(true, { format: "DD/MM/YYYY" })
62
62
  expect(schema.parse("25/12/2023")).toBe("25/12/2023")
63
63
  })
64
64
 
65
65
  it("should reject wrong format for custom format", () => {
66
- const schema = date({ format: "DD/MM/YYYY" })
66
+ const schema = date(true, { format: "DD/MM/YYYY" })
67
67
  expect(() => schema.parse("2023-12-25")).toThrow()
68
68
  })
69
69
 
70
70
  it("should validate date with MM-DD-YYYY format", () => {
71
- const schema = date({ format: "MM-DD-YYYY" })
71
+ const schema = date(true, { format: "MM-DD-YYYY" })
72
72
  expect(schema.parse("12-25-2023")).toBe("12-25-2023")
73
73
  })
74
74
 
75
75
  it("should validate date with YYYY/MM/DD format", () => {
76
- const schema = date({ format: "YYYY/MM/DD" })
76
+ const schema = date(true, { format: "YYYY/MM/DD" })
77
77
  expect(schema.parse("2023/12/25")).toBe("2023/12/25")
78
78
  })
79
79
  })
80
80
 
81
81
  describe("min validation", () => {
82
82
  it("should accept date equal to min", () => {
83
- const schema = date({ min: "2023-01-01" })
83
+ const schema = date(true, { min: "2023-01-01" })
84
84
  expect(schema.parse("2023-01-01")).toBe("2023-01-01")
85
85
  })
86
86
 
87
87
  it("should accept date after min", () => {
88
- const schema = date({ min: "2023-01-01" })
88
+ const schema = date(true, { min: "2023-01-01" })
89
89
  expect(schema.parse("2023-12-31")).toBe("2023-12-31")
90
90
  })
91
91
 
92
92
  it("should reject date before min", () => {
93
- const schema = date({ min: "2023-01-01" })
93
+ const schema = date(true, { min: "2023-01-01" })
94
94
  expect(() => schema.parse("2022-12-31")).toThrow()
95
95
  })
96
96
  })
97
97
 
98
98
  describe("max validation", () => {
99
99
  it("should accept date equal to max", () => {
100
- const schema = date({ max: "2023-12-31" })
100
+ const schema = date(true, { max: "2023-12-31" })
101
101
  expect(schema.parse("2023-12-31")).toBe("2023-12-31")
102
102
  })
103
103
 
104
104
  it("should accept date before max", () => {
105
- const schema = date({ max: "2023-12-31" })
105
+ const schema = date(true, { max: "2023-12-31" })
106
106
  expect(schema.parse("2023-01-01")).toBe("2023-01-01")
107
107
  })
108
108
 
109
109
  it("should reject date after max", () => {
110
- const schema = date({ max: "2023-12-31" })
110
+ const schema = date(true, { max: "2023-12-31" })
111
111
  expect(() => schema.parse("2024-01-01")).toThrow()
112
112
  })
113
113
  })
114
114
 
115
115
  describe("min and max validation", () => {
116
116
  it("should accept date within range", () => {
117
- const schema = date({ min: "2023-01-01", max: "2023-12-31" })
117
+ const schema = date(true, { min: "2023-01-01", max: "2023-12-31" })
118
118
  expect(schema.parse("2023-06-15")).toBe("2023-06-15")
119
119
  })
120
120
 
121
121
  it("should reject date before min in range", () => {
122
- const schema = date({ min: "2023-01-01", max: "2023-12-31" })
122
+ const schema = date(true, { min: "2023-01-01", max: "2023-12-31" })
123
123
  expect(() => schema.parse("2022-12-31")).toThrow()
124
124
  })
125
125
 
126
126
  it("should reject date after max in range", () => {
127
- const schema = date({ min: "2023-01-01", max: "2023-12-31" })
127
+ const schema = date(true, { min: "2023-01-01", max: "2023-12-31" })
128
128
  expect(() => schema.parse("2024-01-01")).toThrow()
129
129
  })
130
130
  })
131
131
 
132
132
  describe("includes validation", () => {
133
133
  it("should accept date containing required substring", () => {
134
- const schema = date({ includes: "2023" })
134
+ const schema = date(true, { includes: "2023" })
135
135
  expect(schema.parse("2023-12-25")).toBe("2023-12-25")
136
136
  })
137
137
 
138
138
  it("should reject date not containing required substring", () => {
139
- const schema = date({ includes: "2023" })
139
+ const schema = date(true, { includes: "2023" })
140
140
  expect(() => schema.parse("2022-12-25")).toThrow()
141
141
  })
142
142
  })
143
143
 
144
144
  describe("default value", () => {
145
145
  it("should use default value when input is empty", () => {
146
- const schema = date({ defaultValue: "2023-01-01" })
146
+ const schema = date(true, { defaultValue: "2023-01-01" })
147
147
  expect(schema.parse("")).toBe("2023-01-01")
148
148
  expect(schema.parse(null)).toBe("2023-01-01")
149
149
  expect(schema.parse(undefined)).toBe("2023-01-01")
150
150
  })
151
151
 
152
152
  it("should use default value when optional and input is empty", () => {
153
- const schema = date({ required: false, defaultValue: "2023-01-01" })
153
+ const schema = date(false, { defaultValue: "2023-01-01" })
154
154
  expect(schema.parse("")).toBe("2023-01-01")
155
155
  expect(schema.parse(null)).toBe("2023-01-01")
156
156
  expect(schema.parse(undefined)).toBe("2023-01-01")
@@ -160,7 +160,7 @@ describe("date", () => {
160
160
  describe("localization", () => {
161
161
  it("should use English error messages", () => {
162
162
  setLocale("en")
163
- const schema = date()
163
+ const schema = date(true)
164
164
 
165
165
  try {
166
166
  schema.parse("invalid")
@@ -171,7 +171,7 @@ describe("date", () => {
171
171
 
172
172
  it("should use Chinese error messages", () => {
173
173
  setLocale("zh-TW")
174
- const schema = date()
174
+ const schema = date(true)
175
175
 
176
176
  try {
177
177
  schema.parse("invalid")
@@ -183,17 +183,17 @@ describe("date", () => {
183
183
 
184
184
  describe("excludes validation", () => {
185
185
  it("should accept date not containing excluded substring", () => {
186
- const schema = date({ excludes: "2022" })
186
+ const schema = date(true, { excludes: "2022" })
187
187
  expect(schema.parse("2023-12-25")).toBe("2023-12-25")
188
188
  })
189
189
 
190
190
  it("should reject date containing excluded substring", () => {
191
- const schema = date({ excludes: "2022" })
191
+ const schema = date(true, { excludes: "2022" })
192
192
  expect(() => schema.parse("2022-12-25")).toThrow()
193
193
  })
194
194
 
195
195
  it("should handle multiple excluded substrings as array", () => {
196
- const schema = date({ excludes: ["2022", "01"] })
196
+ const schema = date(true, { excludes: ["2022", "01"] })
197
197
  expect(schema.parse("2023-12-25")).toBe("2023-12-25")
198
198
  expect(() => schema.parse("2022-12-25")).toThrow()
199
199
  expect(() => schema.parse("2023-01-25")).toThrow()
@@ -211,8 +211,8 @@ describe("date", () => {
211
211
  // Format dates in local timezone
212
212
  const formatLocalDate = (date: Date) => {
213
213
  const year = date.getFullYear()
214
- const month = String(date.getMonth() + 1).padStart(2, '0')
215
- const day = String(date.getDate()).padStart(2, '0')
214
+ const month = String(date.getMonth() + 1).padStart(2, "0")
215
+ const day = String(date.getDate()).padStart(2, "0")
216
216
  return `${year}-${month}-${day}`
217
217
  }
218
218
 
@@ -222,68 +222,68 @@ describe("date", () => {
222
222
 
223
223
  describe("mustBePast", () => {
224
224
  it("should accept past date", () => {
225
- const schema = date({ mustBePast: true })
225
+ const schema = date(true, { mustBePast: true })
226
226
  expect(schema.parse(yesterdayStr)).toBe(yesterdayStr)
227
227
  })
228
228
 
229
229
  it("should reject today when mustBePast", () => {
230
- const schema = date({ mustBePast: true })
230
+ const schema = date(true, { mustBePast: true })
231
231
  expect(() => schema.parse(todayStr)).toThrow()
232
232
  })
233
233
 
234
234
  it("should reject future date", () => {
235
- const schema = date({ mustBePast: true })
235
+ const schema = date(true, { mustBePast: true })
236
236
  expect(() => schema.parse(tomorrowStr)).toThrow()
237
237
  })
238
238
  })
239
239
 
240
240
  describe("mustBeFuture", () => {
241
241
  it("should accept future date", () => {
242
- const schema = date({ mustBeFuture: true })
242
+ const schema = date(true, { mustBeFuture: true })
243
243
  expect(schema.parse(tomorrowStr)).toBe(tomorrowStr)
244
244
  })
245
245
 
246
246
  it("should reject today when mustBeFuture", () => {
247
- const schema = date({ mustBeFuture: true })
247
+ const schema = date(true, { mustBeFuture: true })
248
248
  expect(() => schema.parse(todayStr)).toThrow()
249
249
  })
250
250
 
251
251
  it("should reject past date", () => {
252
- const schema = date({ mustBeFuture: true })
252
+ const schema = date(true, { mustBeFuture: true })
253
253
  expect(() => schema.parse(yesterdayStr)).toThrow()
254
254
  })
255
255
  })
256
256
 
257
257
  describe("mustBeToday", () => {
258
258
  it("should accept today", () => {
259
- const schema = date({ mustBeToday: true })
259
+ const schema = date(true, { mustBeToday: true })
260
260
  expect(schema.parse(todayStr)).toBe(todayStr)
261
261
  })
262
262
 
263
263
  it("should reject past date", () => {
264
- const schema = date({ mustBeToday: true })
264
+ const schema = date(true, { mustBeToday: true })
265
265
  expect(() => schema.parse(yesterdayStr)).toThrow()
266
266
  })
267
267
 
268
268
  it("should reject future date", () => {
269
- const schema = date({ mustBeToday: true })
269
+ const schema = date(true, { mustBeToday: true })
270
270
  expect(() => schema.parse(tomorrowStr)).toThrow()
271
271
  })
272
272
  })
273
273
 
274
274
  describe("mustNotBeToday", () => {
275
275
  it("should accept past date", () => {
276
- const schema = date({ mustNotBeToday: true })
276
+ const schema = date(true, { mustNotBeToday: true })
277
277
  expect(schema.parse(yesterdayStr)).toBe(yesterdayStr)
278
278
  })
279
279
 
280
280
  it("should accept future date", () => {
281
- const schema = date({ mustNotBeToday: true })
281
+ const schema = date(true, { mustNotBeToday: true })
282
282
  expect(schema.parse(tomorrowStr)).toBe(tomorrowStr)
283
283
  })
284
284
 
285
285
  it("should reject today", () => {
286
- const schema = date({ mustNotBeToday: true })
286
+ const schema = date(true, { mustNotBeToday: true })
287
287
  expect(() => schema.parse(todayStr)).toThrow()
288
288
  })
289
289
  })
@@ -292,44 +292,44 @@ describe("date", () => {
292
292
  describe("weekday/weekend validations", () => {
293
293
  describe("weekdaysOnly", () => {
294
294
  it("should accept Monday (weekday)", () => {
295
- const schema = date({ weekdaysOnly: true })
295
+ const schema = date(true, { weekdaysOnly: true })
296
296
  expect(schema.parse("2023-12-25")).toBe("2023-12-25") // Monday
297
297
  })
298
298
 
299
299
  it("should accept Friday (weekday)", () => {
300
- const schema = date({ weekdaysOnly: true })
300
+ const schema = date(true, { weekdaysOnly: true })
301
301
  expect(schema.parse("2023-12-29")).toBe("2023-12-29") // Friday
302
302
  })
303
303
 
304
304
  it("should reject Saturday (weekend)", () => {
305
- const schema = date({ weekdaysOnly: true })
305
+ const schema = date(true, { weekdaysOnly: true })
306
306
  expect(() => schema.parse("2023-12-30")).toThrow() // Saturday
307
307
  })
308
308
 
309
309
  it("should reject Sunday (weekend)", () => {
310
- const schema = date({ weekdaysOnly: true })
310
+ const schema = date(true, { weekdaysOnly: true })
311
311
  expect(() => schema.parse("2023-12-31")).toThrow() // Sunday
312
312
  })
313
313
  })
314
314
 
315
315
  describe("weekendsOnly", () => {
316
316
  it("should accept Saturday (weekend)", () => {
317
- const schema = date({ weekendsOnly: true })
317
+ const schema = date(true, { weekendsOnly: true })
318
318
  expect(schema.parse("2023-12-30")).toBe("2023-12-30") // Saturday
319
319
  })
320
320
 
321
321
  it("should accept Sunday (weekend)", () => {
322
- const schema = date({ weekendsOnly: true })
322
+ const schema = date(true, { weekendsOnly: true })
323
323
  expect(schema.parse("2023-12-31")).toBe("2023-12-31") // Sunday
324
324
  })
325
325
 
326
326
  it("should reject Monday (weekday)", () => {
327
- const schema = date({ weekendsOnly: true })
327
+ const schema = date(true, { weekendsOnly: true })
328
328
  expect(() => schema.parse("2023-12-25")).toThrow() // Monday
329
329
  })
330
330
 
331
331
  it("should reject Friday (weekday)", () => {
332
- const schema = date({ weekendsOnly: true })
332
+ const schema = date(true, { weekendsOnly: true })
333
333
  expect(() => schema.parse("2023-12-29")).toThrow() // Friday
334
334
  })
335
335
  })
@@ -337,7 +337,7 @@ describe("date", () => {
337
337
 
338
338
  describe("transform function", () => {
339
339
  it("should apply custom transform function", () => {
340
- const schema = date({
340
+ const schema = date(true, {
341
341
  format: "YYYY/MM/DD",
342
342
  transform: (val) => val.replace(/-/g, "/"),
343
343
  })
@@ -345,7 +345,7 @@ describe("date", () => {
345
345
  })
346
346
 
347
347
  it("should apply transform before validation", () => {
348
- const schema = date({
348
+ const schema = date(true, {
349
349
  format: "YYYY/MM/DD",
350
350
  transform: (val) => val.replace(/-/g, "/"),
351
351
  })
@@ -353,7 +353,7 @@ describe("date", () => {
353
353
  })
354
354
 
355
355
  it("should work with other validations after transform", () => {
356
- const schema = date({
356
+ const schema = date(true, {
357
357
  format: "YYYY/MM/DD",
358
358
  includes: "/",
359
359
  transform: (val) => val.replace(/-/g, "/"),
@@ -364,7 +364,7 @@ describe("date", () => {
364
364
 
365
365
  describe("i18n custom messages", () => {
366
366
  it("should use custom English messages", () => {
367
- const schema = date({
367
+ const schema = date(true, {
368
368
  i18n: {
369
369
  en: { format: "Custom date format error: ${format}" },
370
370
  "zh-TW": { format: "自定義日期格式錯誤: ${format}" },
@@ -380,7 +380,7 @@ describe("date", () => {
380
380
  })
381
381
 
382
382
  it("should use custom Chinese messages", () => {
383
- const schema = date({
383
+ const schema = date(true, {
384
384
  i18n: {
385
385
  en: { format: "Custom date format error: ${format}" },
386
386
  "zh-TW": { format: "自定義日期格式錯誤: ${format}" },
@@ -396,7 +396,7 @@ describe("date", () => {
396
396
  })
397
397
 
398
398
  it("should fallback to default messages when custom not provided", () => {
399
- const schema = date({
399
+ const schema = date(true, {
400
400
  i18n: {
401
401
  en: { format: "Custom format error" },
402
402
  "zh-TW": { format: "自定義格式錯誤" },
@@ -412,7 +412,7 @@ describe("date", () => {
412
412
  })
413
413
 
414
414
  it("should handle past validation with custom message", () => {
415
- const schema = date({
415
+ const schema = date(true, {
416
416
  mustBePast: true,
417
417
  i18n: {
418
418
  en: { past: "Date must be in the past!" },
@@ -435,23 +435,23 @@ describe("date", () => {
435
435
 
436
436
  describe("edge cases", () => {
437
437
  it("should handle leap year dates", () => {
438
- const schema = date()
438
+ const schema = date(true)
439
439
  expect(schema.parse("2024-02-29")).toBe("2024-02-29")
440
440
  })
441
441
 
442
442
  it("should reject invalid leap year date", () => {
443
- const schema = date()
443
+ const schema = date(true)
444
444
  expect(() => schema.parse("2023-02-29")).toThrow()
445
445
  })
446
446
 
447
447
  it("should handle end of month dates", () => {
448
- const schema = date()
448
+ const schema = date(true)
449
449
  expect(schema.parse("2023-01-31")).toBe("2023-01-31")
450
450
  expect(schema.parse("2023-04-30")).toBe("2023-04-30")
451
451
  })
452
452
 
453
453
  it("should reject invalid end of month dates", () => {
454
- const schema = date()
454
+ const schema = date(true)
455
455
  expect(() => schema.parse("2023-04-31")).toThrow()
456
456
  })
457
457
  })