@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.
- package/.claude/settings.local.json +4 -1
- package/README.md +134 -68
- package/dist/index.cjs +93 -89
- package/dist/index.d.cts +235 -169
- package/dist/index.d.ts +235 -169
- package/dist/index.js +93 -89
- package/package.json +15 -5
- package/src/validators/common/boolean.ts +17 -14
- package/src/validators/common/date.ts +21 -14
- package/src/validators/common/datetime.ts +21 -14
- package/src/validators/common/email.ts +18 -15
- package/src/validators/common/file.ts +20 -13
- package/src/validators/common/id.ts +14 -14
- package/src/validators/common/number.ts +18 -15
- package/src/validators/common/password.ts +21 -14
- package/src/validators/common/text.ts +21 -17
- package/src/validators/common/time.ts +21 -14
- package/src/validators/common/url.ts +22 -15
- package/src/validators/taiwan/business-id.ts +18 -11
- package/src/validators/taiwan/fax.ts +23 -14
- package/src/validators/taiwan/mobile.ts +23 -14
- package/src/validators/taiwan/national-id.ts +11 -12
- package/src/validators/taiwan/postal-code.ts +16 -17
- package/src/validators/taiwan/tel.ts +23 -14
- package/tests/common/boolean.test.ts +38 -38
- package/tests/common/date.test.ts +65 -65
- package/tests/common/datetime.test.ts +100 -118
- package/tests/common/email.test.ts +24 -28
- package/tests/common/file.test.ts +47 -51
- package/tests/common/id.test.ts +80 -113
- package/tests/common/number.test.ts +24 -25
- package/tests/common/password.test.ts +28 -35
- package/tests/common/text.test.ts +36 -37
- package/tests/common/time.test.ts +64 -82
- package/tests/common/url.test.ts +67 -67
- package/tests/taiwan/business-id.test.ts +22 -22
- package/tests/taiwan/fax.test.ts +33 -42
- package/tests/taiwan/mobile.test.ts +32 -41
- package/tests/taiwan/national-id.test.ts +31 -31
- package/tests/taiwan/postal-code.test.ts +142 -96
- package/tests/taiwan/tel.test.ts +33 -42
- package/debug.js +0 -21
- package/debug.ts +0 -16
|
@@ -20,7 +20,10 @@
|
|
|
20
20
|
"WebFetch(domain:foundi.tw)",
|
|
21
21
|
"WebFetch(domain:data.gov.tw)",
|
|
22
22
|
"WebFetch(domain:gist.github.com)",
|
|
23
|
-
"WebFetch(domain:zip5.5432.tw)"
|
|
23
|
+
"WebFetch(domain:zip5.5432.tw)",
|
|
24
|
+
"Bash(npm install:*)",
|
|
25
|
+
"Bash(find:*)",
|
|
26
|
+
"Bash(python3:*)"
|
|
24
27
|
],
|
|
25
28
|
"deny": [],
|
|
26
29
|
"ask": []
|
package/README.md
CHANGED
|
@@ -39,12 +39,16 @@ pnpm add @hy_ong/zod-kit zod
|
|
|
39
39
|
```typescript
|
|
40
40
|
import { email, password, text, mobile, datetime, time, postalCode } from '@hy_ong/zod-kit'
|
|
41
41
|
|
|
42
|
-
// Simple email validation
|
|
43
|
-
const emailSchema = email()
|
|
42
|
+
// Simple email validation (required by default)
|
|
43
|
+
const emailSchema = email(true)
|
|
44
44
|
emailSchema.parse('user@example.com') // ✅ "user@example.com"
|
|
45
45
|
|
|
46
|
+
// Optional email
|
|
47
|
+
const optionalEmail = email(false)
|
|
48
|
+
optionalEmail.parse(null) // ✅ null
|
|
49
|
+
|
|
46
50
|
// Password with complexity requirements
|
|
47
|
-
const passwordSchema = password({
|
|
51
|
+
const passwordSchema = password(true, {
|
|
48
52
|
minLength: 8,
|
|
49
53
|
requireUppercase: true,
|
|
50
54
|
requireDigits: true,
|
|
@@ -52,19 +56,19 @@ const passwordSchema = password({
|
|
|
52
56
|
})
|
|
53
57
|
|
|
54
58
|
// Taiwan mobile phone validation
|
|
55
|
-
const phoneSchema = mobile()
|
|
59
|
+
const phoneSchema = mobile(true)
|
|
56
60
|
phoneSchema.parse('0912345678') // ✅ "0912345678"
|
|
57
61
|
|
|
58
62
|
// DateTime validation
|
|
59
|
-
const datetimeSchema = datetime()
|
|
63
|
+
const datetimeSchema = datetime(true)
|
|
60
64
|
datetimeSchema.parse('2024-03-15 14:30') // ✅ "2024-03-15 14:30"
|
|
61
65
|
|
|
62
66
|
// Time validation
|
|
63
|
-
const timeSchema = time()
|
|
67
|
+
const timeSchema = time(true)
|
|
64
68
|
timeSchema.parse('14:30') // ✅ "14:30"
|
|
65
69
|
|
|
66
70
|
// Taiwan postal code validation
|
|
67
|
-
const postalSchema = postalCode()
|
|
71
|
+
const postalSchema = postalCode(true)
|
|
68
72
|
postalSchema.parse('100001') // ✅ "100001"
|
|
69
73
|
```
|
|
70
74
|
|
|
@@ -72,37 +76,51 @@ postalSchema.parse('100001') // ✅ "100001"
|
|
|
72
76
|
|
|
73
77
|
### Common Validators
|
|
74
78
|
|
|
75
|
-
#### `email(options?)`
|
|
79
|
+
#### `email(required?, options?)`
|
|
76
80
|
|
|
77
81
|
Validates email addresses with comprehensive format checking.
|
|
78
82
|
|
|
83
|
+
**Parameters:**
|
|
84
|
+
- `required` (boolean, optional): Whether the field is required. Default: `false`
|
|
85
|
+
- `options` (object, optional): Configuration options
|
|
86
|
+
|
|
79
87
|
```typescript
|
|
80
88
|
import { email } from '@hy_ong/zod-kit'
|
|
81
89
|
|
|
82
|
-
//
|
|
83
|
-
const
|
|
90
|
+
// Required email (recommended)
|
|
91
|
+
const requiredEmail = email(true)
|
|
92
|
+
|
|
93
|
+
// Optional email
|
|
94
|
+
const optionalEmail = email(false)
|
|
95
|
+
optionalEmail.parse(null) // ✅ null
|
|
84
96
|
|
|
85
97
|
// With options
|
|
86
|
-
const advancedEmail = email({
|
|
87
|
-
required: true, // Default: true
|
|
98
|
+
const advancedEmail = email(true, {
|
|
88
99
|
allowedDomains: ['gmail.com', 'company.com'],
|
|
89
100
|
minLength: 5,
|
|
90
101
|
maxLength: 100,
|
|
91
102
|
transform: (val) => val.toLowerCase(),
|
|
103
|
+
defaultValue: 'default@example.com',
|
|
92
104
|
i18n: {
|
|
93
|
-
en: { invalid: 'Please enter a valid email' }
|
|
105
|
+
en: { invalid: 'Please enter a valid email' },
|
|
106
|
+
'zh-TW': { invalid: '請輸入有效的電子郵件' }
|
|
94
107
|
}
|
|
95
108
|
})
|
|
96
109
|
```
|
|
97
110
|
|
|
98
|
-
#### `password(options?)`
|
|
111
|
+
#### `password(required?, options?)`
|
|
99
112
|
|
|
100
113
|
Validates passwords with customizable complexity requirements.
|
|
101
114
|
|
|
115
|
+
**Parameters:**
|
|
116
|
+
- `required` (boolean, optional): Whether the field is required. Default: `false`
|
|
117
|
+
- `options` (object, optional): Configuration options
|
|
118
|
+
|
|
102
119
|
```typescript
|
|
103
120
|
import { password } from '@hy_ong/zod-kit'
|
|
104
121
|
|
|
105
|
-
|
|
122
|
+
// Required password with complexity rules
|
|
123
|
+
const passwordSchema = password(true, {
|
|
106
124
|
minLength: 8, // Minimum length
|
|
107
125
|
maxLength: 128, // Maximum length
|
|
108
126
|
requireUppercase: true, // Require A-Z
|
|
@@ -113,67 +131,77 @@ const passwordSchema = password({
|
|
|
113
131
|
{ pattern: /[A-Z]/, message: 'Need uppercase' }
|
|
114
132
|
]
|
|
115
133
|
})
|
|
134
|
+
|
|
135
|
+
// Optional password
|
|
136
|
+
const optionalPassword = password(false)
|
|
116
137
|
```
|
|
117
138
|
|
|
118
|
-
#### `text(options?)`
|
|
139
|
+
#### `text(required?, options?)`
|
|
119
140
|
|
|
120
141
|
General text validation with length and pattern constraints.
|
|
121
142
|
|
|
122
143
|
```typescript
|
|
123
144
|
import { text } from '@hy_ong/zod-kit'
|
|
124
145
|
|
|
125
|
-
const nameSchema = text({
|
|
146
|
+
const nameSchema = text(true, {
|
|
126
147
|
minLength: 2,
|
|
127
148
|
maxLength: 50,
|
|
128
149
|
pattern: /^[a-zA-Z\s]+$/,
|
|
129
150
|
transform: (val) => val.trim()
|
|
130
151
|
})
|
|
152
|
+
|
|
153
|
+
const optionalText = text(false)
|
|
131
154
|
```
|
|
132
155
|
|
|
133
|
-
#### `number(options?)`
|
|
156
|
+
#### `number(required?, options?)`
|
|
134
157
|
|
|
135
158
|
Validates numeric values with range and type constraints.
|
|
136
159
|
|
|
137
160
|
```typescript
|
|
138
161
|
import { number } from '@hy_ong/zod-kit'
|
|
139
162
|
|
|
140
|
-
const ageSchema = number({
|
|
163
|
+
const ageSchema = number(true, {
|
|
141
164
|
min: 0,
|
|
142
165
|
max: 150,
|
|
143
166
|
integer: true,
|
|
144
167
|
positive: true
|
|
145
168
|
})
|
|
169
|
+
|
|
170
|
+
const optionalNumber = number(false)
|
|
146
171
|
```
|
|
147
172
|
|
|
148
|
-
#### `url(options?)`
|
|
173
|
+
#### `url(required?, options?)`
|
|
149
174
|
|
|
150
175
|
URL validation with protocol and domain restrictions.
|
|
151
176
|
|
|
152
177
|
```typescript
|
|
153
178
|
import { url } from '@hy_ong/zod-kit'
|
|
154
179
|
|
|
155
|
-
const urlSchema = url({
|
|
180
|
+
const urlSchema = url(true, {
|
|
156
181
|
protocols: ['https'], // Only HTTPS allowed
|
|
157
182
|
allowedDomains: ['safe.com'], // Domain whitelist
|
|
158
183
|
requireTLD: true // Require top-level domain
|
|
159
184
|
})
|
|
185
|
+
|
|
186
|
+
const optionalUrl = url(false)
|
|
160
187
|
```
|
|
161
188
|
|
|
162
|
-
#### `boolean(options?)`
|
|
189
|
+
#### `boolean(required?, options?)`
|
|
163
190
|
|
|
164
191
|
Boolean validation with flexible input handling.
|
|
165
192
|
|
|
166
193
|
```typescript
|
|
167
194
|
import { boolean } from '@hy_ong/zod-kit'
|
|
168
195
|
|
|
169
|
-
const consentSchema = boolean({
|
|
170
|
-
required: true,
|
|
196
|
+
const consentSchema = boolean(true, {
|
|
171
197
|
trueValues: ['yes', '1', 'true'], // Custom truthy values
|
|
172
198
|
falseValues: ['no', '0', 'false'] // Custom falsy values
|
|
173
199
|
})
|
|
200
|
+
|
|
201
|
+
const optionalBoolean = boolean(false)
|
|
174
202
|
```
|
|
175
203
|
|
|
176
|
-
#### `datetime(options?)`
|
|
204
|
+
#### `datetime(required?, options?)`
|
|
177
205
|
|
|
178
206
|
Validates datetime with comprehensive format support and timezone handling.
|
|
179
207
|
|
|
@@ -181,11 +209,11 @@ Validates datetime with comprehensive format support and timezone handling.
|
|
|
181
209
|
import { datetime } from '@hy_ong/zod-kit'
|
|
182
210
|
|
|
183
211
|
// Basic datetime validation
|
|
184
|
-
const basicSchema = datetime()
|
|
212
|
+
const basicSchema = datetime(true)
|
|
185
213
|
basicSchema.parse('2024-03-15 14:30') // ✓ Valid
|
|
186
214
|
|
|
187
215
|
// Business hours validation
|
|
188
|
-
const businessHours = datetime({
|
|
216
|
+
const businessHours = datetime(true, {
|
|
189
217
|
format: 'YYYY-MM-DD HH:mm',
|
|
190
218
|
minHour: 9,
|
|
191
219
|
maxHour: 17,
|
|
@@ -193,19 +221,22 @@ const businessHours = datetime({
|
|
|
193
221
|
})
|
|
194
222
|
|
|
195
223
|
// Timezone-aware validation
|
|
196
|
-
const timezoneSchema = datetime({
|
|
224
|
+
const timezoneSchema = datetime(true, {
|
|
197
225
|
timezone: 'Asia/Taipei',
|
|
198
226
|
mustBeFuture: true
|
|
199
227
|
})
|
|
200
228
|
|
|
201
229
|
// Multiple format support
|
|
202
|
-
const flexibleSchema = datetime({
|
|
230
|
+
const flexibleSchema = datetime(true, {
|
|
203
231
|
format: 'DD/MM/YYYY HH:mm'
|
|
204
232
|
})
|
|
205
233
|
flexibleSchema.parse('15/03/2024 14:30') // ✓ Valid
|
|
234
|
+
|
|
235
|
+
// Optional datetime
|
|
236
|
+
const optionalDatetime = datetime(false)
|
|
206
237
|
```
|
|
207
238
|
|
|
208
|
-
#### `time(options?)`
|
|
239
|
+
#### `time(required?, options?)`
|
|
209
240
|
|
|
210
241
|
Time validation with multiple formats and constraints.
|
|
211
242
|
|
|
@@ -213,15 +244,15 @@ Time validation with multiple formats and constraints.
|
|
|
213
244
|
import { time } from '@hy_ong/zod-kit'
|
|
214
245
|
|
|
215
246
|
// Basic time validation (24-hour format)
|
|
216
|
-
const basicSchema = time()
|
|
247
|
+
const basicSchema = time(true)
|
|
217
248
|
basicSchema.parse('14:30') // ✓ Valid
|
|
218
249
|
|
|
219
250
|
// 12-hour format with AM/PM
|
|
220
|
-
const ampmSchema = time({ format: 'hh:mm A' })
|
|
251
|
+
const ampmSchema = time(true, { format: 'hh:mm A' })
|
|
221
252
|
ampmSchema.parse('02:30 PM') // ✓ Valid
|
|
222
253
|
|
|
223
254
|
// Business hours validation
|
|
224
|
-
const businessHours = time({
|
|
255
|
+
const businessHours = time(true, {
|
|
225
256
|
format: 'HH:mm',
|
|
226
257
|
minHour: 9,
|
|
227
258
|
maxHour: 17,
|
|
@@ -229,28 +260,33 @@ const businessHours = time({
|
|
|
229
260
|
})
|
|
230
261
|
|
|
231
262
|
// Time range validation
|
|
232
|
-
const timeRangeSchema = time({
|
|
263
|
+
const timeRangeSchema = time(true, {
|
|
233
264
|
min: '09:00',
|
|
234
265
|
max: '17:00'
|
|
235
266
|
})
|
|
267
|
+
|
|
268
|
+
// Optional time
|
|
269
|
+
const optionalTime = time(false)
|
|
236
270
|
```
|
|
237
271
|
|
|
238
|
-
#### `date(options?)`
|
|
272
|
+
#### `date(required?, options?)`
|
|
239
273
|
|
|
240
274
|
Date validation with range and format constraints.
|
|
241
275
|
|
|
242
276
|
```typescript
|
|
243
277
|
import { date } from '@hy_ong/zod-kit'
|
|
244
278
|
|
|
245
|
-
const birthdateSchema = date({
|
|
279
|
+
const birthdateSchema = date(true, {
|
|
246
280
|
format: 'YYYY-MM-DD',
|
|
247
281
|
minDate: '1900-01-01',
|
|
248
282
|
maxDate: new Date(),
|
|
249
283
|
timezone: 'Asia/Taipei'
|
|
250
284
|
})
|
|
285
|
+
|
|
286
|
+
const optionalDate = date(false)
|
|
251
287
|
```
|
|
252
288
|
|
|
253
|
-
#### `file(options?)`
|
|
289
|
+
#### `file(required?, options?)`
|
|
254
290
|
|
|
255
291
|
File validation with MIME type filtering and size constraints.
|
|
256
292
|
|
|
@@ -258,113 +294,127 @@ File validation with MIME type filtering and size constraints.
|
|
|
258
294
|
import { file } from '@hy_ong/zod-kit'
|
|
259
295
|
|
|
260
296
|
// Basic file validation
|
|
261
|
-
const basicSchema = file()
|
|
297
|
+
const basicSchema = file(true)
|
|
262
298
|
basicSchema.parse(new File(['content'], 'test.txt'))
|
|
263
299
|
|
|
264
300
|
// Size restrictions
|
|
265
|
-
const sizeSchema = file({
|
|
301
|
+
const sizeSchema = file(true, {
|
|
266
302
|
maxSize: 1024 * 1024, // 1MB
|
|
267
303
|
minSize: 1024 // 1KB
|
|
268
304
|
})
|
|
269
305
|
|
|
270
306
|
// Extension restrictions
|
|
271
|
-
const imageSchema = file({
|
|
307
|
+
const imageSchema = file(true, {
|
|
272
308
|
extension: ['.jpg', '.png', '.gif'],
|
|
273
309
|
maxSize: 5 * 1024 * 1024 // 5MB
|
|
274
310
|
})
|
|
275
311
|
|
|
276
312
|
// MIME type restrictions
|
|
277
|
-
const documentSchema = file({
|
|
313
|
+
const documentSchema = file(true, {
|
|
278
314
|
type: ['application/pdf', 'application/msword'],
|
|
279
315
|
maxSize: 10 * 1024 * 1024 // 10MB
|
|
280
316
|
})
|
|
281
317
|
|
|
282
318
|
// Image files only
|
|
283
|
-
const imageOnlySchema = file({ imageOnly: true })
|
|
319
|
+
const imageOnlySchema = file(true, { imageOnly: true })
|
|
320
|
+
|
|
321
|
+
// Optional file
|
|
322
|
+
const optionalFile = file(false)
|
|
284
323
|
```
|
|
285
324
|
|
|
286
|
-
#### `id(options?)`
|
|
325
|
+
#### `id(required?, options?)`
|
|
287
326
|
|
|
288
327
|
Flexible ID validation supporting multiple formats.
|
|
289
328
|
|
|
290
329
|
```typescript
|
|
291
330
|
import { id } from '@hy_ong/zod-kit'
|
|
292
331
|
|
|
293
|
-
const userIdSchema = id({
|
|
332
|
+
const userIdSchema = id(true, {
|
|
294
333
|
type: 'uuid', // 'uuid', 'nanoid', 'objectId', 'auto', etc.
|
|
295
334
|
allowedTypes: ['uuid', 'nanoid'], // Multiple allowed types
|
|
296
335
|
customRegex: /^USR_[A-Z0-9]+$/ // Custom pattern
|
|
297
336
|
})
|
|
337
|
+
|
|
338
|
+
const optionalId = id(false)
|
|
298
339
|
```
|
|
299
340
|
|
|
300
341
|
### Taiwan-Specific Validators
|
|
301
342
|
|
|
302
|
-
#### `nationalId(options?)`
|
|
343
|
+
#### `nationalId(required?, options?)`
|
|
303
344
|
|
|
304
345
|
Validates Taiwan National ID (身份證字號).
|
|
305
346
|
|
|
306
347
|
```typescript
|
|
307
348
|
import { nationalId } from '@hy_ong/zod-kit'
|
|
308
349
|
|
|
309
|
-
const idSchema = nationalId({
|
|
310
|
-
required: true,
|
|
350
|
+
const idSchema = nationalId(true, {
|
|
311
351
|
normalize: true, // Convert to uppercase
|
|
312
352
|
whitelist: ['A123456789'] // Allow specific IDs
|
|
313
353
|
})
|
|
314
354
|
|
|
315
355
|
idSchema.parse('A123456789') // ✅ Valid Taiwan National ID
|
|
356
|
+
|
|
357
|
+
const optionalId = nationalId(false)
|
|
316
358
|
```
|
|
317
359
|
|
|
318
|
-
#### `businessId(options?)`
|
|
360
|
+
#### `businessId(required?, options?)`
|
|
319
361
|
|
|
320
362
|
Validates Taiwan Business ID (統一編號).
|
|
321
363
|
|
|
322
364
|
```typescript
|
|
323
365
|
import { businessId } from '@hy_ong/zod-kit'
|
|
324
366
|
|
|
325
|
-
const bizSchema = businessId()
|
|
367
|
+
const bizSchema = businessId(true)
|
|
326
368
|
bizSchema.parse('12345675') // ✅ Valid business ID with checksum
|
|
369
|
+
|
|
370
|
+
const optionalBizId = businessId(false)
|
|
327
371
|
```
|
|
328
372
|
|
|
329
|
-
#### `mobile(options?)`
|
|
373
|
+
#### `mobile(required?, options?)`
|
|
330
374
|
|
|
331
375
|
Validates Taiwan mobile phone numbers.
|
|
332
376
|
|
|
333
377
|
```typescript
|
|
334
378
|
import { mobile } from '@hy_ong/zod-kit'
|
|
335
379
|
|
|
336
|
-
const phoneSchema = mobile({
|
|
380
|
+
const phoneSchema = mobile(true, {
|
|
337
381
|
allowInternational: true, // Allow +886 prefix
|
|
338
382
|
allowSeparators: true, // Allow 0912-345-678
|
|
339
383
|
operators: ['09'] // Restrict to specific operators
|
|
340
384
|
})
|
|
385
|
+
|
|
386
|
+
const optionalMobile = mobile(false)
|
|
341
387
|
```
|
|
342
388
|
|
|
343
|
-
#### `tel(options?)`
|
|
389
|
+
#### `tel(required?, options?)`
|
|
344
390
|
|
|
345
391
|
Validates Taiwan landline telephone numbers.
|
|
346
392
|
|
|
347
393
|
```typescript
|
|
348
394
|
import { tel } from '@hy_ong/zod-kit'
|
|
349
395
|
|
|
350
|
-
const landlineSchema = tel({
|
|
396
|
+
const landlineSchema = tel(true, {
|
|
351
397
|
allowSeparators: true, // Allow 02-1234-5678
|
|
352
398
|
areaCodes: ['02', '03'] // Restrict to specific areas
|
|
353
399
|
})
|
|
400
|
+
|
|
401
|
+
const optionalTel = tel(false)
|
|
354
402
|
```
|
|
355
403
|
|
|
356
|
-
#### `fax(options?)`
|
|
404
|
+
#### `fax(required?, options?)`
|
|
357
405
|
|
|
358
406
|
Validates Taiwan fax numbers (same format as landline).
|
|
359
407
|
|
|
360
408
|
```typescript
|
|
361
409
|
import { fax } from '@hy_ong/zod-kit'
|
|
362
410
|
|
|
363
|
-
const faxSchema = fax()
|
|
411
|
+
const faxSchema = fax(true)
|
|
364
412
|
faxSchema.parse('02-2345-6789') // ✅ Valid fax number
|
|
413
|
+
|
|
414
|
+
const optionalFax = fax(false)
|
|
365
415
|
```
|
|
366
416
|
|
|
367
|
-
#### `postalCode(options?)`
|
|
417
|
+
#### `postalCode(required?, options?)`
|
|
368
418
|
|
|
369
419
|
Validates Taiwan postal codes with support for 3-digit, 5-digit, and 6-digit formats.
|
|
370
420
|
|
|
@@ -372,31 +422,34 @@ Validates Taiwan postal codes with support for 3-digit, 5-digit, and 6-digit for
|
|
|
372
422
|
import { postalCode } from '@hy_ong/zod-kit'
|
|
373
423
|
|
|
374
424
|
// Accept 3-digit or 6-digit formats (recommended)
|
|
375
|
-
const modernSchema = postalCode()
|
|
425
|
+
const modernSchema = postalCode(true)
|
|
376
426
|
modernSchema.parse('100') // ✅ Valid 3-digit
|
|
377
427
|
modernSchema.parse('100001') // ✅ Valid 6-digit
|
|
378
428
|
|
|
379
429
|
// Accept all formats
|
|
380
|
-
const flexibleSchema = postalCode({ format: 'all' })
|
|
430
|
+
const flexibleSchema = postalCode(true, { format: 'all' })
|
|
381
431
|
flexibleSchema.parse('100') // ✅ Valid
|
|
382
432
|
flexibleSchema.parse('10001') // ✅ Valid (5-digit legacy)
|
|
383
433
|
flexibleSchema.parse('100001') // ✅ Valid
|
|
384
434
|
|
|
385
435
|
// Only 6-digit format (current standard)
|
|
386
|
-
const modernOnlySchema = postalCode({ format: '6' })
|
|
436
|
+
const modernOnlySchema = postalCode(true, { format: '6' })
|
|
387
437
|
modernOnlySchema.parse('100001') // ✅ Valid
|
|
388
438
|
modernOnlySchema.parse('100') // ❌ Invalid
|
|
389
439
|
|
|
390
440
|
// With dashes allowed
|
|
391
|
-
const dashSchema = postalCode({ allowDashes: true })
|
|
441
|
+
const dashSchema = postalCode(true, { allowDashes: true })
|
|
392
442
|
dashSchema.parse('100-001') // ✅ Valid (normalized to '100001')
|
|
393
443
|
|
|
394
444
|
// Specific areas only
|
|
395
|
-
const taipeiSchema = postalCode({
|
|
445
|
+
const taipeiSchema = postalCode(true, {
|
|
396
446
|
allowedPrefixes: ['100', '103', '104', '105', '106']
|
|
397
447
|
})
|
|
398
448
|
taipeiSchema.parse('100001') // ✅ Valid (Taipei area)
|
|
399
449
|
taipeiSchema.parse('200001') // ❌ Invalid (not in allowlist)
|
|
450
|
+
|
|
451
|
+
// Optional postal code
|
|
452
|
+
const optionalPostal = postalCode(false)
|
|
400
453
|
```
|
|
401
454
|
|
|
402
455
|
## 🌐 Internationalization
|
|
@@ -411,7 +464,7 @@ setLocale('zh-TW') // Traditional Chinese
|
|
|
411
464
|
setLocale('en') // English (default)
|
|
412
465
|
|
|
413
466
|
// Or use custom messages per validator
|
|
414
|
-
const emailSchema = email({
|
|
467
|
+
const emailSchema = email(true, {
|
|
415
468
|
i18n: {
|
|
416
469
|
en: {
|
|
417
470
|
required: 'Email is required',
|
|
@@ -429,10 +482,10 @@ const emailSchema = email({
|
|
|
429
482
|
|
|
430
483
|
### Optional Fields
|
|
431
484
|
|
|
432
|
-
Make any field optional by
|
|
485
|
+
Make any field optional by passing `false` as the first argument:
|
|
433
486
|
|
|
434
487
|
```typescript
|
|
435
|
-
const optionalEmail = email(
|
|
488
|
+
const optionalEmail = email(false)
|
|
436
489
|
|
|
437
490
|
optionalEmail.parse(null) // ✅ null
|
|
438
491
|
optionalEmail.parse('') // ✅ null
|
|
@@ -444,7 +497,7 @@ optionalEmail.parse('test@example.com') // ✅ "test@example.com"
|
|
|
444
497
|
Transform values during validation:
|
|
445
498
|
|
|
446
499
|
```typescript
|
|
447
|
-
const trimmedText = text({
|
|
500
|
+
const trimmedText = text(true, {
|
|
448
501
|
transform: (val) => val.trim().toLowerCase(),
|
|
449
502
|
minLength: 1
|
|
450
503
|
})
|
|
@@ -452,12 +505,25 @@ const trimmedText = text({
|
|
|
452
505
|
trimmedText.parse(' HELLO ') // ✅ "hello"
|
|
453
506
|
```
|
|
454
507
|
|
|
508
|
+
### Default Values
|
|
509
|
+
|
|
510
|
+
Provide default values for empty inputs:
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
const emailWithDefault = email(true, {
|
|
514
|
+
defaultValue: 'default@example.com'
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
emailWithDefault.parse('') // ✅ "default@example.com"
|
|
518
|
+
emailWithDefault.parse(null) // ✅ "default@example.com"
|
|
519
|
+
```
|
|
520
|
+
|
|
455
521
|
### Whitelist Validation
|
|
456
522
|
|
|
457
523
|
Allow specific values regardless of format:
|
|
458
524
|
|
|
459
525
|
```typescript
|
|
460
|
-
const flexibleId = id({
|
|
526
|
+
const flexibleId = id(true, {
|
|
461
527
|
type: 'uuid',
|
|
462
528
|
whitelist: ['admin', 'system', 'test-user']
|
|
463
529
|
})
|
|
@@ -475,8 +541,8 @@ import { z } from 'zod'
|
|
|
475
541
|
import { email, password } from '@hy_ong/zod-kit'
|
|
476
542
|
|
|
477
543
|
const userSchema = z.object({
|
|
478
|
-
email: email(),
|
|
479
|
-
password: password({ minLength: 8 }),
|
|
544
|
+
email: email(true),
|
|
545
|
+
password: password(true, { minLength: 8 }),
|
|
480
546
|
confirmPassword: z.string()
|
|
481
547
|
}).refine(data => data.password === data.confirmPassword, {
|
|
482
548
|
message: "Passwords don't match"
|