@hy_ong/zod-kit 0.0.2 → 0.0.5

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 (51) hide show
  1. package/.claude/settings.local.json +23 -0
  2. package/LICENSE +21 -0
  3. package/README.md +7 -7
  4. package/debug.js +21 -0
  5. package/debug.ts +16 -0
  6. package/dist/index.cjs +1663 -189
  7. package/dist/index.d.cts +324 -32
  8. package/dist/index.d.ts +324 -32
  9. package/dist/index.js +1634 -187
  10. package/eslint.config.mts +8 -0
  11. package/package.json +10 -9
  12. package/src/config.ts +1 -1
  13. package/src/i18n/locales/en.json +123 -49
  14. package/src/i18n/locales/zh-TW.json +123 -46
  15. package/src/index.ts +13 -7
  16. package/src/validators/common/boolean.ts +97 -0
  17. package/src/validators/common/date.ts +171 -0
  18. package/src/validators/common/email.ts +200 -0
  19. package/src/validators/common/id.ts +259 -0
  20. package/src/validators/common/number.ts +194 -0
  21. package/src/validators/common/password.ts +214 -0
  22. package/src/validators/common/text.ts +151 -0
  23. package/src/validators/common/url.ts +207 -0
  24. package/src/validators/taiwan/business-id.ts +140 -0
  25. package/src/validators/taiwan/fax.ts +182 -0
  26. package/src/validators/taiwan/mobile.ts +110 -0
  27. package/src/validators/taiwan/national-id.ts +208 -0
  28. package/src/validators/taiwan/tel.ts +182 -0
  29. package/tests/common/boolean.test.ts +340 -92
  30. package/tests/common/date.test.ts +458 -0
  31. package/tests/common/email.test.ts +232 -60
  32. package/tests/common/id.test.ts +535 -0
  33. package/tests/common/number.test.ts +230 -60
  34. package/tests/common/password.test.ts +281 -54
  35. package/tests/common/text.test.ts +227 -30
  36. package/tests/common/url.test.ts +492 -67
  37. package/tests/taiwan/business-id.test.ts +240 -0
  38. package/tests/taiwan/fax.test.ts +463 -0
  39. package/tests/taiwan/mobile.test.ts +373 -0
  40. package/tests/taiwan/national-id.test.ts +435 -0
  41. package/tests/taiwan/tel.test.ts +467 -0
  42. package/eslint.config.mjs +0 -10
  43. package/src/common/boolean.ts +0 -37
  44. package/src/common/date.ts +0 -44
  45. package/src/common/email.ts +0 -45
  46. package/src/common/integer.ts +0 -47
  47. package/src/common/number.ts +0 -38
  48. package/src/common/password.ts +0 -34
  49. package/src/common/text.ts +0 -35
  50. package/src/common/url.ts +0 -38
  51. package/tests/common/integer.test.ts +0 -90
package/dist/index.d.ts CHANGED
@@ -1,84 +1,376 @@
1
1
  import { ZodBoolean, ZodNullable, ZodString, ZodNumber } from 'zod';
2
2
 
3
+ type Locale = "zh-TW" | "en";
4
+ declare const setLocale: (locale: Locale) => void;
5
+ declare const getLocale: () => Locale;
6
+
7
+ type BooleanMessages = {
8
+ required?: string;
9
+ shouldBeTrue?: string;
10
+ shouldBeFalse?: string;
11
+ invalid?: string;
12
+ };
3
13
  type BooleanOptions<IsRequired extends boolean = true> = {
4
14
  required?: IsRequired;
5
- label: string;
6
15
  defaultValue?: IsRequired extends true ? boolean : boolean | null;
7
16
  shouldBe?: boolean;
17
+ truthyValues?: unknown[];
18
+ falsyValues?: unknown[];
19
+ strict?: boolean;
20
+ transform?: (value: boolean) => boolean;
21
+ i18n?: Record<Locale, BooleanMessages>;
8
22
  };
9
23
  type BooleanSchema<IsRequired extends boolean> = IsRequired extends true ? ZodBoolean : ZodNullable<ZodBoolean>;
10
24
  declare function boolean<IsRequired extends boolean = true>(options?: BooleanOptions<IsRequired>): BooleanSchema<IsRequired>;
11
25
 
12
- type EmailOptions<IsRequired extends boolean = true> = {
26
+ type DateMessages = {
27
+ required?: string;
28
+ invalid?: string;
29
+ format?: string;
30
+ min?: string;
31
+ max?: string;
32
+ includes?: string;
33
+ excludes?: string;
34
+ past?: string;
35
+ future?: string;
36
+ today?: string;
37
+ notToday?: string;
38
+ weekday?: string;
39
+ notWeekday?: string;
40
+ weekend?: string;
41
+ notWeekend?: string;
42
+ };
43
+ type DateOptions<IsRequired extends boolean = true> = {
13
44
  required?: IsRequired;
14
- label: string;
45
+ min?: string;
46
+ max?: string;
47
+ format?: string;
48
+ includes?: string;
49
+ excludes?: string | string[];
50
+ mustBePast?: boolean;
51
+ mustBeFuture?: boolean;
52
+ mustBeToday?: boolean;
53
+ mustNotBeToday?: boolean;
54
+ weekdaysOnly?: boolean;
55
+ weekendsOnly?: boolean;
56
+ transform?: (value: string) => string;
57
+ defaultValue?: IsRequired extends true ? string : string | null;
58
+ i18n?: Record<Locale, DateMessages>;
59
+ };
60
+ type DateSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
61
+ declare function date<IsRequired extends boolean = true>(options?: DateOptions<IsRequired>): DateSchema<IsRequired>;
62
+
63
+ type EmailMessages = {
64
+ required?: string;
65
+ invalid?: string;
66
+ minLength?: string;
67
+ maxLength?: string;
68
+ includes?: string;
15
69
  domain?: string;
16
- min?: number;
17
- max?: number;
70
+ domainBlacklist?: string;
71
+ businessOnly?: string;
72
+ noDisposable?: string;
73
+ };
74
+ type EmailOptions<IsRequired extends boolean = true> = {
75
+ required?: IsRequired;
76
+ domain?: string | string[];
77
+ domainBlacklist?: string[];
78
+ minLength?: number;
79
+ maxLength?: number;
18
80
  includes?: string;
81
+ excludes?: string | string[];
82
+ allowSubdomains?: boolean;
83
+ businessOnly?: boolean;
84
+ noDisposable?: boolean;
85
+ lowercase?: boolean;
86
+ transform?: (value: string) => string;
87
+ defaultValue?: IsRequired extends true ? string : string | null;
88
+ i18n?: Record<Locale, EmailMessages>;
19
89
  };
20
90
  type EmailSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
21
91
  declare function email<IsRequired extends boolean = true>(options?: EmailOptions<IsRequired>): EmailSchema<IsRequired>;
22
92
 
23
- type UrlOptions<IsRequired extends boolean = true> = {
24
- required?: IsRequired;
25
- label: string;
26
- min?: number;
27
- max?: number;
93
+ type IdMessages = {
94
+ required?: string;
95
+ invalid?: string;
96
+ minLength?: string;
97
+ maxLength?: string;
98
+ numeric?: string;
99
+ uuid?: string;
100
+ objectId?: string;
101
+ nanoid?: string;
102
+ snowflake?: string;
103
+ cuid?: string;
104
+ ulid?: string;
105
+ shortid?: string;
106
+ customFormat?: string;
28
107
  includes?: string;
108
+ excludes?: string;
109
+ startsWith?: string;
110
+ endsWith?: string;
29
111
  };
30
- type UrlSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
31
- declare function url<IsRequired extends boolean = true>(options?: UrlOptions<IsRequired>): UrlSchema<IsRequired>;
32
-
33
- type TextOptions<IsRequired extends boolean = true> = {
112
+ type IdType = "numeric" | "uuid" | "objectId" | "nanoid" | "snowflake" | "cuid" | "ulid" | "shortid" | "auto";
113
+ type IdOptions<IsRequired extends boolean = true> = {
34
114
  required?: IsRequired;
35
- label: string;
36
- min?: number;
37
- max?: number;
115
+ type?: IdType;
116
+ minLength?: number;
117
+ maxLength?: number;
118
+ allowedTypes?: IdType[];
119
+ customRegex?: RegExp;
120
+ includes?: string;
121
+ excludes?: string | string[];
38
122
  startsWith?: string;
39
123
  endsWith?: string;
40
- includes?: string;
41
- regex?: RegExp;
124
+ caseSensitive?: boolean;
125
+ transform?: (value: string) => string;
42
126
  defaultValue?: IsRequired extends true ? string : string | null;
127
+ i18n?: Record<Locale, IdMessages>;
43
128
  };
44
- type TextSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
45
- declare function text<IsRequired extends boolean = true>(options?: TextOptions<IsRequired>): TextSchema<IsRequired>;
129
+ type IdSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
130
+ declare const ID_PATTERNS: {
131
+ readonly numeric: RegExp;
132
+ readonly uuid: RegExp;
133
+ readonly objectId: RegExp;
134
+ readonly nanoid: RegExp;
135
+ readonly snowflake: RegExp;
136
+ readonly cuid: RegExp;
137
+ readonly ulid: RegExp;
138
+ readonly shortid: RegExp;
139
+ };
140
+ declare const detectIdType: (value: string) => IdType | null;
141
+ declare const validateIdType: (value: string, type: IdType) => boolean;
142
+ declare function id<IsRequired extends boolean = true>(options?: IdOptions<IsRequired>): IdSchema<IsRequired>;
46
143
 
144
+ type NumberMessages = {
145
+ required?: string;
146
+ invalid?: string;
147
+ integer?: string;
148
+ float?: string;
149
+ min?: string;
150
+ max?: string;
151
+ positive?: string;
152
+ negative?: string;
153
+ nonNegative?: string;
154
+ nonPositive?: string;
155
+ multipleOf?: string;
156
+ finite?: string;
157
+ precision?: string;
158
+ };
47
159
  type NumberOptions<IsRequired extends boolean = true> = {
48
160
  required?: IsRequired;
49
- label: string;
50
161
  min?: number;
51
162
  max?: number;
52
163
  defaultValue?: IsRequired extends true ? number : number | null;
164
+ type?: "integer" | "float" | "both";
165
+ positive?: boolean;
166
+ negative?: boolean;
167
+ nonNegative?: boolean;
168
+ nonPositive?: boolean;
169
+ multipleOf?: number;
170
+ precision?: number;
171
+ finite?: boolean;
172
+ transform?: (value: number) => number;
173
+ parseCommas?: boolean;
174
+ i18n?: Record<Locale, NumberMessages>;
53
175
  };
54
176
  type NumberSchema<IsRequired extends boolean> = IsRequired extends true ? ZodNumber : ZodNullable<ZodNumber>;
55
177
  declare function number<IsRequired extends boolean = true>(options?: NumberOptions<IsRequired>): NumberSchema<IsRequired>;
56
178
 
179
+ type PasswordMessages = {
180
+ required?: string;
181
+ min?: string;
182
+ max?: string;
183
+ uppercase?: string;
184
+ lowercase?: string;
185
+ digits?: string;
186
+ special?: string;
187
+ noRepeating?: string;
188
+ noSequential?: string;
189
+ noCommonWords?: string;
190
+ minStrength?: string;
191
+ excludes?: string;
192
+ includes?: string;
193
+ invalid?: string;
194
+ };
195
+ type PasswordStrength = "weak" | "medium" | "strong" | "very-strong";
57
196
  type PasswordOptions<IsRequired extends boolean = true> = {
58
197
  required?: IsRequired;
59
- label: string;
60
198
  min?: number;
61
199
  max?: number;
62
200
  uppercase?: boolean;
63
201
  lowercase?: boolean;
64
202
  digits?: boolean;
65
203
  special?: boolean;
204
+ noRepeating?: boolean;
205
+ noSequential?: boolean;
206
+ noCommonWords?: boolean;
207
+ minStrength?: PasswordStrength;
208
+ excludes?: string | string[];
209
+ includes?: string;
210
+ regex?: RegExp;
211
+ transform?: (value: string) => string;
212
+ defaultValue?: IsRequired extends true ? string : string | null;
213
+ i18n?: Record<Locale, PasswordMessages>;
66
214
  };
67
215
  type PasswordSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
68
216
  declare function password<IsRequired extends boolean = true>(options?: PasswordOptions<IsRequired>): PasswordSchema<IsRequired>;
69
217
 
70
- type IntegerOptions<IsRequired extends boolean = true> = {
218
+ type TextMessages = {
219
+ required?: string;
220
+ notEmpty?: string;
221
+ minLength?: string;
222
+ maxLength?: string;
223
+ startsWith?: string;
224
+ endsWith?: string;
225
+ includes?: string;
226
+ excludes?: string;
227
+ invalid?: string;
228
+ };
229
+ type TextOptions<IsRequired extends boolean = true> = {
230
+ required?: IsRequired;
231
+ minLength?: number;
232
+ maxLength?: number;
233
+ startsWith?: string;
234
+ endsWith?: string;
235
+ includes?: string;
236
+ excludes?: string | string[];
237
+ regex?: RegExp;
238
+ trimMode?: "trim" | "trimStart" | "trimEnd" | "none";
239
+ casing?: "upper" | "lower" | "title" | "none";
240
+ transform?: (value: string) => string;
241
+ notEmpty?: boolean;
242
+ defaultValue?: IsRequired extends true ? string : string | null;
243
+ i18n?: Record<Locale, TextMessages>;
244
+ };
245
+ type TextSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
246
+ declare function text<IsRequired extends boolean = true>(options?: TextOptions<IsRequired>): TextSchema<IsRequired>;
247
+
248
+ type UrlMessages = {
249
+ required?: string;
250
+ invalid?: string;
251
+ min?: string;
252
+ max?: string;
253
+ includes?: string;
254
+ excludes?: string;
255
+ protocol?: string;
256
+ domain?: string;
257
+ domainBlacklist?: string;
258
+ port?: string;
259
+ pathStartsWith?: string;
260
+ pathEndsWith?: string;
261
+ hasQuery?: string;
262
+ noQuery?: string;
263
+ hasFragment?: string;
264
+ noFragment?: string;
265
+ localhost?: string;
266
+ noLocalhost?: string;
267
+ };
268
+ type UrlOptions<IsRequired extends boolean = true> = {
71
269
  required?: IsRequired;
72
- label: string;
73
270
  min?: number;
74
271
  max?: number;
75
- defaultValue?: IsRequired extends true ? number : number | null;
272
+ includes?: string;
273
+ excludes?: string | string[];
274
+ protocols?: string[];
275
+ allowedDomains?: string[];
276
+ blockedDomains?: string[];
277
+ allowedPorts?: number[];
278
+ blockedPorts?: number[];
279
+ pathStartsWith?: string;
280
+ pathEndsWith?: string;
281
+ mustHaveQuery?: boolean;
282
+ mustNotHaveQuery?: boolean;
283
+ mustHaveFragment?: boolean;
284
+ mustNotHaveFragment?: boolean;
285
+ allowLocalhost?: boolean;
286
+ blockLocalhost?: boolean;
287
+ transform?: (value: string) => string;
288
+ defaultValue?: IsRequired extends true ? string : string | null;
289
+ i18n?: Record<Locale, UrlMessages>;
76
290
  };
77
- type IntegerSchema<IsRequired extends boolean> = IsRequired extends true ? ZodNumber : ZodNullable<ZodNumber>;
78
- declare function integer<IsRequired extends boolean = true>(options?: IntegerOptions<IsRequired>): IntegerSchema<IsRequired>;
291
+ type UrlSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
292
+ declare function url<IsRequired extends boolean = true>(options?: UrlOptions<IsRequired>): UrlSchema<IsRequired>;
79
293
 
80
- type Locale = "zh-TW" | "en";
81
- declare const setLocale: (locale: Locale) => void;
82
- declare const getLocale: () => Locale;
294
+ type BusinessIdMessages = {
295
+ required?: string;
296
+ invalid?: string;
297
+ };
298
+ type BusinessIdOptions<IsRequired extends boolean = true> = {
299
+ required?: IsRequired;
300
+ transform?: (value: string) => string;
301
+ defaultValue?: IsRequired extends true ? string : string | null;
302
+ i18n?: Record<Locale, BusinessIdMessages>;
303
+ };
304
+ type BusinessIdSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
305
+ declare const validateTaiwanBusinessId: (value: string) => boolean;
306
+ declare function businessId<IsRequired extends boolean = true>(options?: BusinessIdOptions<IsRequired>): BusinessIdSchema<IsRequired>;
307
+
308
+ type NationalIdMessages = {
309
+ required?: string;
310
+ invalid?: string;
311
+ };
312
+ type NationalIdType = "citizen" | "resident" | "both";
313
+ type NationalIdOptions<IsRequired extends boolean = true> = {
314
+ required?: IsRequired;
315
+ type?: NationalIdType;
316
+ allowOldResident?: boolean;
317
+ transform?: (value: string) => string;
318
+ defaultValue?: IsRequired extends true ? string : string | null;
319
+ i18n?: Record<Locale, NationalIdMessages>;
320
+ };
321
+ type NationalIdSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
322
+ declare const validateCitizenId: (value: string) => boolean;
323
+ declare const validateOldResidentId: (value: string) => boolean;
324
+ declare const validateNewResidentId: (value: string) => boolean;
325
+ declare const validateTaiwanNationalId: (value: string, type?: NationalIdType, allowOldResident?: boolean) => boolean;
326
+ declare function nationalId<IsRequired extends boolean = true>(options?: NationalIdOptions<IsRequired>): NationalIdSchema<IsRequired>;
327
+
328
+ type MobileMessages = {
329
+ required?: string;
330
+ invalid?: string;
331
+ notInWhitelist?: string;
332
+ };
333
+ type MobileOptions<IsRequired extends boolean = true> = {
334
+ required?: IsRequired;
335
+ whitelist?: string[];
336
+ transform?: (value: string) => string;
337
+ defaultValue?: IsRequired extends true ? string : string | null;
338
+ i18n?: Record<Locale, MobileMessages>;
339
+ };
340
+ type MobileSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
341
+ declare const validateTaiwanMobile: (value: string) => boolean;
342
+ declare function mobile<IsRequired extends boolean = true>(options?: MobileOptions<IsRequired>): MobileSchema<IsRequired>;
343
+
344
+ type TelMessages = {
345
+ required?: string;
346
+ invalid?: string;
347
+ notInWhitelist?: string;
348
+ };
349
+ type TelOptions<IsRequired extends boolean = true> = {
350
+ required?: IsRequired;
351
+ whitelist?: string[];
352
+ transform?: (value: string) => string;
353
+ defaultValue?: IsRequired extends true ? string : string | null;
354
+ i18n?: Record<Locale, TelMessages>;
355
+ };
356
+ type TelSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
357
+ declare const validateTaiwanTel: (value: string) => boolean;
358
+ declare function tel<IsRequired extends boolean = true>(options?: TelOptions<IsRequired>): TelSchema<IsRequired>;
359
+
360
+ type FaxMessages = {
361
+ required?: string;
362
+ invalid?: string;
363
+ notInWhitelist?: string;
364
+ };
365
+ type FaxOptions<IsRequired extends boolean = true> = {
366
+ required?: IsRequired;
367
+ whitelist?: string[];
368
+ transform?: (value: string) => string;
369
+ defaultValue?: IsRequired extends true ? string : string | null;
370
+ i18n?: Record<Locale, FaxMessages>;
371
+ };
372
+ type FaxSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
373
+ declare const validateTaiwanFax: (value: string) => boolean;
374
+ declare function fax<IsRequired extends boolean = true>(options?: FaxOptions<IsRequired>): FaxSchema<IsRequired>;
83
375
 
84
- export { type BooleanOptions, type BooleanSchema, type EmailOptions, type EmailSchema, type IntegerOptions, type IntegerSchema, type Locale, type NumberOptions, type NumberSchema, type PasswordOptions, type PasswordSchema, type TextOptions, type TextSchema, type UrlOptions, type UrlSchema, boolean, email, getLocale, integer, number, password, setLocale, text, url };
376
+ export { type BooleanMessages, type BooleanOptions, type BooleanSchema, type BusinessIdMessages, type BusinessIdOptions, type BusinessIdSchema, type DateMessages, type DateOptions, type DateSchema, type EmailMessages, type EmailOptions, type EmailSchema, type FaxMessages, type FaxOptions, type FaxSchema, ID_PATTERNS, type IdMessages, type IdOptions, type IdSchema, type IdType, type Locale, type MobileMessages, type MobileOptions, type MobileSchema, type NationalIdMessages, type NationalIdOptions, type NationalIdSchema, type NationalIdType, type NumberMessages, type NumberOptions, type NumberSchema, type PasswordMessages, type PasswordOptions, type PasswordSchema, type PasswordStrength, type TelMessages, type TelOptions, type TelSchema, type TextMessages, type TextOptions, type TextSchema, type UrlMessages, type UrlOptions, type UrlSchema, boolean, businessId, date, detectIdType, email, fax, getLocale, id, mobile, nationalId, number, password, setLocale, tel, text, url, validateCitizenId, validateIdType, validateNewResidentId, validateOldResidentId, validateTaiwanBusinessId, validateTaiwanFax, validateTaiwanMobile, validateTaiwanNationalId, validateTaiwanTel };