@meng-xi/vite-plugin 0.1.3 → 0.1.4

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 (75) hide show
  1. package/README-en.md +48 -774
  2. package/README.md +45 -769
  3. package/dist/common/html/index.cjs +2 -2
  4. package/dist/common/html/index.d.cts +268 -19
  5. package/dist/common/html/index.d.mts +268 -19
  6. package/dist/common/html/index.d.ts +268 -19
  7. package/dist/common/html/index.mjs +2 -2
  8. package/dist/common/index.cjs +1 -1
  9. package/dist/common/index.d.cts +4 -1
  10. package/dist/common/index.d.mts +4 -1
  11. package/dist/common/index.d.ts +4 -1
  12. package/dist/common/index.mjs +1 -1
  13. package/dist/common/ui/index.cjs +1 -0
  14. package/dist/common/ui/index.d.cts +132 -0
  15. package/dist/common/ui/index.d.mts +132 -0
  16. package/dist/common/ui/index.d.ts +132 -0
  17. package/dist/common/ui/index.mjs +1 -0
  18. package/dist/common/validation/index.cjs +1 -1
  19. package/dist/common/validation/index.d.cts +1 -0
  20. package/dist/common/validation/index.d.mts +1 -0
  21. package/dist/common/validation/index.d.ts +1 -0
  22. package/dist/common/validation/index.mjs +1 -1
  23. package/dist/index.cjs +1 -1
  24. package/dist/index.d.cts +6 -2
  25. package/dist/index.d.mts +6 -2
  26. package/dist/index.d.ts +6 -2
  27. package/dist/index.mjs +1 -1
  28. package/dist/plugins/buildProgress/index.cjs +2 -2
  29. package/dist/plugins/buildProgress/index.mjs +2 -2
  30. package/dist/plugins/copyFile/index.d.cts +20 -2
  31. package/dist/plugins/copyFile/index.d.mts +20 -2
  32. package/dist/plugins/copyFile/index.d.ts +20 -2
  33. package/dist/plugins/envGuard/index.cjs +67 -0
  34. package/dist/plugins/envGuard/index.d.cts +156 -0
  35. package/dist/plugins/envGuard/index.d.mts +156 -0
  36. package/dist/plugins/envGuard/index.d.ts +156 -0
  37. package/dist/plugins/envGuard/index.mjs +67 -0
  38. package/dist/plugins/faviconManager/index.cjs +1 -1
  39. package/dist/plugins/faviconManager/index.d.cts +43 -5
  40. package/dist/plugins/faviconManager/index.d.mts +43 -5
  41. package/dist/plugins/faviconManager/index.d.ts +43 -5
  42. package/dist/plugins/faviconManager/index.mjs +1 -1
  43. package/dist/plugins/generateRouter/index.d.cts +61 -14
  44. package/dist/plugins/generateRouter/index.d.mts +61 -14
  45. package/dist/plugins/generateRouter/index.d.ts +61 -14
  46. package/dist/plugins/generateVersion/index.d.cts +12 -0
  47. package/dist/plugins/generateVersion/index.d.mts +12 -0
  48. package/dist/plugins/generateVersion/index.d.ts +12 -0
  49. package/dist/plugins/htmlInject/index.cjs +1 -7
  50. package/dist/plugins/htmlInject/index.d.cts +49 -194
  51. package/dist/plugins/htmlInject/index.d.mts +49 -194
  52. package/dist/plugins/htmlInject/index.d.ts +49 -194
  53. package/dist/plugins/htmlInject/index.mjs +1 -7
  54. package/dist/plugins/index.cjs +1 -1
  55. package/dist/plugins/index.d.cts +4 -1
  56. package/dist/plugins/index.d.mts +4 -1
  57. package/dist/plugins/index.d.ts +4 -1
  58. package/dist/plugins/index.mjs +1 -1
  59. package/dist/plugins/loadingManager/index.cjs +2 -2
  60. package/dist/plugins/loadingManager/index.mjs +1 -1
  61. package/dist/plugins/versionUpdateChecker/index.cjs +2 -2
  62. package/dist/plugins/versionUpdateChecker/index.mjs +3 -3
  63. package/dist/shared/vite-plugin.BCuhU1au.mjs +7 -0
  64. package/dist/shared/vite-plugin.BrI73DHA.cjs +7 -0
  65. package/dist/shared/vite-plugin.CmtcnItg.d.cts +261 -0
  66. package/dist/shared/vite-plugin.CmtcnItg.d.mts +261 -0
  67. package/dist/shared/vite-plugin.CmtcnItg.d.ts +261 -0
  68. package/dist/shared/vite-plugin.DnFDPjNf.mjs +1 -0
  69. package/dist/shared/vite-plugin.Dumot0up.mjs +1 -0
  70. package/dist/shared/vite-plugin.FfJ-Wwfu.d.cts +143 -0
  71. package/dist/shared/vite-plugin.FfJ-Wwfu.d.mts +143 -0
  72. package/dist/shared/vite-plugin.FfJ-Wwfu.d.ts +143 -0
  73. package/dist/shared/vite-plugin.soT9a-KD.cjs +1 -0
  74. package/dist/shared/vite-plugin.vwox4bU0.cjs +1 -0
  75. package/package.json +11 -1
@@ -0,0 +1,261 @@
1
+ /**
2
+ * 环境变量验证模块
3
+ *
4
+ * @module common/validation/env
5
+ * @description 提供环境变量的类型校验、范围验证、长度验证和批量验证等功能,
6
+ * 支持 string、number、url、boolean、enum、json、semver、path 等多种类型,
7
+ * 为 envGuard 等插件提供统一的环境变量校验能力。
8
+ */
9
+ /**
10
+ * 环境变量值类型
11
+ *
12
+ * @description 定义环境变量支持的所有值类型:
13
+ * - `string`:普通字符串(默认类型)
14
+ * - `number`:数字类型
15
+ * - `url`:URL 地址
16
+ * - `boolean`:布尔值
17
+ * - `enum`:枚举值
18
+ * - `json`:JSON 字符串
19
+ * - `semver`:语义化版本号
20
+ * - `path`:文件路径
21
+ */
22
+ type EnvType = 'string' | 'number' | 'url' | 'boolean' | 'enum' | 'json' | 'semver' | 'path';
23
+ /**
24
+ * 环境变量字段校验规则
25
+ *
26
+ * @description 定义单个环境变量的校验规则,包括类型约束、范围限制、
27
+ * 自定义验证函数和元数据信息等。
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // 基本字符串字段
32
+ * const rule: EnvFieldRule = { type: 'string', required: true }
33
+ *
34
+ * // 带范围的数字字段
35
+ * const portRule: EnvFieldRule = { type: 'number', required: true, minValue: 1, maxValue: 65535 }
36
+ *
37
+ * // 枚举字段
38
+ * const envRule: EnvFieldRule = { type: 'enum', required: true, enumValues: ['development', 'staging', 'production'] }
39
+ *
40
+ * // 自定义验证
41
+ * const customRule: EnvFieldRule = {
42
+ * type: 'string',
43
+ * required: true,
44
+ * validator: (v) => v.startsWith('https://') || '必须使用 HTTPS'
45
+ * }
46
+ * ```
47
+ */
48
+ interface EnvFieldRule {
49
+ /** 值类型,默认为 `'string'` */
50
+ type?: EnvType;
51
+ /** 是否为必需字段,默认为 `true` */
52
+ required?: boolean;
53
+ /** 正则表达式,值必须匹配此模式 */
54
+ pattern?: RegExp;
55
+ /**
56
+ * 自定义验证函数
57
+ *
58
+ * @description 返回 `true` 表示验证通过,返回字符串表示验证失败且字符串为错误消息
59
+ */
60
+ validator?: (value: string) => boolean | string;
61
+ /** 自定义错误消息,覆盖默认的错误提示 */
62
+ message?: string;
63
+ /** 当值为空时使用的默认值 */
64
+ default?: string;
65
+ /** 枚举值列表(仅 `enum` 类型需要) */
66
+ enumValues?: string[];
67
+ /** 数值最小值(仅 `number` 类型) */
68
+ minValue?: number;
69
+ /** 数值最大值(仅 `number` 类型) */
70
+ maxValue?: number;
71
+ /** 字符串最小长度(仅字符串类类型) */
72
+ minLength?: number;
73
+ /** 字符串最大长度(仅字符串类类型) */
74
+ maxLength?: number;
75
+ /** 变量分组名称,用于模板生成时的分组显示 */
76
+ group?: string;
77
+ /** 变量描述信息,用于模板生成时的说明文本 */
78
+ description?: string;
79
+ /** 是否为敏感变量,模板中会隐藏实际值 */
80
+ sensitive?: boolean;
81
+ }
82
+ /**
83
+ * 环境变量验证结果
84
+ *
85
+ * @description 表示单个环境变量的验证结果,包含验证状态、错误消息和有效值。
86
+ */
87
+ interface EnvValidationResult {
88
+ /** 环境变量名 */
89
+ key: string;
90
+ /**
91
+ * 验证状态:
92
+ * - `pass`:验证通过
93
+ * - `missing`:必需变量缺失
94
+ * - `type_error`:类型不匹配
95
+ * - `custom_error`:自定义验证失败或正则不匹配
96
+ * - `enum_mismatch`:枚举值不匹配
97
+ * - `range_error`:数值超出范围
98
+ * - `length_error`:字符串长度不符合要求
99
+ */
100
+ status: 'pass' | 'missing' | 'type_error' | 'custom_error' | 'enum_mismatch' | 'range_error' | 'length_error';
101
+ /** 验证消息(验证通过时为空字符串,失败时为错误描述) */
102
+ message: string;
103
+ /** 环境变量的有效值(缺失时可能为 `undefined` 或默认值) */
104
+ value: string | undefined;
105
+ /** 应用的校验规则(含默认值填充后的完整规则) */
106
+ rule: EnvFieldRule;
107
+ }
108
+ /**
109
+ * 字符串类类型集合
110
+ *
111
+ * @description 包含所有以字符串形式存储的值类型(以及未指定类型的默认情况),
112
+ * 用于在长度验证时判断是否需要检查 `minLength`/`maxLength`。
113
+ * 非字符串类类型(如 `number`、`boolean`、`json`)不进行长度验证。
114
+ */
115
+ declare const STRING_LIKE_TYPES: ReadonlySet<EnvType | undefined>;
116
+ /**
117
+ * 验证环境变量值的类型
118
+ *
119
+ * @param value - 环境变量值的字符串表示
120
+ * @param rule - 校验规则(需包含 `type` 字段)
121
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
122
+ *
123
+ * @description 根据规则中指定的类型对值进行类型校验:
124
+ * - `string`:始终通过(默认类型)
125
+ * - `number`:必须为合法数字
126
+ * - `url`:必须能被 `URL` 构造函数解析
127
+ * - `boolean`:必须为 `true`/`false`/`1`/`0`/`yes`/`no`(不区分大小写)
128
+ * - `enum`:必须在 `enumValues` 列表中
129
+ * - `json`:必须能被 `JSON.parse` 解析
130
+ * - `semver`:必须匹配 `x.y.z` 格式(支持 prerelease 和 build metadata)
131
+ * - `path`:必须以 `./`、`/`、`\\` 或盘符开头
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * validateType('123', { type: 'number' }) // { valid: true, status: 'pass', message: '' }
136
+ * validateType('abc', { type: 'number' }) // { valid: false, status: 'type_error', message: '...' }
137
+ * validateType('d', { type: 'enum', enumValues: ['a', 'b', 'c'] })
138
+ * // { valid: false, status: 'enum_mismatch', message: '...' }
139
+ * ```
140
+ */
141
+ declare function validateType(value: string, rule: EnvFieldRule): {
142
+ valid: boolean;
143
+ status: 'type_error' | 'enum_mismatch' | 'pass';
144
+ message: string;
145
+ };
146
+ /**
147
+ * 验证数值范围
148
+ *
149
+ * @param value - 环境变量值的字符串表示
150
+ * @param rule - 校验规则(需包含 `minValue`/`maxValue` 字段)
151
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
152
+ *
153
+ * @description 仅对 `number` 类型进行范围验证,检查值是否在 `minValue` 和 `maxValue` 之间。
154
+ * 非 `number` 类型始终返回验证通过。
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * validateRange('5', { type: 'number', minValue: 1, maxValue: 10 })
159
+ * // { valid: true, status: 'pass', message: '' }
160
+ *
161
+ * validateRange('0', { type: 'number', minValue: 1 })
162
+ * // { valid: false, status: 'range_error', message: '数值 0 小于最小值 1' }
163
+ * ```
164
+ */
165
+ declare function validateRange(value: string, rule: EnvFieldRule): {
166
+ valid: boolean;
167
+ status: 'range_error' | 'pass';
168
+ message: string;
169
+ };
170
+ /**
171
+ * 验证字符串长度
172
+ *
173
+ * @param value - 环境变量值
174
+ * @param rule - 校验规则(需包含 `minLength`/`maxLength` 字段)
175
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
176
+ *
177
+ * @description 仅对字符串类类型(`string`、`url`、`path`、`enum`、`semver` 及未指定类型)
178
+ * 进行长度验证。非字符串类类型(如 `number`、`boolean`、`json`)始终返回验证通过。
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * validateLength('hello', { type: 'string', minLength: 1, maxLength: 10 })
183
+ * // { valid: true, status: 'pass', message: '' }
184
+ *
185
+ * validateLength('hi', { type: 'string', minLength: 5 })
186
+ * // { valid: false, status: 'length_error', message: '字符串长度 2 小于最小长度 5' }
187
+ * ```
188
+ */
189
+ declare function validateLength(value: string, rule: EnvFieldRule): {
190
+ valid: boolean;
191
+ status: 'length_error' | 'pass';
192
+ message: string;
193
+ };
194
+ /**
195
+ * 验证单个环境变量值
196
+ *
197
+ * @param key - 环境变量名
198
+ * @param value - 环境变量值(可能为 `undefined`)
199
+ * @param rule - 校验规则
200
+ * @returns 验证结果对象
201
+ *
202
+ * @description 按顺序执行以下验证步骤:
203
+ * 1. **缺失检查**:必需变量为空时返回 `missing` 状态,非必需变量为空时使用默认值
204
+ * 2. **类型验证**:调用 `validateType` 检查值的类型
205
+ * 3. **范围验证**:调用 `validateRange` 检查数值范围
206
+ * 4. **长度验证**:调用 `validateLength` 检查字符串长度
207
+ * 5. **正则验证**:检查值是否匹配 `pattern` 正则
208
+ * 6. **自定义验证**:执行 `validator` 函数
209
+ *
210
+ * 所有验证步骤中,自定义 `message` 会覆盖默认的错误消息。
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * // 必需变量缺失
215
+ * validateValue('API_KEY', undefined, { type: 'string', required: true })
216
+ * // { key: 'API_KEY', status: 'missing', message: '缺少必需的环境变量: API_KEY', ... }
217
+ *
218
+ * // 类型错误
219
+ * validateValue('PORT', 'abc', { type: 'number', required: true })
220
+ * // { key: 'PORT', status: 'type_error', message: '...', ... }
221
+ *
222
+ * // 使用默认值
223
+ * validateValue('DEBUG', undefined, { type: 'boolean', required: false, default: 'false' })
224
+ * // { key: 'DEBUG', status: 'pass', value: 'false', ... }
225
+ * ```
226
+ */
227
+ declare function validateValue(key: string, value: string | undefined, rule: EnvFieldRule): EnvValidationResult;
228
+ /**
229
+ * 批量验证环境变量
230
+ *
231
+ * @param env - 环境变量键值对对象
232
+ * @param rules - 环境变量校验规则映射
233
+ * @returns 所有变量的验证结果数组
234
+ *
235
+ * @description 遍历所有规则,对每个环境变量调用 `validateValue` 进行验证,
236
+ * 返回包含所有验证结果的数组。可用于汇总展示所有验证错误。
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * const results = validateEnvironment(
241
+ * { API_URL: 'https://api.example.com', PORT: 'abc', MISSING_VAR: undefined },
242
+ * {
243
+ * API_URL: { type: 'url', required: true },
244
+ * PORT: { type: 'number', required: true },
245
+ * MISSING_VAR: { type: 'string', required: true }
246
+ * }
247
+ * )
248
+ * // [
249
+ * // { key: 'API_URL', status: 'pass', ... },
250
+ * // { key: 'PORT', status: 'type_error', ... },
251
+ * // { key: 'MISSING_VAR', status: 'missing', ... }
252
+ * // ]
253
+ *
254
+ * // 检查是否有错误
255
+ * const hasErrors = results.some(r => r.status !== 'pass')
256
+ * ```
257
+ */
258
+ declare function validateEnvironment(env: Record<string, string | undefined>, rules: Record<string, EnvFieldRule>): EnvValidationResult[];
259
+
260
+ export { STRING_LIKE_TYPES as S, validateLength as c, validateRange as d, validateType as e, validateValue as f, validateEnvironment as v };
261
+ export type { EnvFieldRule as E, EnvType as a, EnvValidationResult as b };
@@ -0,0 +1,261 @@
1
+ /**
2
+ * 环境变量验证模块
3
+ *
4
+ * @module common/validation/env
5
+ * @description 提供环境变量的类型校验、范围验证、长度验证和批量验证等功能,
6
+ * 支持 string、number、url、boolean、enum、json、semver、path 等多种类型,
7
+ * 为 envGuard 等插件提供统一的环境变量校验能力。
8
+ */
9
+ /**
10
+ * 环境变量值类型
11
+ *
12
+ * @description 定义环境变量支持的所有值类型:
13
+ * - `string`:普通字符串(默认类型)
14
+ * - `number`:数字类型
15
+ * - `url`:URL 地址
16
+ * - `boolean`:布尔值
17
+ * - `enum`:枚举值
18
+ * - `json`:JSON 字符串
19
+ * - `semver`:语义化版本号
20
+ * - `path`:文件路径
21
+ */
22
+ type EnvType = 'string' | 'number' | 'url' | 'boolean' | 'enum' | 'json' | 'semver' | 'path';
23
+ /**
24
+ * 环境变量字段校验规则
25
+ *
26
+ * @description 定义单个环境变量的校验规则,包括类型约束、范围限制、
27
+ * 自定义验证函数和元数据信息等。
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * // 基本字符串字段
32
+ * const rule: EnvFieldRule = { type: 'string', required: true }
33
+ *
34
+ * // 带范围的数字字段
35
+ * const portRule: EnvFieldRule = { type: 'number', required: true, minValue: 1, maxValue: 65535 }
36
+ *
37
+ * // 枚举字段
38
+ * const envRule: EnvFieldRule = { type: 'enum', required: true, enumValues: ['development', 'staging', 'production'] }
39
+ *
40
+ * // 自定义验证
41
+ * const customRule: EnvFieldRule = {
42
+ * type: 'string',
43
+ * required: true,
44
+ * validator: (v) => v.startsWith('https://') || '必须使用 HTTPS'
45
+ * }
46
+ * ```
47
+ */
48
+ interface EnvFieldRule {
49
+ /** 值类型,默认为 `'string'` */
50
+ type?: EnvType;
51
+ /** 是否为必需字段,默认为 `true` */
52
+ required?: boolean;
53
+ /** 正则表达式,值必须匹配此模式 */
54
+ pattern?: RegExp;
55
+ /**
56
+ * 自定义验证函数
57
+ *
58
+ * @description 返回 `true` 表示验证通过,返回字符串表示验证失败且字符串为错误消息
59
+ */
60
+ validator?: (value: string) => boolean | string;
61
+ /** 自定义错误消息,覆盖默认的错误提示 */
62
+ message?: string;
63
+ /** 当值为空时使用的默认值 */
64
+ default?: string;
65
+ /** 枚举值列表(仅 `enum` 类型需要) */
66
+ enumValues?: string[];
67
+ /** 数值最小值(仅 `number` 类型) */
68
+ minValue?: number;
69
+ /** 数值最大值(仅 `number` 类型) */
70
+ maxValue?: number;
71
+ /** 字符串最小长度(仅字符串类类型) */
72
+ minLength?: number;
73
+ /** 字符串最大长度(仅字符串类类型) */
74
+ maxLength?: number;
75
+ /** 变量分组名称,用于模板生成时的分组显示 */
76
+ group?: string;
77
+ /** 变量描述信息,用于模板生成时的说明文本 */
78
+ description?: string;
79
+ /** 是否为敏感变量,模板中会隐藏实际值 */
80
+ sensitive?: boolean;
81
+ }
82
+ /**
83
+ * 环境变量验证结果
84
+ *
85
+ * @description 表示单个环境变量的验证结果,包含验证状态、错误消息和有效值。
86
+ */
87
+ interface EnvValidationResult {
88
+ /** 环境变量名 */
89
+ key: string;
90
+ /**
91
+ * 验证状态:
92
+ * - `pass`:验证通过
93
+ * - `missing`:必需变量缺失
94
+ * - `type_error`:类型不匹配
95
+ * - `custom_error`:自定义验证失败或正则不匹配
96
+ * - `enum_mismatch`:枚举值不匹配
97
+ * - `range_error`:数值超出范围
98
+ * - `length_error`:字符串长度不符合要求
99
+ */
100
+ status: 'pass' | 'missing' | 'type_error' | 'custom_error' | 'enum_mismatch' | 'range_error' | 'length_error';
101
+ /** 验证消息(验证通过时为空字符串,失败时为错误描述) */
102
+ message: string;
103
+ /** 环境变量的有效值(缺失时可能为 `undefined` 或默认值) */
104
+ value: string | undefined;
105
+ /** 应用的校验规则(含默认值填充后的完整规则) */
106
+ rule: EnvFieldRule;
107
+ }
108
+ /**
109
+ * 字符串类类型集合
110
+ *
111
+ * @description 包含所有以字符串形式存储的值类型(以及未指定类型的默认情况),
112
+ * 用于在长度验证时判断是否需要检查 `minLength`/`maxLength`。
113
+ * 非字符串类类型(如 `number`、`boolean`、`json`)不进行长度验证。
114
+ */
115
+ declare const STRING_LIKE_TYPES: ReadonlySet<EnvType | undefined>;
116
+ /**
117
+ * 验证环境变量值的类型
118
+ *
119
+ * @param value - 环境变量值的字符串表示
120
+ * @param rule - 校验规则(需包含 `type` 字段)
121
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
122
+ *
123
+ * @description 根据规则中指定的类型对值进行类型校验:
124
+ * - `string`:始终通过(默认类型)
125
+ * - `number`:必须为合法数字
126
+ * - `url`:必须能被 `URL` 构造函数解析
127
+ * - `boolean`:必须为 `true`/`false`/`1`/`0`/`yes`/`no`(不区分大小写)
128
+ * - `enum`:必须在 `enumValues` 列表中
129
+ * - `json`:必须能被 `JSON.parse` 解析
130
+ * - `semver`:必须匹配 `x.y.z` 格式(支持 prerelease 和 build metadata)
131
+ * - `path`:必须以 `./`、`/`、`\\` 或盘符开头
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * validateType('123', { type: 'number' }) // { valid: true, status: 'pass', message: '' }
136
+ * validateType('abc', { type: 'number' }) // { valid: false, status: 'type_error', message: '...' }
137
+ * validateType('d', { type: 'enum', enumValues: ['a', 'b', 'c'] })
138
+ * // { valid: false, status: 'enum_mismatch', message: '...' }
139
+ * ```
140
+ */
141
+ declare function validateType(value: string, rule: EnvFieldRule): {
142
+ valid: boolean;
143
+ status: 'type_error' | 'enum_mismatch' | 'pass';
144
+ message: string;
145
+ };
146
+ /**
147
+ * 验证数值范围
148
+ *
149
+ * @param value - 环境变量值的字符串表示
150
+ * @param rule - 校验规则(需包含 `minValue`/`maxValue` 字段)
151
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
152
+ *
153
+ * @description 仅对 `number` 类型进行范围验证,检查值是否在 `minValue` 和 `maxValue` 之间。
154
+ * 非 `number` 类型始终返回验证通过。
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * validateRange('5', { type: 'number', minValue: 1, maxValue: 10 })
159
+ * // { valid: true, status: 'pass', message: '' }
160
+ *
161
+ * validateRange('0', { type: 'number', minValue: 1 })
162
+ * // { valid: false, status: 'range_error', message: '数值 0 小于最小值 1' }
163
+ * ```
164
+ */
165
+ declare function validateRange(value: string, rule: EnvFieldRule): {
166
+ valid: boolean;
167
+ status: 'range_error' | 'pass';
168
+ message: string;
169
+ };
170
+ /**
171
+ * 验证字符串长度
172
+ *
173
+ * @param value - 环境变量值
174
+ * @param rule - 校验规则(需包含 `minLength`/`maxLength` 字段)
175
+ * @returns 验证结果对象,包含是否合法、状态码和错误消息
176
+ *
177
+ * @description 仅对字符串类类型(`string`、`url`、`path`、`enum`、`semver` 及未指定类型)
178
+ * 进行长度验证。非字符串类类型(如 `number`、`boolean`、`json`)始终返回验证通过。
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * validateLength('hello', { type: 'string', minLength: 1, maxLength: 10 })
183
+ * // { valid: true, status: 'pass', message: '' }
184
+ *
185
+ * validateLength('hi', { type: 'string', minLength: 5 })
186
+ * // { valid: false, status: 'length_error', message: '字符串长度 2 小于最小长度 5' }
187
+ * ```
188
+ */
189
+ declare function validateLength(value: string, rule: EnvFieldRule): {
190
+ valid: boolean;
191
+ status: 'length_error' | 'pass';
192
+ message: string;
193
+ };
194
+ /**
195
+ * 验证单个环境变量值
196
+ *
197
+ * @param key - 环境变量名
198
+ * @param value - 环境变量值(可能为 `undefined`)
199
+ * @param rule - 校验规则
200
+ * @returns 验证结果对象
201
+ *
202
+ * @description 按顺序执行以下验证步骤:
203
+ * 1. **缺失检查**:必需变量为空时返回 `missing` 状态,非必需变量为空时使用默认值
204
+ * 2. **类型验证**:调用 `validateType` 检查值的类型
205
+ * 3. **范围验证**:调用 `validateRange` 检查数值范围
206
+ * 4. **长度验证**:调用 `validateLength` 检查字符串长度
207
+ * 5. **正则验证**:检查值是否匹配 `pattern` 正则
208
+ * 6. **自定义验证**:执行 `validator` 函数
209
+ *
210
+ * 所有验证步骤中,自定义 `message` 会覆盖默认的错误消息。
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * // 必需变量缺失
215
+ * validateValue('API_KEY', undefined, { type: 'string', required: true })
216
+ * // { key: 'API_KEY', status: 'missing', message: '缺少必需的环境变量: API_KEY', ... }
217
+ *
218
+ * // 类型错误
219
+ * validateValue('PORT', 'abc', { type: 'number', required: true })
220
+ * // { key: 'PORT', status: 'type_error', message: '...', ... }
221
+ *
222
+ * // 使用默认值
223
+ * validateValue('DEBUG', undefined, { type: 'boolean', required: false, default: 'false' })
224
+ * // { key: 'DEBUG', status: 'pass', value: 'false', ... }
225
+ * ```
226
+ */
227
+ declare function validateValue(key: string, value: string | undefined, rule: EnvFieldRule): EnvValidationResult;
228
+ /**
229
+ * 批量验证环境变量
230
+ *
231
+ * @param env - 环境变量键值对对象
232
+ * @param rules - 环境变量校验规则映射
233
+ * @returns 所有变量的验证结果数组
234
+ *
235
+ * @description 遍历所有规则,对每个环境变量调用 `validateValue` 进行验证,
236
+ * 返回包含所有验证结果的数组。可用于汇总展示所有验证错误。
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * const results = validateEnvironment(
241
+ * { API_URL: 'https://api.example.com', PORT: 'abc', MISSING_VAR: undefined },
242
+ * {
243
+ * API_URL: { type: 'url', required: true },
244
+ * PORT: { type: 'number', required: true },
245
+ * MISSING_VAR: { type: 'string', required: true }
246
+ * }
247
+ * )
248
+ * // [
249
+ * // { key: 'API_URL', status: 'pass', ... },
250
+ * // { key: 'PORT', status: 'type_error', ... },
251
+ * // { key: 'MISSING_VAR', status: 'missing', ... }
252
+ * // ]
253
+ *
254
+ * // 检查是否有错误
255
+ * const hasErrors = results.some(r => r.status !== 'pass')
256
+ * ```
257
+ */
258
+ declare function validateEnvironment(env: Record<string, string | undefined>, rules: Record<string, EnvFieldRule>): EnvValidationResult[];
259
+
260
+ export { STRING_LIKE_TYPES as S, validateLength as c, validateRange as d, validateType as e, validateValue as f, validateEnvironment as v };
261
+ export type { EnvFieldRule as E, EnvType as a, EnvValidationResult as b };