schema-dsl 1.1.5 → 1.1.7

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.
@@ -134,7 +134,18 @@ class ErrorFormatter {
134
134
  // 处理 ajv 错误格式
135
135
  const keyword = err.keyword || err.type || 'validation';
136
136
  const instancePath = err.instancePath || err.path || '';
137
- const fieldName = instancePath.replace(/^\//, '') || (err.params && err.params.missingProperty) || 'value';
137
+
138
+ // ✅ 修复:正确处理 required 错误的字段名
139
+ let fieldName;
140
+ if (keyword === 'required' && err.params && err.params.missingProperty) {
141
+ // required 错误:字段名应该是父路径 + 缺失属性
142
+ const parentPath = instancePath.replace(/^\//, '');
143
+ const missingProp = err.params.missingProperty;
144
+ fieldName = parentPath ? `${parentPath}/${missingProp}` : missingProp;
145
+ } else {
146
+ // 其他错误:直接使用 instancePath
147
+ fieldName = instancePath.replace(/^\//, '') || 'value';
148
+ }
138
149
 
139
150
  // 获取 Schema 中的自定义信息
140
151
  let schema = err.parentSchema || err.schema || {};
@@ -155,14 +166,27 @@ class ErrorFormatter {
155
166
  label = finalMessages[label];
156
167
  }
157
168
  } else {
169
+ // ✅ 修复:对于所有错误类型,label 都应该只显示字段名(不含路径)
170
+ let labelKey;
171
+ if (keyword === 'required' && err.params && err.params.missingProperty) {
172
+ // required 错误:使用 missingProperty(缺失的字段名)
173
+ labelKey = err.params.missingProperty;
174
+ } else {
175
+ // 其他错误:从完整路径中提取最后一级字段名
176
+ // 例如: "user/profile/age" -> "age"
177
+ // "user/scores/1" -> "1"
178
+ const parts = fieldName.split('/');
179
+ labelKey = parts[parts.length - 1];
180
+ }
181
+
158
182
  // 如果没有显式设置 label,尝试自动查找翻译 (label.fieldName)
159
183
  // 将路径分隔符 / 转换为 . (例如 address/city -> address.city)
160
- const autoKey = `label.${fieldName.replace(/\//g, '.')}`;
184
+ const autoKey = `label.${labelKey.replace(/\//g, '.')}`;
161
185
  if (finalMessages[autoKey]) {
162
186
  label = finalMessages[autoKey];
163
187
  } else {
164
- // 没找到翻译,回退到 fieldName
165
- label = fieldName;
188
+ // 没找到翻译,回退到字段名
189
+ label = labelKey;
166
190
  }
167
191
  }
168
192
 
@@ -282,7 +306,12 @@ class ErrorFormatter {
282
306
  actual: err.data === null ? 'null' :
283
307
  err.data === undefined ? 'undefined' :
284
308
  Array.isArray(err.data) ? 'array' :
285
- typeof err.data
309
+ typeof err.data,
310
+ // ✅ 修复 enum 错误:将 allowedValues 映射为 valids 和 allowed
311
+ valids: err.params && err.params.allowedValues ? err.params.allowedValues.join(', ') : undefined,
312
+ allowed: err.params && err.params.allowedValues ? err.params.allowedValues.join(', ') : undefined,
313
+ // ✅ 修复 additionalProperties 错误:将 additionalProperty 映射为 key
314
+ key: err.params && err.params.additionalProperty ? err.params.additionalProperty : undefined
286
315
  };
287
316
 
288
317
  message = this._interpolate(message, interpolateData);
@@ -96,6 +96,8 @@ module.exports = {
96
96
  // v1.0.2
97
97
  'object.missing': '{{#label}} is missing required properties',
98
98
  'object.schema': '{{#label}} contains additional properties',
99
+ // v1.1.6 - additionalProperties
100
+ 'additionalProperties': '{{#label}} must NOT have additional properties: {{#key}}',
99
101
 
100
102
  // Array
101
103
  'array.base': '{{#label}} must be an array',
@@ -44,6 +44,8 @@ module.exports = {
44
44
  'object.min': '{{#label}} debe tener al menos {{#limit}} propiedades',
45
45
  'object.max': '{{#label}} debe tener como máximo {{#limit}} propiedades',
46
46
  'object.unknown': '{{#label}} contiene una propiedad desconocida: {{#key}}',
47
+ // v1.1.6 - additionalProperties
48
+ 'additionalProperties': '{{#label}} NO debe tener propiedades adicionales: {{#key}}',
47
49
 
48
50
  // Array
49
51
  'array.base': '{{#label}} debe ser un array',
@@ -44,6 +44,8 @@ module.exports = {
44
44
  'object.min': '{{#label}} doit avoir au moins {{#limit}} propriétés',
45
45
  'object.max': '{{#label}} doit avoir au plus {{#limit}} propriétés',
46
46
  'object.unknown': '{{#label}} contient une propriété inconnue : {{#key}}',
47
+ // v1.1.6 - additionalProperties
48
+ 'additionalProperties': '{{#label}} NE doit PAS avoir de propriétés supplémentaires : {{#key}}',
47
49
 
48
50
  // Array
49
51
  'array.base': '{{#label}} doit être un tableau',
@@ -49,6 +49,8 @@ module.exports = {
49
49
  'object.min': '{{#label}}は少なくとも{{#limit}}個のプロパティを持つ必要があります',
50
50
  'object.max': '{{#label}}は最大{{#limit}}個のプロパティを持つことができます',
51
51
  'object.unknown': '{{#label}}に未知のプロパティが含まれています: {{#key}}',
52
+ // v1.1.6 - additionalProperties
53
+ 'additionalProperties': '{{#label}}に追加のプロパティを含めてはいけません: {{#key}}',
52
54
 
53
55
  // Array
54
56
  'array.base': '{{#label}}は配列である必要があります',
@@ -97,6 +97,8 @@ module.exports = {
97
97
  // v1.0.2新增
98
98
  'object.missing': '{{#label}}缺少必需属性',
99
99
  'object.schema': '{{#label}}包含额外属性',
100
+ // v1.1.6新增 - additionalProperties
101
+ 'additionalProperties': '{{#label}}不允许有额外属性: {{#key}}',
100
102
 
101
103
  // Array
102
104
  'array.base': '{{#label}}必须是数组类型',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "schema-dsl",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "简洁强大的JSON Schema验证库 - DSL语法 + String扩展 + 便捷validate",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",