node-env-resolver 5.0.1 → 6.0.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/README.md +119 -57
- package/dist/audit.d.cts +23 -0
- package/dist/audit.d.ts +23 -0
- package/dist/builder.cjs +1 -1
- package/dist/builder.js +1 -1
- package/dist/chunk-3KAHSFPX.js +9 -0
- package/dist/{chunk-FH54ESWI.cjs → chunk-5E2SZR3V.cjs} +3 -3
- package/dist/{chunk-XJLMVSPN.js → chunk-GMQOMCGI.js} +1 -1
- package/dist/chunk-LCW5BDAW.js +15 -0
- package/dist/chunk-OSMNJM5N.cjs +9 -0
- package/dist/chunk-PNUGGPRS.cjs +15 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.js +1 -1
- package/dist/resolver.cjs +1 -1
- package/dist/resolver.js +1 -1
- package/dist/resolvers.cjs +1 -1
- package/dist/resolvers.d.cts +44 -266
- package/dist/resolvers.d.ts +44 -266
- package/dist/resolvers.js +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.cts +60 -0
- package/dist/utils.d.ts +60 -0
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -1
- package/dist/valibot.js +1 -1
- package/dist/validators.cjs +2 -2
- package/dist/validators.d.cts +771 -17
- package/dist/validators.d.ts +771 -17
- package/dist/validators.js +2 -2
- package/dist/web.cjs +3 -3
- package/dist/web.js +1 -1
- package/dist/zod.cjs +1 -1
- package/dist/zod.js +1 -1
- package/package.json +25 -13
- package/dist/chunk-4YVXFL3I.cjs +0 -9
- package/dist/chunk-DIXI6MUB.js +0 -9
- package/dist/chunk-TAX63ALN.js +0 -16
- package/dist/chunk-XPL5XLTA.cjs +0 -16
package/dist/validators.d.cts
CHANGED
|
@@ -8,73 +8,111 @@ import { Validator } from './types.cjs';
|
|
|
8
8
|
* 'postgres', 'mysql', 'mongodb', 'redis', 'http', 'https', 'url', 'email', 'port', 'json', 'date', 'timestamp'
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Result of a validation operation
|
|
13
|
+
*/
|
|
11
14
|
interface ValidationResult {
|
|
15
|
+
/** Whether the validation passed */
|
|
12
16
|
valid: boolean;
|
|
17
|
+
/** Error message if validation failed */
|
|
13
18
|
error?: string;
|
|
19
|
+
/** Parsed/transformed value if validation succeeded */
|
|
14
20
|
value?: unknown;
|
|
15
21
|
}
|
|
16
22
|
/**
|
|
17
23
|
* Validate PostgreSQL connection string
|
|
24
|
+
* Checks for postgres:// or postgresql:// URLs
|
|
25
|
+
*
|
|
26
|
+
* @param value - Connection string to validate
|
|
27
|
+
* @returns Validation result with success/error information
|
|
18
28
|
*/
|
|
19
29
|
declare function validatePostgres(value: string): ValidationResult;
|
|
20
30
|
/**
|
|
21
31
|
* Validate MySQL connection string
|
|
32
|
+
* Checks for mysql:// URLs
|
|
33
|
+
*
|
|
34
|
+
* @param value - Connection string to validate
|
|
35
|
+
* @returns Validation result with success/error information
|
|
22
36
|
*/
|
|
23
37
|
declare function validateMysql(value: string): ValidationResult;
|
|
24
38
|
/**
|
|
25
39
|
* Validate MongoDB connection string
|
|
26
40
|
* Supports mongodb:// and mongodb+srv:// with replica sets
|
|
41
|
+
*
|
|
42
|
+
* @param value - Connection string to validate
|
|
43
|
+
* @returns Validation result with success/error information
|
|
27
44
|
*/
|
|
28
45
|
declare function validateMongodb(value: string): ValidationResult;
|
|
29
46
|
/**
|
|
30
47
|
* Validate Redis connection string
|
|
31
48
|
* Supports redis:// and rediss:// (TLS)
|
|
49
|
+
*
|
|
50
|
+
* @param value - Connection string to validate
|
|
51
|
+
* @returns Validation result with success/error information
|
|
32
52
|
*/
|
|
33
53
|
declare function validateRedis(value: string): ValidationResult;
|
|
34
|
-
/**
|
|
35
|
-
* Validate HTTP/HTTPS URL (accepts both)
|
|
36
|
-
*/
|
|
37
|
-
declare function validateHttp(value: string): ValidationResult;
|
|
38
|
-
/**
|
|
39
|
-
* Validate HTTPS URL only (strict)
|
|
40
|
-
*/
|
|
41
|
-
declare function validateHttps(value: string): ValidationResult;
|
|
42
54
|
/**
|
|
43
55
|
* Validate generic URL (with protocol whitelist for security)
|
|
56
|
+
* Only allows safe protocols: http, https, ws, wss, ftp, ftps, file, postgres, mysql, mongodb, redis
|
|
57
|
+
*
|
|
58
|
+
* @param value - URL to validate
|
|
59
|
+
* @returns Validation result with success/error information
|
|
44
60
|
*/
|
|
45
61
|
declare function validateUrl(value: string): ValidationResult;
|
|
46
62
|
/**
|
|
47
|
-
* Validate email address
|
|
63
|
+
* Validate email address using RFC-compliant regex
|
|
64
|
+
* Also checks maximum length (254 characters)
|
|
65
|
+
*
|
|
66
|
+
* @param value - Email address to validate
|
|
67
|
+
* @returns Validation result with success/error information
|
|
48
68
|
*/
|
|
49
69
|
declare function validateEmail(value: string): ValidationResult;
|
|
50
70
|
/**
|
|
51
|
-
* Validate port number
|
|
71
|
+
* Validate port number (1-65535)
|
|
72
|
+
*
|
|
73
|
+
* @param value - Port number to validate
|
|
74
|
+
* @returns Validation result with success/error information
|
|
52
75
|
*/
|
|
53
76
|
declare function validatePort(value: string): ValidationResult;
|
|
54
77
|
/**
|
|
55
78
|
* Validate number
|
|
79
|
+
* Converts string to number and checks if it's a valid number
|
|
80
|
+
*
|
|
81
|
+
* @param value - Number string to validate
|
|
82
|
+
* @returns Validation result with success/error information
|
|
56
83
|
*/
|
|
57
84
|
declare function validateNumber(value: string): ValidationResult;
|
|
58
85
|
/**
|
|
59
86
|
* Validate boolean
|
|
87
|
+
* Accepts: true/1/yes/on, false/0/no/off
|
|
88
|
+
*
|
|
89
|
+
* @param value - Boolean string to validate
|
|
90
|
+
* @returns Validation result with success/error information
|
|
60
91
|
*/
|
|
61
92
|
declare function validateBoolean(value: string): ValidationResult;
|
|
62
|
-
/**
|
|
63
|
-
* Validate JSON
|
|
64
|
-
*/
|
|
65
|
-
declare function validateJson(value: string): ValidationResult;
|
|
66
93
|
/**
|
|
67
94
|
* Validate and normalize date string to ISO 8601
|
|
68
95
|
* Accepts various formats and coerces them to YYYY-MM-DD or ISO 8601 datetime
|
|
96
|
+
*
|
|
97
|
+
* @param value - Date string to validate
|
|
98
|
+
* @returns Validation result with success/error information
|
|
69
99
|
*/
|
|
70
100
|
declare function validateDate(value: string): ValidationResult;
|
|
71
101
|
/**
|
|
72
102
|
* Validate Unix timestamp (seconds since epoch)
|
|
103
|
+
* Validates positive integers representing Unix timestamps
|
|
104
|
+
*
|
|
105
|
+
* @param value - Timestamp string to validate
|
|
106
|
+
* @returns Validation result with success/error information
|
|
73
107
|
*/
|
|
74
108
|
declare function validateTimestamp(value: string): ValidationResult;
|
|
75
109
|
/**
|
|
76
110
|
* Validate and parse time duration (Go-style: 5s, 2h, 30m, etc.)
|
|
77
111
|
* Returns milliseconds
|
|
112
|
+
* Supports formats like: 5s, 2h, 30m, 1.5h, 2h30m, etc.
|
|
113
|
+
*
|
|
114
|
+
* @param value - Duration string to validate
|
|
115
|
+
* @returns Validation result with parsed value in milliseconds
|
|
78
116
|
*/
|
|
79
117
|
declare function validateDuration(value: string): ValidationResult & {
|
|
80
118
|
value?: number;
|
|
@@ -88,7 +126,14 @@ declare function validateDuration(value: string): ValidationResult & {
|
|
|
88
126
|
* @returns ValidationResult with file content or error
|
|
89
127
|
*/
|
|
90
128
|
declare function validateFile(value: string, key?: string): ValidationResult;
|
|
91
|
-
|
|
129
|
+
/**
|
|
130
|
+
* HTTP/HTTPS URL validator function factory
|
|
131
|
+
* Creates a validator that accepts both http:// and https:// URLs
|
|
132
|
+
*
|
|
133
|
+
* @param opts - Validation options
|
|
134
|
+
* @returns HTTP URL validator
|
|
135
|
+
*/
|
|
136
|
+
declare function http<Opts extends {
|
|
92
137
|
default?: string;
|
|
93
138
|
optional?: boolean;
|
|
94
139
|
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
@@ -100,7 +145,14 @@ declare function httpValidator<Opts extends {
|
|
|
100
145
|
optional?: Opts['optional'];
|
|
101
146
|
default?: Opts['default'];
|
|
102
147
|
};
|
|
103
|
-
|
|
148
|
+
/**
|
|
149
|
+
* JSON validator function factory
|
|
150
|
+
* Creates a validator that parses JSON strings
|
|
151
|
+
*
|
|
152
|
+
* @param opts - Validation options
|
|
153
|
+
* @returns JSON validator
|
|
154
|
+
*/
|
|
155
|
+
declare function json<Opts extends {
|
|
104
156
|
default?: unknown;
|
|
105
157
|
optional?: boolean;
|
|
106
158
|
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
@@ -112,5 +164,707 @@ declare function jsonValidator<Opts extends {
|
|
|
112
164
|
optional?: Opts['optional'];
|
|
113
165
|
default?: Opts['default'];
|
|
114
166
|
};
|
|
167
|
+
/**
|
|
168
|
+
* String validator with length and pattern validation
|
|
169
|
+
*
|
|
170
|
+
* @param opts Validation options
|
|
171
|
+
* @returns String validator
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { string } from 'node-env-resolver/validators';
|
|
176
|
+
*
|
|
177
|
+
* const config = resolve({
|
|
178
|
+
* API_KEY: string({ min: 32, pattern: '^[a-zA-Z0-9]+$' }),
|
|
179
|
+
* NAME: string({ optional: true, max: 100 }),
|
|
180
|
+
* DESCRIPTION: string({ allowEmpty: true })
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
declare function string<Opts extends {
|
|
185
|
+
/** Default value if not provided */
|
|
186
|
+
default?: string;
|
|
187
|
+
/** Make the field optional */
|
|
188
|
+
optional?: boolean;
|
|
189
|
+
/** Minimum string length */
|
|
190
|
+
min?: number;
|
|
191
|
+
/** Maximum string length */
|
|
192
|
+
max?: number;
|
|
193
|
+
/** Allow empty strings */
|
|
194
|
+
allowEmpty?: boolean;
|
|
195
|
+
/** Regex pattern to validate against */
|
|
196
|
+
pattern?: string;
|
|
197
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
198
|
+
optional: true;
|
|
199
|
+
} ? Validator<string> & {
|
|
200
|
+
optional: true;
|
|
201
|
+
default?: Opts['default'];
|
|
202
|
+
} : Validator<string> & {
|
|
203
|
+
optional?: Opts['optional'];
|
|
204
|
+
default?: Opts['default'];
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Number validator with range validation
|
|
208
|
+
*
|
|
209
|
+
* @param opts Validation options
|
|
210
|
+
* @returns Number validator
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* import { number } from 'node-env-resolver/validators';
|
|
215
|
+
*
|
|
216
|
+
* const config = resolve({
|
|
217
|
+
* PORT: number({ min: 1, max: 65535 }),
|
|
218
|
+
* TIMEOUT: number({ default: 5000, optional: true }),
|
|
219
|
+
* MAX_RETRIES: number({ min: 0, max: 10 })
|
|
220
|
+
* });
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
declare function number<Opts extends {
|
|
224
|
+
/** Default value if not provided */
|
|
225
|
+
default?: number;
|
|
226
|
+
/** Make the field optional */
|
|
227
|
+
optional?: boolean;
|
|
228
|
+
/** Minimum number value */
|
|
229
|
+
min?: number;
|
|
230
|
+
/** Maximum number value */
|
|
231
|
+
max?: number;
|
|
232
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
233
|
+
optional: true;
|
|
234
|
+
} ? Validator<number> & {
|
|
235
|
+
optional: true;
|
|
236
|
+
default?: Opts['default'];
|
|
237
|
+
} : Validator<number> & {
|
|
238
|
+
optional?: Opts['optional'];
|
|
239
|
+
default?: Opts['default'];
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* Boolean validator with string coercion
|
|
243
|
+
* Accepts: true/1/yes/on, false/0/no/off
|
|
244
|
+
*
|
|
245
|
+
* @param opts Validation options
|
|
246
|
+
* @returns Boolean validator
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* import { boolean } from 'node-env-resolver/validators';
|
|
251
|
+
*
|
|
252
|
+
* const config = resolve({
|
|
253
|
+
* DEBUG: boolean({ default: false }),
|
|
254
|
+
* ENABLE_LOGGING: boolean(),
|
|
255
|
+
* FEATURE_FLAG: boolean({ optional: true })
|
|
256
|
+
* });
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
declare function boolean<Opts extends {
|
|
260
|
+
/** Default value if not provided */
|
|
261
|
+
default?: boolean;
|
|
262
|
+
/** Make the field optional */
|
|
263
|
+
optional?: boolean;
|
|
264
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
265
|
+
optional: true;
|
|
266
|
+
} ? Validator<boolean> & {
|
|
267
|
+
optional: true;
|
|
268
|
+
default?: Opts['default'];
|
|
269
|
+
} : Validator<boolean> & {
|
|
270
|
+
optional?: Opts['optional'];
|
|
271
|
+
default?: Opts['default'];
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* URL validator using the built-in URL constructor
|
|
275
|
+
*
|
|
276
|
+
* @param opts Validation options
|
|
277
|
+
* @returns URL validator
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* import { url } from 'node-env-resolver/validators';
|
|
282
|
+
*
|
|
283
|
+
* const config = resolve({
|
|
284
|
+
* API_URL: url(),
|
|
285
|
+
* WEBHOOK_URL: url({ optional: true }),
|
|
286
|
+
* BASE_URL: url({ default: 'https://api.example.com' })
|
|
287
|
+
* });
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
declare function url<Opts extends {
|
|
291
|
+
/** Default value if not provided */
|
|
292
|
+
default?: string;
|
|
293
|
+
/** Make the field optional */
|
|
294
|
+
optional?: boolean;
|
|
295
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
296
|
+
optional: true;
|
|
297
|
+
} ? Validator<string> & {
|
|
298
|
+
optional: true;
|
|
299
|
+
default?: Opts['default'];
|
|
300
|
+
} : Validator<string> & {
|
|
301
|
+
optional?: Opts['optional'];
|
|
302
|
+
default?: Opts['default'];
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Email address validator using regex pattern
|
|
306
|
+
*
|
|
307
|
+
* @param opts Validation options
|
|
308
|
+
* @returns Email validator
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* import { email } from 'node-env-resolver/validators';
|
|
313
|
+
*
|
|
314
|
+
* const config = resolve({
|
|
315
|
+
* ADMIN_EMAIL: email(),
|
|
316
|
+
* CONTACT_EMAIL: email({ optional: true }),
|
|
317
|
+
* SUPPORT_EMAIL: email({ default: 'support@example.com' })
|
|
318
|
+
* });
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
declare function email<Opts extends {
|
|
322
|
+
/** Default value if not provided */
|
|
323
|
+
default?: string;
|
|
324
|
+
/** Make the field optional */
|
|
325
|
+
optional?: boolean;
|
|
326
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
327
|
+
optional: true;
|
|
328
|
+
} ? Validator<string> & {
|
|
329
|
+
optional: true;
|
|
330
|
+
default?: Opts['default'];
|
|
331
|
+
} : Validator<string> & {
|
|
332
|
+
optional?: Opts['optional'];
|
|
333
|
+
default?: Opts['default'];
|
|
334
|
+
};
|
|
335
|
+
/**
|
|
336
|
+
* Port number validator (1-65535)
|
|
337
|
+
*
|
|
338
|
+
* @param opts Validation options
|
|
339
|
+
* @returns Port validator
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* import { port } from 'node-env-resolver/validators';
|
|
344
|
+
*
|
|
345
|
+
* const config = resolve({
|
|
346
|
+
* PORT: port({ default: 3000 }),
|
|
347
|
+
* REDIS_PORT: port(),
|
|
348
|
+
* MONITORING_PORT: port({ optional: true })
|
|
349
|
+
* });
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
declare function port<Opts extends {
|
|
353
|
+
/** Default value if not provided */
|
|
354
|
+
default?: number;
|
|
355
|
+
/** Make the field optional */
|
|
356
|
+
optional?: boolean;
|
|
357
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
358
|
+
optional: true;
|
|
359
|
+
} ? Validator<number> & {
|
|
360
|
+
optional: true;
|
|
361
|
+
default?: Opts['default'];
|
|
362
|
+
} : Validator<number> & {
|
|
363
|
+
optional?: Opts['optional'];
|
|
364
|
+
default?: Opts['default'];
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* PostgreSQL connection string validator
|
|
368
|
+
* Validates postgres:// and postgresql:// URLs
|
|
369
|
+
*
|
|
370
|
+
* @param opts Validation options
|
|
371
|
+
* @returns PostgreSQL URL validator
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```ts
|
|
375
|
+
* import { postgres } from 'node-env-resolver/validators';
|
|
376
|
+
*
|
|
377
|
+
* const config = resolve({
|
|
378
|
+
* DATABASE_URL: postgres(),
|
|
379
|
+
* BACKUP_DB_URL: postgres({ optional: true }),
|
|
380
|
+
* TEST_DATABASE_URL: postgres({ default: 'postgres://localhost:5432/test' })
|
|
381
|
+
* });
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
declare function postgres<Opts extends {
|
|
385
|
+
/** Default value if not provided */
|
|
386
|
+
default?: string;
|
|
387
|
+
/** Make the field optional */
|
|
388
|
+
optional?: boolean;
|
|
389
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
390
|
+
optional: true;
|
|
391
|
+
} ? Validator<string> & {
|
|
392
|
+
optional: true;
|
|
393
|
+
default?: Opts['default'];
|
|
394
|
+
} : Validator<string> & {
|
|
395
|
+
optional?: Opts['optional'];
|
|
396
|
+
default?: Opts['default'];
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* MySQL connection string validator
|
|
400
|
+
* Validates mysql:// URLs
|
|
401
|
+
*
|
|
402
|
+
* @param opts Validation options
|
|
403
|
+
* @returns MySQL URL validator
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```ts
|
|
407
|
+
* import { mysql } from 'node-env-resolver/validators';
|
|
408
|
+
*
|
|
409
|
+
* const config = resolve({
|
|
410
|
+
* MYSQL_URL: mysql(),
|
|
411
|
+
* READ_REPLICA_URL: mysql({ optional: true }),
|
|
412
|
+
* TEST_MYSQL_URL: mysql({ default: 'mysql://localhost:3306/test' })
|
|
413
|
+
* });
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
declare function mysql<Opts extends {
|
|
417
|
+
/** Default value if not provided */
|
|
418
|
+
default?: string;
|
|
419
|
+
/** Make the field optional */
|
|
420
|
+
optional?: boolean;
|
|
421
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
422
|
+
optional: true;
|
|
423
|
+
} ? Validator<string> & {
|
|
424
|
+
optional: true;
|
|
425
|
+
default?: Opts['default'];
|
|
426
|
+
} : Validator<string> & {
|
|
427
|
+
optional?: Opts['optional'];
|
|
428
|
+
default?: Opts['default'];
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* MongoDB connection string validator
|
|
432
|
+
* Validates mongodb:// and mongodb+srv:// URLs
|
|
433
|
+
*
|
|
434
|
+
* @param opts Validation options
|
|
435
|
+
* @returns MongoDB URL validator
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* ```ts
|
|
439
|
+
* import { mongodb } from 'node-env-resolver/validators';
|
|
440
|
+
*
|
|
441
|
+
* const config = resolve({
|
|
442
|
+
* MONGODB_URL: mongodb(),
|
|
443
|
+
* MONGODB_ATLAS_URL: mongodb({ optional: true }),
|
|
444
|
+
* TEST_MONGODB_URL: mongodb({ default: 'mongodb://localhost:27017/test' })
|
|
445
|
+
* });
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
declare function mongodb<Opts extends {
|
|
449
|
+
/** Default value if not provided */
|
|
450
|
+
default?: string;
|
|
451
|
+
/** Make the field optional */
|
|
452
|
+
optional?: boolean;
|
|
453
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
454
|
+
optional: true;
|
|
455
|
+
} ? Validator<string> & {
|
|
456
|
+
optional: true;
|
|
457
|
+
default?: Opts['default'];
|
|
458
|
+
} : Validator<string> & {
|
|
459
|
+
optional?: Opts['optional'];
|
|
460
|
+
default?: Opts['default'];
|
|
461
|
+
};
|
|
462
|
+
/**
|
|
463
|
+
* Redis connection string validator
|
|
464
|
+
* Validates redis:// and rediss:// URLs
|
|
465
|
+
*
|
|
466
|
+
* @param opts Validation options
|
|
467
|
+
* @returns Redis URL validator
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```ts
|
|
471
|
+
* import { redis } from 'node-env-resolver/validators';
|
|
472
|
+
*
|
|
473
|
+
* const config = resolve({
|
|
474
|
+
* REDIS_URL: redis(),
|
|
475
|
+
* REDIS_CACHE_URL: redis({ optional: true }),
|
|
476
|
+
* TEST_REDIS_URL: redis({ default: 'redis://localhost:6379/0' })
|
|
477
|
+
* });
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
declare function redis<Opts extends {
|
|
481
|
+
/** Default value if not provided */
|
|
482
|
+
default?: string;
|
|
483
|
+
/** Make the field optional */
|
|
484
|
+
optional?: boolean;
|
|
485
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
486
|
+
optional: true;
|
|
487
|
+
} ? Validator<string> & {
|
|
488
|
+
optional: true;
|
|
489
|
+
default?: Opts['default'];
|
|
490
|
+
} : Validator<string> & {
|
|
491
|
+
optional?: Opts['optional'];
|
|
492
|
+
default?: Opts['default'];
|
|
493
|
+
};
|
|
494
|
+
/**
|
|
495
|
+
* HTTPS-only URL validator
|
|
496
|
+
* Only accepts URLs starting with https://
|
|
497
|
+
*
|
|
498
|
+
* @param opts Validation options
|
|
499
|
+
* @returns HTTPS URL validator
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```ts
|
|
503
|
+
* import { https } from 'node-env-resolver/validators';
|
|
504
|
+
*
|
|
505
|
+
* const config = resolve({
|
|
506
|
+
* API_URL: https(),
|
|
507
|
+
* WEBHOOK_URL: https({ optional: true }),
|
|
508
|
+
* SECURE_URL: https({ default: 'https://api.example.com' })
|
|
509
|
+
* });
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
512
|
+
declare function https<Opts extends {
|
|
513
|
+
/** Default value if not provided */
|
|
514
|
+
default?: string;
|
|
515
|
+
/** Make the field optional */
|
|
516
|
+
optional?: boolean;
|
|
517
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
518
|
+
optional: true;
|
|
519
|
+
} ? Validator<string> & {
|
|
520
|
+
optional: true;
|
|
521
|
+
default?: Opts['default'];
|
|
522
|
+
} : Validator<string> & {
|
|
523
|
+
optional?: Opts['optional'];
|
|
524
|
+
default?: Opts['default'];
|
|
525
|
+
};
|
|
526
|
+
/**
|
|
527
|
+
* String array validator
|
|
528
|
+
* Splits comma-separated values into an array
|
|
529
|
+
*
|
|
530
|
+
* @param opts Validation options
|
|
531
|
+
* @returns String array validator
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* ```ts
|
|
535
|
+
* import { stringArray } from 'node-env-resolver/validators';
|
|
536
|
+
*
|
|
537
|
+
* const config = resolve({
|
|
538
|
+
* ALLOWED_ORIGINS: stringArray(), // "a.com,b.com,c.com"
|
|
539
|
+
* FEATURES: stringArray({ separator: ':' }), // "feature1:feature2"
|
|
540
|
+
* TAGS: stringArray({ optional: true })
|
|
541
|
+
* });
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
declare function stringArray<Opts extends {
|
|
545
|
+
/** Default value if not provided */
|
|
546
|
+
default?: string[];
|
|
547
|
+
/** Make the field optional */
|
|
548
|
+
optional?: boolean;
|
|
549
|
+
/** Separator character (default: ',') */
|
|
550
|
+
separator?: string;
|
|
551
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
552
|
+
optional: true;
|
|
553
|
+
} ? Validator<string[]> & {
|
|
554
|
+
optional: true;
|
|
555
|
+
default?: Opts['default'];
|
|
556
|
+
} : Validator<string[]> & {
|
|
557
|
+
optional?: Opts['optional'];
|
|
558
|
+
default?: Opts['default'];
|
|
559
|
+
};
|
|
560
|
+
/**
|
|
561
|
+
* Number array validator
|
|
562
|
+
* Splits comma-separated values into an array of numbers
|
|
563
|
+
*
|
|
564
|
+
* @param opts Validation options
|
|
565
|
+
* @returns Number array validator
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```ts
|
|
569
|
+
* import { numberArray } from 'node-env-resolver/validators';
|
|
570
|
+
*
|
|
571
|
+
* const config = resolve({
|
|
572
|
+
* PORTS: numberArray(), // "3000,3001,3002"
|
|
573
|
+
* TIMEOUTS: numberArray({ separator: ':' }), // "1000:2000:3000"
|
|
574
|
+
* RETRY_COUNTS: numberArray({ optional: true })
|
|
575
|
+
* });
|
|
576
|
+
* ```
|
|
577
|
+
*/
|
|
578
|
+
declare function numberArray<Opts extends {
|
|
579
|
+
/** Default value if not provided */
|
|
580
|
+
default?: number[];
|
|
581
|
+
/** Make the field optional */
|
|
582
|
+
optional?: boolean;
|
|
583
|
+
/** Separator character (default: ',') */
|
|
584
|
+
separator?: string;
|
|
585
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
586
|
+
optional: true;
|
|
587
|
+
} ? Validator<number[]> & {
|
|
588
|
+
optional: true;
|
|
589
|
+
default?: Opts['default'];
|
|
590
|
+
} : Validator<number[]> & {
|
|
591
|
+
optional?: Opts['optional'];
|
|
592
|
+
default?: Opts['default'];
|
|
593
|
+
};
|
|
594
|
+
/**
|
|
595
|
+
* URL array validator
|
|
596
|
+
* Splits comma-separated URLs into an array with URL validation
|
|
597
|
+
*
|
|
598
|
+
* @param opts Validation options
|
|
599
|
+
* @returns URL array validator
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* ```ts
|
|
603
|
+
* import { urlArray } from 'node-env-resolver/validators';
|
|
604
|
+
*
|
|
605
|
+
* const config = resolve({
|
|
606
|
+
* API_ENDPOINTS: urlArray(), // "https://api1.com,https://api2.com"
|
|
607
|
+
* WEBHOOK_URLS: urlArray({ separator: ':' }),
|
|
608
|
+
* BACKUP_URLS: urlArray({ optional: true })
|
|
609
|
+
* });
|
|
610
|
+
* ```
|
|
611
|
+
*/
|
|
612
|
+
declare function urlArray<Opts extends {
|
|
613
|
+
/** Default value if not provided */
|
|
614
|
+
default?: string[];
|
|
615
|
+
/** Make the field optional */
|
|
616
|
+
optional?: boolean;
|
|
617
|
+
/** Separator character (default: ',') */
|
|
618
|
+
separator?: string;
|
|
619
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
620
|
+
optional: true;
|
|
621
|
+
} ? Validator<string[]> & {
|
|
622
|
+
optional: true;
|
|
623
|
+
default?: Opts['default'];
|
|
624
|
+
} : Validator<string[]> & {
|
|
625
|
+
optional?: Opts['optional'];
|
|
626
|
+
default?: Opts['default'];
|
|
627
|
+
};
|
|
628
|
+
/**
|
|
629
|
+
* One-of validator for enum-like values
|
|
630
|
+
* Validates that the value is one of the provided options
|
|
631
|
+
*
|
|
632
|
+
* @param values Array of allowed values
|
|
633
|
+
* @param opts Validation options
|
|
634
|
+
* @returns One-of validator
|
|
635
|
+
*
|
|
636
|
+
* @example
|
|
637
|
+
* ```ts
|
|
638
|
+
* import { oneOf } from 'node-env-resolver/validators';
|
|
639
|
+
*
|
|
640
|
+
* const config = resolve({
|
|
641
|
+
* NODE_ENV: oneOf(['development', 'production', 'test'] as const),
|
|
642
|
+
* LOG_LEVEL: oneOf(['debug', 'info', 'warn', 'error'] as const),
|
|
643
|
+
* MODE: oneOf(['dev', 'prod'] as const, { optional: true })
|
|
644
|
+
* });
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
647
|
+
declare function oneOf<T extends readonly (string | number)[], Opts extends {
|
|
648
|
+
/** Default value if not provided */
|
|
649
|
+
default?: T[number];
|
|
650
|
+
/** Make the field optional */
|
|
651
|
+
optional?: boolean;
|
|
652
|
+
} = Record<string, never>>(values: T, opts?: Opts): Opts extends {
|
|
653
|
+
optional: true;
|
|
654
|
+
} ? Validator<T[number]> & {
|
|
655
|
+
optional: true;
|
|
656
|
+
default?: Opts['default'];
|
|
657
|
+
} : Validator<T[number]> & {
|
|
658
|
+
optional?: Opts['optional'];
|
|
659
|
+
default?: Opts['default'];
|
|
660
|
+
};
|
|
661
|
+
/**
|
|
662
|
+
* Duration validator with time unit parsing
|
|
663
|
+
* Converts duration strings to milliseconds
|
|
664
|
+
* Supports: s (seconds), m (minutes), h (hours), d (days)
|
|
665
|
+
*
|
|
666
|
+
* @param opts Validation options
|
|
667
|
+
* @returns Duration validator
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```ts
|
|
671
|
+
* import { duration } from 'node-env-resolver/validators';
|
|
672
|
+
*
|
|
673
|
+
* const config = resolve({
|
|
674
|
+
* CACHE_TTL: duration(), // "5m" → 300000ms
|
|
675
|
+
* TIMEOUT: duration({ default: 30000 }), // "30s" → 30000ms
|
|
676
|
+
* RETRY_DELAY: duration({ optional: true }) // "2h" → 7200000ms
|
|
677
|
+
* });
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
declare function duration<Opts extends {
|
|
681
|
+
/** Default value if not provided */
|
|
682
|
+
default?: number;
|
|
683
|
+
/** Make the field optional */
|
|
684
|
+
optional?: boolean;
|
|
685
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
686
|
+
optional: true;
|
|
687
|
+
} ? Validator<number> & {
|
|
688
|
+
optional: true;
|
|
689
|
+
default?: Opts['default'];
|
|
690
|
+
} : Validator<number> & {
|
|
691
|
+
optional?: Opts['optional'];
|
|
692
|
+
default?: Opts['default'];
|
|
693
|
+
};
|
|
694
|
+
/**
|
|
695
|
+
* File content validator
|
|
696
|
+
* Reads the content of a file from the filesystem
|
|
697
|
+
*
|
|
698
|
+
* @param opts Validation options
|
|
699
|
+
* @returns File content validator
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
* ```ts
|
|
703
|
+
* import { file } from 'node-env-resolver/validators';
|
|
704
|
+
*
|
|
705
|
+
* const config = resolve({
|
|
706
|
+
* SSL_KEY: file(), // Reads content of SSL_KEY file path
|
|
707
|
+
* CONFIG_FILE: file({ optional: true }),
|
|
708
|
+
* DEFAULT_CERT: file({ default: './certs/default.pem' })
|
|
709
|
+
* });
|
|
710
|
+
* ```
|
|
711
|
+
*/
|
|
712
|
+
declare function file<Opts extends {
|
|
713
|
+
/** Default value if not provided */
|
|
714
|
+
default?: string;
|
|
715
|
+
/** Make the field optional */
|
|
716
|
+
optional?: boolean;
|
|
717
|
+
/** Base directory for secrets (Docker/K8s) */
|
|
718
|
+
secretsDir?: string;
|
|
719
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
720
|
+
optional: true;
|
|
721
|
+
} ? Validator<string> & {
|
|
722
|
+
optional: true;
|
|
723
|
+
default?: Opts['default'];
|
|
724
|
+
secretsDir?: Opts['secretsDir'];
|
|
725
|
+
} : Validator<string> & {
|
|
726
|
+
optional?: Opts['optional'];
|
|
727
|
+
default?: Opts['default'];
|
|
728
|
+
secretsDir?: Opts['secretsDir'];
|
|
729
|
+
};
|
|
730
|
+
/**
|
|
731
|
+
* Secret value validator
|
|
732
|
+
* A generic validator for secret values with no specific validation
|
|
733
|
+
*
|
|
734
|
+
* @param opts Validation options
|
|
735
|
+
* @returns Secret validator
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```ts
|
|
739
|
+
* import { secret } from 'node-env-resolver/validators';
|
|
740
|
+
*
|
|
741
|
+
* const config = resolve({
|
|
742
|
+
* API_SECRET: secret(),
|
|
743
|
+
* ENCRYPTION_KEY: secret({ optional: true }),
|
|
744
|
+
* JWT_SECRET: secret({ default: 'fallback-secret' })
|
|
745
|
+
* });
|
|
746
|
+
* ```
|
|
747
|
+
*/
|
|
748
|
+
declare function secret<Opts extends {
|
|
749
|
+
/** Default value if not provided */
|
|
750
|
+
default?: string;
|
|
751
|
+
/** Make the field optional */
|
|
752
|
+
optional?: boolean;
|
|
753
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
754
|
+
optional: true;
|
|
755
|
+
} ? Validator<string> & {
|
|
756
|
+
optional: true;
|
|
757
|
+
default?: Opts['default'];
|
|
758
|
+
} : Validator<string> & {
|
|
759
|
+
optional?: Opts['optional'];
|
|
760
|
+
default?: Opts['default'];
|
|
761
|
+
};
|
|
762
|
+
/**
|
|
763
|
+
* Custom validator function
|
|
764
|
+
* Allows you to define your own validation logic
|
|
765
|
+
*
|
|
766
|
+
* @param validator Custom validation function
|
|
767
|
+
* @param opts Validation options
|
|
768
|
+
* @returns Custom validator
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* ```ts
|
|
772
|
+
* import { custom } from 'node-env-resolver/validators';
|
|
773
|
+
*
|
|
774
|
+
* const config = resolve({
|
|
775
|
+
* CUSTOM_PORT: custom((value) => {
|
|
776
|
+
* const port = parseInt(value, 10);
|
|
777
|
+
* if (port < 1000 || port > 9999) {
|
|
778
|
+
* throw new Error('Port must be between 1000-9999');
|
|
779
|
+
* }
|
|
780
|
+
* return port;
|
|
781
|
+
* }),
|
|
782
|
+
* API_VERSION: custom((value) => {
|
|
783
|
+
* if (!value.match(/^v\d+\.\d+$/)) {
|
|
784
|
+
* throw new Error('Invalid API version format');
|
|
785
|
+
* }
|
|
786
|
+
* return value;
|
|
787
|
+
* }, { optional: true })
|
|
788
|
+
* });
|
|
789
|
+
* ```
|
|
790
|
+
*/
|
|
791
|
+
declare function custom<T = unknown, Opts extends {
|
|
792
|
+
/** Default value if not provided */
|
|
793
|
+
default?: T;
|
|
794
|
+
/** Make the field optional */
|
|
795
|
+
optional?: boolean;
|
|
796
|
+
} = Record<string, never>>(validator: (value: string) => T, opts?: Opts): Opts extends {
|
|
797
|
+
optional: true;
|
|
798
|
+
} ? Validator<T> & {
|
|
799
|
+
optional: true;
|
|
800
|
+
default?: Opts['default'];
|
|
801
|
+
} : Validator<T> & {
|
|
802
|
+
optional?: Opts['optional'];
|
|
803
|
+
default?: Opts['default'];
|
|
804
|
+
};
|
|
805
|
+
/**
|
|
806
|
+
* ISO 8601 date validator
|
|
807
|
+
* Validates dates in YYYY-MM-DD or YYYY-MM-DDTHH:mm:ssZ format
|
|
808
|
+
*
|
|
809
|
+
* @param opts Validation options
|
|
810
|
+
* @returns Date validator
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```ts
|
|
814
|
+
* import { date } from 'node-env-resolver/validators';
|
|
815
|
+
*
|
|
816
|
+
* const config = resolve({
|
|
817
|
+
* START_DATE: date(), // "2024-01-01" or "2024-01-01T12:00:00Z"
|
|
818
|
+
* END_DATE: date({ optional: true }),
|
|
819
|
+
* DEFAULT_DATE: date({ default: '2024-01-01' })
|
|
820
|
+
* });
|
|
821
|
+
* ```
|
|
822
|
+
*/
|
|
823
|
+
declare function date<Opts extends {
|
|
824
|
+
/** Default value if not provided */
|
|
825
|
+
default?: string;
|
|
826
|
+
/** Make the field optional */
|
|
827
|
+
optional?: boolean;
|
|
828
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
829
|
+
optional: true;
|
|
830
|
+
} ? Validator<string> & {
|
|
831
|
+
optional: true;
|
|
832
|
+
default?: Opts['default'];
|
|
833
|
+
} : Validator<string> & {
|
|
834
|
+
optional?: Opts['optional'];
|
|
835
|
+
default?: Opts['default'];
|
|
836
|
+
};
|
|
837
|
+
/**
|
|
838
|
+
* Unix timestamp validator
|
|
839
|
+
* Validates positive integers representing Unix timestamps
|
|
840
|
+
*
|
|
841
|
+
* @param opts Validation options
|
|
842
|
+
* @returns Timestamp validator
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* ```ts
|
|
846
|
+
* import { timestamp } from 'node-env-resolver/validators';
|
|
847
|
+
*
|
|
848
|
+
* const config = resolve({
|
|
849
|
+
* CREATED_AT: timestamp(), // "1640995200"
|
|
850
|
+
* UPDATED_AT: timestamp({ optional: true }),
|
|
851
|
+
* EXPIRES_AT: timestamp({ default: 1640995200 })
|
|
852
|
+
* });
|
|
853
|
+
* ```
|
|
854
|
+
*/
|
|
855
|
+
declare function timestamp<Opts extends {
|
|
856
|
+
/** Default value if not provided */
|
|
857
|
+
default?: number;
|
|
858
|
+
/** Make the field optional */
|
|
859
|
+
optional?: boolean;
|
|
860
|
+
} = Record<string, never>>(opts?: Opts): Opts extends {
|
|
861
|
+
optional: true;
|
|
862
|
+
} ? Validator<number> & {
|
|
863
|
+
optional: true;
|
|
864
|
+
default?: Opts['default'];
|
|
865
|
+
} : Validator<number> & {
|
|
866
|
+
optional?: Opts['optional'];
|
|
867
|
+
default?: Opts['default'];
|
|
868
|
+
};
|
|
115
869
|
|
|
116
|
-
export { type ValidationResult,
|
|
870
|
+
export { type ValidationResult, boolean, custom, date, duration, email, file, http, https, json, mongodb, mysql, number, numberArray, oneOf, port, postgres, redis, secret, string, stringArray, timestamp, url, urlArray, validateBoolean, validateDate, validateDuration, validateEmail, validateFile, validateMongodb, validateMysql, validateNumber, validatePort, validatePostgres, validateRedis, validateTimestamp, validateUrl };
|