schema-dsl 1.0.4 → 1.0.6

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/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * 简洁 + 强大 = 完美平衡
6
6
  *
7
7
  * @module schema-dsl
8
- * @version 1.0.3
8
+ * @version 1.0.4
9
9
  */
10
10
 
11
11
  // ========== 核心层 ==========
@@ -36,6 +36,7 @@ dsl.if = dsl.DslAdapter.if;
36
36
  * @param {Object} options - 配置选项
37
37
  * @param {Object} options.patterns - 验证规则扩展 (phone, idCard, creditCard)
38
38
  * @param {string|Object} options.i18n - 多语言配置(目录路径或语言包对象)
39
+ * @param {Object} options.cache - 缓存配置
39
40
  */
40
41
  dsl.config = function (options = {}) {
41
42
  const patterns = require('./lib/config/patterns');
@@ -74,6 +75,29 @@ dsl.config = function (options = {}) {
74
75
  });
75
76
  }
76
77
  }
78
+
79
+ // 缓存配置 (v1.0.4+)
80
+ if (options.cache) {
81
+ // 如果 Validator 还未创建,保存配置供后续创建时使用
82
+ if (!_defaultValidator) {
83
+ _validatorOptions.cache = options.cache;
84
+ } else {
85
+ // 如果已创建,动态修改现有实例的配置(向后兼容)
86
+ const cacheOpts = _defaultValidator.cache.options;
87
+ if (options.cache.maxSize !== undefined) {
88
+ cacheOpts.maxSize = options.cache.maxSize;
89
+ }
90
+ if (options.cache.ttl !== undefined) {
91
+ cacheOpts.ttl = options.cache.ttl;
92
+ }
93
+ if (options.cache.enabled !== undefined) {
94
+ cacheOpts.enabled = options.cache.enabled;
95
+ }
96
+ if (options.cache.statsEnabled !== undefined) {
97
+ cacheOpts.statsEnabled = options.cache.statsEnabled;
98
+ }
99
+ }
100
+ }
77
101
  };
78
102
 
79
103
  // ========== 导出器层 ==========
@@ -87,6 +111,7 @@ Object.entries(defaultLocales).forEach(([locale, messages]) => {
87
111
 
88
112
  // ========== 单例Validator ==========
89
113
  let _defaultValidator = null;
114
+ let _validatorOptions = {}; // 存储 Validator 配置选项
90
115
 
91
116
  /**
92
117
  * 获取默认Validator实例(单例)
@@ -94,7 +119,7 @@ let _defaultValidator = null;
94
119
  */
95
120
  function getDefaultValidator() {
96
121
  if (!_defaultValidator) {
97
- _defaultValidator = new Validator();
122
+ _defaultValidator = new Validator(_validatorOptions);
98
123
  }
99
124
  return _defaultValidator;
100
125
  }
@@ -128,91 +153,6 @@ const CONSTANTS = require('./lib/config/constants');
128
153
  // ========== 自动安装 String 扩展 ==========
129
154
  installStringExtensions(dsl);
130
155
 
131
- // ========== 全局配置函数 ==========
132
- /**
133
- * 全局配置
134
- * @param {Object} options - 配置选项
135
- * @param {Object} options.i18n - 多语言配置
136
- * @param {string} options.i18n.localesPath - 用户语言包目录路径
137
- * @param {Object} options.i18n.locales - 直接传入的语言包对象
138
- * @param {Object} options.cache - 缓存配置
139
- * @param {number} options.cache.maxSize - 缓存最大条目数
140
- * @param {number} options.cache.ttl - 缓存过期时间(ms)
141
- *
142
- * @example
143
- * dsl.config({
144
- * i18n: {
145
- * localesPath: './i18n/labels'
146
- * },
147
- * cache: {
148
- * maxSize: 10000,
149
- * ttl: 7200000
150
- * }
151
- * });
152
- */
153
- dsl.config = function (options = {}) {
154
- // ========== 用户语言包配置 ==========
155
- if (options.i18n) {
156
- const { localesPath, locales } = options.i18n;
157
-
158
- // 方式 1:从路径加载语言包文件
159
- if (localesPath) {
160
- const fs = require('fs');
161
- const path = require('path');
162
-
163
- const resolvedPath = path.resolve(localesPath);
164
-
165
- if (fs.existsSync(resolvedPath)) {
166
- const files = fs.readdirSync(resolvedPath);
167
-
168
- files.forEach(file => {
169
- if (file.endsWith('.js') || file.endsWith('.json')) {
170
- const locale = path.basename(file, path.extname(file));
171
- try {
172
- const messages = require(path.join(resolvedPath, file));
173
- Locale.addLocale(locale, messages);
174
- if (process.env.DEBUG) {
175
- console.log(`[schema-dsl] Loaded user locale: ${locale}`);
176
- }
177
- } catch (error) {
178
- console.warn(`[schema-dsl] Failed to load locale ${locale}:`, error.message);
179
- }
180
- }
181
- });
182
- } else {
183
- console.warn(`[schema-dsl] Locales path not found: ${resolvedPath}`);
184
- }
185
- }
186
-
187
- // 方式 2:直接传入语言包对象
188
- if (locales && typeof locales === 'object') {
189
- Object.keys(locales).forEach(locale => {
190
- Locale.addLocale(locale, locales[locale]);
191
- if (process.env.DEBUG) {
192
- console.log(`[schema-dsl] Added user locale: ${locale}`);
193
- }
194
- });
195
- }
196
- }
197
-
198
- // ========== 缓存配置 ==========
199
- if (options.cache) {
200
- const { maxSize, ttl } = options.cache;
201
-
202
- // 更新默认 Validator 的缓存配置
203
- if (_defaultValidator) {
204
- if (maxSize !== undefined) {
205
- _defaultValidator.cache.options.maxSize = maxSize;
206
- }
207
- if (ttl !== undefined) {
208
- _defaultValidator.cache.options.ttl = ttl;
209
- }
210
- if (process.env.DEBUG) {
211
- console.log(`[schema-dsl] Updated cache config: maxSize=${maxSize || 'default'}, ttl=${ttl || 'default'}ms`);
212
- }
213
- }
214
- }
215
- };
216
156
 
217
157
  // ========== 导出 ==========
218
158
 
@@ -227,6 +167,9 @@ module.exports = {
227
167
  dsl,
228
168
  DslBuilder,
229
169
 
170
+ // 配置函数 (v1.0.4+)
171
+ config: dsl.config,
172
+
230
173
  // String 扩展控制
231
174
  installStringExtensions: () => installStringExtensions(dsl),
232
175
  uninstallStringExtensions,
@@ -272,7 +215,7 @@ module.exports = {
272
215
  CONSTANTS,
273
216
 
274
217
  // 版本信息
275
- VERSION: '1.0.3'
218
+ VERSION: '1.0.4'
276
219
  };
277
220
 
278
221
 
@@ -28,6 +28,9 @@ class Validator {
28
28
  * @param {boolean} options.useDefaults - 是否使用默认值(默认true)
29
29
  * @param {boolean} options.coerceTypes - 是否自动类型转换(默认false)
30
30
  * @param {boolean} options.removeAdditional - 是否移除额外属性(默认false)
31
+ * @param {Object} options.cache - 缓存配置选项 (v1.0.4+)
32
+ * @param {number} options.cache.maxSize - 最大缓存条目数(默认100)
33
+ * @param {number} options.cache.ttl - 缓存过期时间(毫秒,默认3600000)
31
34
  */
32
35
  constructor(options = {}) {
33
36
  // ajv配置
@@ -49,11 +52,14 @@ class Validator {
49
52
  // 注册自定义关键字
50
53
  CustomKeywords.registerAll(this.ajv);
51
54
 
52
- // 编译缓存
53
- this.cache = new CacheManager({
54
- maxSize: 100,
55
- ttl: 3600000 // 1小时
56
- });
55
+ // 编译缓存(支持自定义配置)
56
+ const cacheOptions = {
57
+ maxSize: options.cache?.maxSize ?? 100,
58
+ ttl: options.cache?.ttl ?? 3600000, // 1小时
59
+ enabled: options.cache?.enabled,
60
+ statsEnabled: options.cache?.statsEnabled
61
+ };
62
+ this.cache = new CacheManager(cacheOptions);
57
63
 
58
64
  // 错误格式化器
59
65
  this.errorFormatter = new ErrorFormatter();
@@ -395,7 +401,7 @@ class Validator {
395
401
 
396
402
  // Support calling without new
397
403
  const ValidatorProxy = new Proxy(Validator, {
398
- apply: function(target, thisArg, argumentsList) {
404
+ apply: function (target, thisArg, argumentsList) {
399
405
  return new target(...argumentsList);
400
406
  }
401
407
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schema-dsl",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "简洁强大的JSON Schema验证库 - DSL语法 + String扩展 + 便捷validate",
5
5
  "main": "index.js",
6
6
  "exports": {