auto-translate-json-library 1.5.4 → 2.0.2
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 +1141 -148
- package/build/package.json +97 -21
- package/build/src/aws.d.ts +1 -1
- package/build/src/aws.js +27 -38
- package/build/src/azure.d.ts +1 -1
- package/build/src/azure.js +50 -63
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +196 -37
- package/build/src/config.d.ts +1 -0
- package/build/src/config.js +1 -2
- package/build/src/deepl.d.ts +1 -1
- package/build/src/deepl.js +18 -64
- package/build/src/files.d.ts +11 -2
- package/build/src/files.js +356 -69
- package/build/src/folderFiles.d.ts +6 -3
- package/build/src/folderFiles.js +33 -44
- package/build/src/format/arb-handler.d.ts +33 -0
- package/build/src/format/arb-handler.js +679 -0
- package/build/src/format/base-format-handler.d.ts +75 -0
- package/build/src/format/base-format-handler.js +166 -0
- package/build/src/format/csv-handler.d.ts +38 -0
- package/build/src/format/csv-handler.js +604 -0
- package/build/src/format/index.d.ts +17 -0
- package/build/src/format/index.js +48 -0
- package/build/src/format/json-handler.d.ts +17 -0
- package/build/src/format/json-handler.js +294 -0
- package/build/src/format/po-handler.d.ts +17 -0
- package/build/src/format/po-handler.js +338 -0
- package/build/src/format/po-utils.d.ts +86 -0
- package/build/src/format/po-utils.js +385 -0
- package/build/src/format/pot-handler.d.ts +22 -0
- package/build/src/format/pot-handler.js +331 -0
- package/build/src/format/properties-handler.d.ts +26 -0
- package/build/src/format/properties-handler.js +500 -0
- package/build/src/format/tsv-handler.d.ts +13 -0
- package/build/src/format/tsv-handler.js +67 -0
- package/build/src/format/xliff-handler.d.ts +24 -0
- package/build/src/format/xliff-handler.js +433 -0
- package/build/src/format/xmb-handler.d.ts +33 -0
- package/build/src/format/xmb-handler.js +484 -0
- package/build/src/format/xmb-xtb-utils.d.ts +43 -0
- package/build/src/format/xmb-xtb-utils.js +180 -0
- package/build/src/format/xml-handler.d.ts +28 -0
- package/build/src/format/xml-handler.js +561 -0
- package/build/src/format/xtb-handler.d.ts +33 -0
- package/build/src/format/xtb-handler.js +518 -0
- package/build/src/format/yaml-handler.d.ts +26 -0
- package/build/src/format/yaml-handler.js +522 -0
- package/build/src/format-detector.d.ts +7 -0
- package/build/src/format-detector.js +58 -0
- package/build/src/format-handler-factory.d.ts +9 -0
- package/build/src/format-handler-factory.js +18 -0
- package/build/src/format.interface.d.ts +56 -0
- package/build/src/format.interface.js +1 -0
- package/build/src/google.d.ts +1 -1
- package/build/src/google.js +32 -49
- package/build/src/index.d.ts +4 -3
- package/build/src/index.js +15 -17
- package/build/src/lib.d.ts +1 -1
- package/build/src/lib.js +188 -133
- package/build/src/openai.d.ts +2 -2
- package/build/src/openai.js +59 -50
- package/build/src/translate.interface.d.ts +1 -1
- package/build/src/translate.interface.js +1 -2
- package/build/src/util.js +4 -9
- package/build/src/validation/enhanced-validation-result.d.ts +96 -0
- package/build/src/validation/enhanced-validation-result.js +198 -0
- package/build/src/validation/error-messages.d.ts +88 -0
- package/build/src/validation/error-messages.js +436 -0
- package/build/src/validation/error-recovery.d.ts +78 -0
- package/build/src/validation/error-recovery.js +593 -0
- package/build/src/validation/example-usage.d.ts +31 -0
- package/build/src/validation/example-usage.js +148 -0
- package/build/src/validation/format-rules.d.ts +37 -0
- package/build/src/validation/format-rules.js +550 -0
- package/build/src/validation/format-validator.d.ts +70 -0
- package/build/src/validation/format-validator.js +279 -0
- package/build/src/validation/index.d.ts +13 -0
- package/build/src/validation/index.js +30 -0
- package/build/src/validation/validation-service.d.ts +56 -0
- package/build/src/validation/validation-service.js +262 -0
- package/build/tests/arb-handler.test.d.ts +1 -0
- package/build/tests/arb-handler.test.js +694 -0
- package/build/tests/cli-integration.test.d.ts +1 -0
- package/build/tests/cli-integration.test.js +352 -0
- package/build/tests/csv-handler.test.d.ts +1 -0
- package/build/tests/csv-handler.test.js +507 -0
- package/build/tests/error-handling.test.d.ts +1 -0
- package/build/tests/error-handling.test.js +456 -0
- package/build/tests/files-integration.test.d.ts +1 -0
- package/build/tests/files-integration.test.js +421 -0
- package/build/tests/json-handler.test.d.ts +1 -0
- package/build/tests/json-handler.test.js +526 -0
- package/build/tests/po-handler.test.d.ts +1 -0
- package/build/tests/po-handler.test.js +768 -0
- package/build/tests/properties-handler.test.d.ts +1 -0
- package/build/tests/properties-handler.test.js +316 -0
- package/build/tests/setup.d.ts +1 -0
- package/build/tests/setup.js +5 -0
- package/build/tests/xliff-handler.test.d.ts +1 -0
- package/build/tests/xliff-handler.test.js +459 -0
- package/build/tests/xmb-xtb-handler.test.d.ts +1 -0
- package/build/tests/xmb-xtb-handler.test.js +758 -0
- package/build/tests/xml-handler.test.d.ts +1 -0
- package/build/tests/xml-handler.test.js +570 -0
- package/build/tests/yaml-handler.test.d.ts +1 -0
- package/build/tests/yaml-handler.test.js +156 -0
- package/package.json +97 -21
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
2
|
+
import { ErrorMessageFormatter, } from "./error-messages.js";
|
|
3
|
+
export class FormatValidator {
|
|
4
|
+
static rules = new Map();
|
|
5
|
+
static globalRules = [];
|
|
6
|
+
/**
|
|
7
|
+
* Register format-specific validation rules
|
|
8
|
+
*/
|
|
9
|
+
static registerFormatRules(format, rules) {
|
|
10
|
+
const existingRules = this.rules.get(format) || [];
|
|
11
|
+
this.rules.set(format, [...existingRules, ...rules]);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Register global validation rules that apply to all formats
|
|
15
|
+
*/
|
|
16
|
+
static registerGlobalRules(rules) {
|
|
17
|
+
this.globalRules.push(...rules);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Validate translation file data using format-specific and global rules
|
|
21
|
+
*/
|
|
22
|
+
static validate(data, context) {
|
|
23
|
+
const issues = [];
|
|
24
|
+
// Apply global rules
|
|
25
|
+
for (const rule of this.globalRules) {
|
|
26
|
+
try {
|
|
27
|
+
const ruleIssues = rule.validate(data, context);
|
|
28
|
+
issues.push(...ruleIssues);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
issues.push({
|
|
32
|
+
code: "VALIDATION_RULE_ERROR",
|
|
33
|
+
message: ErrorMessageFormatter.formatMessage("VALIDATION_RULE_ERROR", {
|
|
34
|
+
error: error instanceof Error ? error.message : String(error),
|
|
35
|
+
}),
|
|
36
|
+
severity: "warning",
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Apply format-specific rules
|
|
41
|
+
const formatRules = this.rules.get(context.format) || [];
|
|
42
|
+
for (const rule of formatRules) {
|
|
43
|
+
try {
|
|
44
|
+
const ruleIssues = rule.validate(data, context);
|
|
45
|
+
issues.push(...ruleIssues);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
issues.push({
|
|
49
|
+
code: "VALIDATION_RULE_ERROR",
|
|
50
|
+
message: ErrorMessageFormatter.formatMessage("VALIDATION_RULE_ERROR", {
|
|
51
|
+
error: error instanceof Error ? error.message : String(error),
|
|
52
|
+
}),
|
|
53
|
+
severity: "warning",
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return this.createEnhancedValidationResult(issues, context);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validate translation file data and return enhanced result with detailed messaging
|
|
61
|
+
*/
|
|
62
|
+
static validateWithEnhancedResult(data, context) {
|
|
63
|
+
const basicResult = this.validate(data, context);
|
|
64
|
+
return EnhancedValidationResult.fromValidationResult(basicResult);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Create enhanced validation result from issues
|
|
68
|
+
*/
|
|
69
|
+
static createEnhancedValidationResult(issues, context) {
|
|
70
|
+
const errors = [];
|
|
71
|
+
const warnings = [];
|
|
72
|
+
for (const issue of issues) {
|
|
73
|
+
const messageContext = {
|
|
74
|
+
format: context.format,
|
|
75
|
+
filePath: context.filePath,
|
|
76
|
+
line: issue.line,
|
|
77
|
+
column: issue.column,
|
|
78
|
+
path: issue.path,
|
|
79
|
+
...issue,
|
|
80
|
+
};
|
|
81
|
+
const formattedMessage = issue.message ||
|
|
82
|
+
ErrorMessageFormatter.formatMessage(issue.code, messageContext);
|
|
83
|
+
if (issue.severity === "error") {
|
|
84
|
+
errors.push({
|
|
85
|
+
code: issue.code,
|
|
86
|
+
message: formattedMessage,
|
|
87
|
+
line: issue.line,
|
|
88
|
+
column: issue.column,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
warnings.push({
|
|
93
|
+
code: issue.code,
|
|
94
|
+
message: formattedMessage,
|
|
95
|
+
line: issue.line,
|
|
96
|
+
column: issue.column,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
isValid: errors.length === 0,
|
|
102
|
+
errors,
|
|
103
|
+
warnings,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get all registered rules for a format
|
|
108
|
+
*/
|
|
109
|
+
static getFormatRules(format) {
|
|
110
|
+
return this.rules.get(format) || [];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get all global rules
|
|
114
|
+
*/
|
|
115
|
+
static getGlobalRules() {
|
|
116
|
+
return [...this.globalRules];
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Clear all rules (useful for testing)
|
|
120
|
+
*/
|
|
121
|
+
static clearRules() {
|
|
122
|
+
this.rules.clear();
|
|
123
|
+
this.globalRules.length = 0;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Validate structure integrity for translation files
|
|
127
|
+
*/
|
|
128
|
+
static validateStructureIntegrity(data, context) {
|
|
129
|
+
const issues = [];
|
|
130
|
+
// Basic structure validation
|
|
131
|
+
if (!data || typeof data !== "object") {
|
|
132
|
+
issues.push({
|
|
133
|
+
code: "INVALID_STRUCTURE",
|
|
134
|
+
message: "Translation data must be an object",
|
|
135
|
+
severity: "error",
|
|
136
|
+
});
|
|
137
|
+
return this.createValidationResult(issues);
|
|
138
|
+
}
|
|
139
|
+
// Check for empty data
|
|
140
|
+
const keys = Object.keys(data).filter((key) => !key.startsWith("_"));
|
|
141
|
+
if (keys.length === 0) {
|
|
142
|
+
issues.push({
|
|
143
|
+
code: "EMPTY_TRANSLATION_FILE",
|
|
144
|
+
message: "Translation file contains no translatable content",
|
|
145
|
+
severity: "warning",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Validate translatable content
|
|
149
|
+
this.validateTranslatableContent(data, "", issues);
|
|
150
|
+
// Check for circular references
|
|
151
|
+
try {
|
|
152
|
+
JSON.stringify(data);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
if (error instanceof TypeError && error.message.includes("circular")) {
|
|
156
|
+
issues.push({
|
|
157
|
+
code: "CIRCULAR_REFERENCE",
|
|
158
|
+
message: "Translation data contains circular references",
|
|
159
|
+
severity: "error",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return this.createValidationResult(issues);
|
|
164
|
+
}
|
|
165
|
+
static validateTranslatableContent(obj, path, issues, visited = new Set()) {
|
|
166
|
+
// Prevent infinite recursion
|
|
167
|
+
if (visited.has(obj)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (typeof obj === "object" && obj !== null) {
|
|
171
|
+
visited.add(obj);
|
|
172
|
+
}
|
|
173
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
174
|
+
// Skip metadata keys
|
|
175
|
+
if (key.startsWith("_")) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
179
|
+
if (typeof value === "string") {
|
|
180
|
+
// String values are translatable - validate string content
|
|
181
|
+
this.validateStringContent(value, currentPath, issues);
|
|
182
|
+
}
|
|
183
|
+
else if (typeof value === "object" &&
|
|
184
|
+
value !== null &&
|
|
185
|
+
!Array.isArray(value)) {
|
|
186
|
+
// Nested objects - recursively validate
|
|
187
|
+
this.validateTranslatableContent(value, currentPath, issues, visited);
|
|
188
|
+
}
|
|
189
|
+
else if (Array.isArray(value)) {
|
|
190
|
+
// Arrays - validate each element
|
|
191
|
+
value.forEach((item, index) => {
|
|
192
|
+
const arrayPath = `${currentPath}[${index}]`;
|
|
193
|
+
if (typeof item === "string") {
|
|
194
|
+
this.validateStringContent(item, arrayPath, issues);
|
|
195
|
+
}
|
|
196
|
+
else if (typeof item === "object" && item !== null) {
|
|
197
|
+
this.validateTranslatableContent(item, arrayPath, issues, visited);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
issues.push({
|
|
201
|
+
code: "NON_TRANSLATABLE_ARRAY_ITEM",
|
|
202
|
+
message: `Array item at ${arrayPath} is not translatable (${typeof item})`,
|
|
203
|
+
severity: "warning",
|
|
204
|
+
path: arrayPath,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
// Non-string, non-object values
|
|
211
|
+
issues.push({
|
|
212
|
+
code: "NON_TRANSLATABLE_VALUE",
|
|
213
|
+
message: `Value at ${currentPath} is not translatable (${typeof value})`,
|
|
214
|
+
severity: "warning",
|
|
215
|
+
path: currentPath,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (typeof obj === "object" && obj !== null) {
|
|
220
|
+
visited.delete(obj);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
static validateStringContent(value, path, issues) {
|
|
224
|
+
// Check for empty strings
|
|
225
|
+
if (value.trim().length === 0) {
|
|
226
|
+
issues.push({
|
|
227
|
+
code: "EMPTY_TRANSLATION_STRING",
|
|
228
|
+
message: `Empty or whitespace-only string at ${path}`,
|
|
229
|
+
severity: "warning",
|
|
230
|
+
path: path,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// Check for very long strings (potential data issues)
|
|
234
|
+
if (value.length > 10000) {
|
|
235
|
+
issues.push({
|
|
236
|
+
code: "VERY_LONG_STRING",
|
|
237
|
+
message: `String at ${path} is very long (${value.length} characters) - may indicate data issue`,
|
|
238
|
+
severity: "info",
|
|
239
|
+
path: path,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
// Check for potential HTML/XML content in non-XML formats
|
|
243
|
+
if (this.containsHtmlTags(value)) {
|
|
244
|
+
issues.push({
|
|
245
|
+
code: "POTENTIAL_HTML_CONTENT",
|
|
246
|
+
message: `String at ${path} contains HTML-like tags - ensure proper escaping`,
|
|
247
|
+
severity: "info",
|
|
248
|
+
path: path,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
static containsHtmlTags(value) {
|
|
253
|
+
const htmlTagPattern = /<[^>]+>/;
|
|
254
|
+
return htmlTagPattern.test(value);
|
|
255
|
+
}
|
|
256
|
+
static createValidationResult(issues) {
|
|
257
|
+
const errors = issues
|
|
258
|
+
.filter((issue) => issue.severity === "error")
|
|
259
|
+
.map((issue) => ({
|
|
260
|
+
code: issue.code,
|
|
261
|
+
message: issue.message,
|
|
262
|
+
line: issue.line,
|
|
263
|
+
column: issue.column,
|
|
264
|
+
}));
|
|
265
|
+
const warnings = issues
|
|
266
|
+
.filter((issue) => issue.severity === "warning" || issue.severity === "info")
|
|
267
|
+
.map((issue) => ({
|
|
268
|
+
code: issue.code,
|
|
269
|
+
message: issue.message,
|
|
270
|
+
line: issue.line,
|
|
271
|
+
column: issue.column,
|
|
272
|
+
}));
|
|
273
|
+
return {
|
|
274
|
+
isValid: errors.length === 0,
|
|
275
|
+
errors,
|
|
276
|
+
warnings,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Initialize the validation system with all rules and error recovery
|
|
3
|
+
*/
|
|
4
|
+
export declare function initializeValidation(): void;
|
|
5
|
+
export { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
6
|
+
export { ErrorMessageFormatter } from "./error-messages.js";
|
|
7
|
+
export type { RecoveryResult, RecoveryStrategy, } from "./error-recovery.js";
|
|
8
|
+
export { ErrorRecoveryManager, initializeErrorRecovery, UserGuidanceSystem, } from "./error-recovery.js";
|
|
9
|
+
export { formatSpecificRules, globalValidationRules, } from "./format-rules.js";
|
|
10
|
+
export type { FormatValidationRule, ValidationContext, ValidationIssue, } from "./format-validator.js";
|
|
11
|
+
export { FormatValidator } from "./format-validator.js";
|
|
12
|
+
export type { ValidationServiceResult } from "./validation-service.js";
|
|
13
|
+
export { ValidationService } from "./validation-service.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { initializeErrorRecovery } from "./error-recovery.js";
|
|
2
|
+
import { formatSpecificRules, globalValidationRules } from "./format-rules.js";
|
|
3
|
+
import { FormatValidator } from "./format-validator.js";
|
|
4
|
+
/**
|
|
5
|
+
* Initialize the validation system with all rules and error recovery
|
|
6
|
+
*/
|
|
7
|
+
export function initializeValidation() {
|
|
8
|
+
// Register global rules
|
|
9
|
+
FormatValidator.registerGlobalRules(globalValidationRules);
|
|
10
|
+
// Register format-specific rules
|
|
11
|
+
for (const [format, rules] of Object.entries(formatSpecificRules)) {
|
|
12
|
+
if (rules.length > 0) {
|
|
13
|
+
FormatValidator.registerFormatRules(format, rules);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
// Initialize error recovery strategies
|
|
17
|
+
initializeErrorRecovery();
|
|
18
|
+
}
|
|
19
|
+
// Export enhanced validation result
|
|
20
|
+
export { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
21
|
+
// Export error messaging
|
|
22
|
+
export { ErrorMessageFormatter } from "./error-messages.js";
|
|
23
|
+
// Export error recovery
|
|
24
|
+
export { ErrorRecoveryManager, initializeErrorRecovery, UserGuidanceSystem, } from "./error-recovery.js";
|
|
25
|
+
// Export rule collections for testing or custom usage
|
|
26
|
+
export { formatSpecificRules, globalValidationRules, } from "./format-rules.js";
|
|
27
|
+
// Export the main validator and types
|
|
28
|
+
export { FormatValidator } from "./format-validator.js";
|
|
29
|
+
// Export validation service
|
|
30
|
+
export { ValidationService } from "./validation-service.js";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { IFormatHandler } from "../format.interface.js";
|
|
2
|
+
import type { TranslationFile } from "../translate.interface.js";
|
|
3
|
+
import { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
4
|
+
import { type RecoveryResult } from "./error-recovery.js";
|
|
5
|
+
import { type ValidationContext } from "./format-validator.js";
|
|
6
|
+
/**
|
|
7
|
+
* Comprehensive validation and recovery service
|
|
8
|
+
*/
|
|
9
|
+
export declare class ValidationService {
|
|
10
|
+
/**
|
|
11
|
+
* Validate a translation file with comprehensive error handling and recovery
|
|
12
|
+
*/
|
|
13
|
+
static validateFile(content: string, filePath: string, handler: IFormatHandler, options?: {
|
|
14
|
+
attemptRecovery?: boolean;
|
|
15
|
+
includeGuidance?: boolean;
|
|
16
|
+
strictMode?: boolean;
|
|
17
|
+
}): Promise<ValidationServiceResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Validate translation data without parsing (for already parsed data)
|
|
20
|
+
*/
|
|
21
|
+
static validateData(data: TranslationFile, context: ValidationContext, options?: {
|
|
22
|
+
includeGuidance?: boolean;
|
|
23
|
+
strictMode?: boolean;
|
|
24
|
+
}): ValidationServiceResult;
|
|
25
|
+
/**
|
|
26
|
+
* Get format name from handler
|
|
27
|
+
*/
|
|
28
|
+
private static getFormatFromHandler;
|
|
29
|
+
/**
|
|
30
|
+
* Get guidance for parse errors
|
|
31
|
+
*/
|
|
32
|
+
private static getParseErrorGuidance;
|
|
33
|
+
/**
|
|
34
|
+
* Get guidance for validation issues
|
|
35
|
+
*/
|
|
36
|
+
private static getValidationGuidance;
|
|
37
|
+
/**
|
|
38
|
+
* Get format-specific guidance
|
|
39
|
+
*/
|
|
40
|
+
private static getFormatSpecificGuidance;
|
|
41
|
+
/**
|
|
42
|
+
* Create a validation report for CLI or logging
|
|
43
|
+
*/
|
|
44
|
+
static createValidationReport(result: ValidationServiceResult): string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Result of validation service operation
|
|
48
|
+
*/
|
|
49
|
+
export interface ValidationServiceResult {
|
|
50
|
+
success: boolean;
|
|
51
|
+
data?: TranslationFile;
|
|
52
|
+
validationResult: EnhancedValidationResult;
|
|
53
|
+
parseError: Error | null;
|
|
54
|
+
recoveryResult: RecoveryResult | null;
|
|
55
|
+
guidance?: string;
|
|
56
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { EnhancedValidationResult } from "./enhanced-validation-result.js";
|
|
2
|
+
import { ErrorRecoveryManager, UserGuidanceSystem, } from "./error-recovery.js";
|
|
3
|
+
import { FormatValidator } from "./format-validator.js";
|
|
4
|
+
/**
|
|
5
|
+
* Comprehensive validation and recovery service
|
|
6
|
+
*/
|
|
7
|
+
export class ValidationService {
|
|
8
|
+
/**
|
|
9
|
+
* Validate a translation file with comprehensive error handling and recovery
|
|
10
|
+
*/
|
|
11
|
+
static async validateFile(content, filePath, handler, options = {}) {
|
|
12
|
+
const { attemptRecovery = true, includeGuidance = true, strictMode = false, } = options;
|
|
13
|
+
let data;
|
|
14
|
+
let parseError = null;
|
|
15
|
+
let recoveryResult = null;
|
|
16
|
+
// Attempt to parse the file
|
|
17
|
+
try {
|
|
18
|
+
data = handler.parse(content);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
parseError = error instanceof Error ? error : new Error(String(error));
|
|
22
|
+
if (!attemptRecovery) {
|
|
23
|
+
return {
|
|
24
|
+
success: false,
|
|
25
|
+
validationResult: new EnhancedValidationResult([
|
|
26
|
+
{
|
|
27
|
+
code: "PARSE_ERROR",
|
|
28
|
+
message: `Failed to parse file: ${parseError.message}`,
|
|
29
|
+
line: undefined,
|
|
30
|
+
column: undefined,
|
|
31
|
+
},
|
|
32
|
+
], []),
|
|
33
|
+
parseError,
|
|
34
|
+
recoveryResult: null,
|
|
35
|
+
guidance: includeGuidance
|
|
36
|
+
? this.getParseErrorGuidance(parseError, filePath)
|
|
37
|
+
: undefined,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// Attempt recovery
|
|
41
|
+
recoveryResult = ErrorRecoveryManager.attemptRecovery(parseError, content, filePath);
|
|
42
|
+
if (!recoveryResult.success) {
|
|
43
|
+
return {
|
|
44
|
+
success: false,
|
|
45
|
+
validationResult: new EnhancedValidationResult([
|
|
46
|
+
{
|
|
47
|
+
code: "PARSE_ERROR",
|
|
48
|
+
message: `Failed to parse file and recovery failed: ${parseError.message}`,
|
|
49
|
+
line: undefined,
|
|
50
|
+
column: undefined,
|
|
51
|
+
},
|
|
52
|
+
], []),
|
|
53
|
+
parseError,
|
|
54
|
+
recoveryResult,
|
|
55
|
+
guidance: includeGuidance
|
|
56
|
+
? this.getParseErrorGuidance(parseError, filePath)
|
|
57
|
+
: undefined,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
data = recoveryResult.data;
|
|
61
|
+
}
|
|
62
|
+
// Validate the parsed data
|
|
63
|
+
const context = {
|
|
64
|
+
format: this.getFormatFromHandler(handler),
|
|
65
|
+
filePath,
|
|
66
|
+
originalContent: content,
|
|
67
|
+
handler,
|
|
68
|
+
};
|
|
69
|
+
let validationResult;
|
|
70
|
+
if (handler.validateWithRules) {
|
|
71
|
+
const result = handler.validateWithRules(data, filePath, content);
|
|
72
|
+
validationResult = EnhancedValidationResult.fromValidationResult(result);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const result = FormatValidator.validate(data, context);
|
|
76
|
+
validationResult = EnhancedValidationResult.fromValidationResult(result);
|
|
77
|
+
}
|
|
78
|
+
// In strict mode, treat warnings as errors
|
|
79
|
+
if (strictMode && validationResult.warnings.length > 0) {
|
|
80
|
+
const strictErrors = validationResult.warnings.map((warning) => ({
|
|
81
|
+
...warning,
|
|
82
|
+
severity: "error",
|
|
83
|
+
}));
|
|
84
|
+
validationResult = new EnhancedValidationResult([...validationResult.errors, ...strictErrors], []);
|
|
85
|
+
}
|
|
86
|
+
const success = validationResult.isValid &&
|
|
87
|
+
(parseError === null || recoveryResult?.success === true);
|
|
88
|
+
return {
|
|
89
|
+
success,
|
|
90
|
+
data,
|
|
91
|
+
validationResult,
|
|
92
|
+
parseError,
|
|
93
|
+
recoveryResult,
|
|
94
|
+
guidance: includeGuidance
|
|
95
|
+
? this.getValidationGuidance(validationResult, context)
|
|
96
|
+
: undefined,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Validate translation data without parsing (for already parsed data)
|
|
101
|
+
*/
|
|
102
|
+
static validateData(data, context, options = {}) {
|
|
103
|
+
const { includeGuidance = true, strictMode = false } = options;
|
|
104
|
+
let validationResult = EnhancedValidationResult.fromValidationResult(FormatValidator.validate(data, context));
|
|
105
|
+
// In strict mode, treat warnings as errors
|
|
106
|
+
if (strictMode && validationResult.warnings.length > 0) {
|
|
107
|
+
const strictErrors = validationResult.warnings.map((warning) => ({
|
|
108
|
+
...warning,
|
|
109
|
+
severity: "error",
|
|
110
|
+
}));
|
|
111
|
+
validationResult = new EnhancedValidationResult([...validationResult.errors, ...strictErrors], []);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
success: validationResult.isValid,
|
|
115
|
+
data,
|
|
116
|
+
validationResult,
|
|
117
|
+
parseError: null,
|
|
118
|
+
recoveryResult: null,
|
|
119
|
+
guidance: includeGuidance
|
|
120
|
+
? this.getValidationGuidance(validationResult, context)
|
|
121
|
+
: undefined,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Get format name from handler
|
|
126
|
+
*/
|
|
127
|
+
static getFormatFromHandler(handler) {
|
|
128
|
+
const extension = handler.getFileExtension();
|
|
129
|
+
return extension.replace(".", "");
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get guidance for parse errors
|
|
133
|
+
*/
|
|
134
|
+
static getParseErrorGuidance(error, filePath) {
|
|
135
|
+
const errorMessage = error.message.toLowerCase();
|
|
136
|
+
let errorType = "PARSE_ERROR";
|
|
137
|
+
if (errorMessage.includes("json")) {
|
|
138
|
+
errorType = "JSON_PARSE_ERROR";
|
|
139
|
+
}
|
|
140
|
+
else if (errorMessage.includes("xml")) {
|
|
141
|
+
errorType = "XML_PARSE_ERROR";
|
|
142
|
+
}
|
|
143
|
+
else if (errorMessage.includes("xliff")) {
|
|
144
|
+
errorType = "XLIFF_VALIDATION_ERROR";
|
|
145
|
+
}
|
|
146
|
+
else if (errorMessage.includes("encoding")) {
|
|
147
|
+
errorType = "ENCODING_ERROR";
|
|
148
|
+
}
|
|
149
|
+
return UserGuidanceSystem.getFormattedGuidance(errorType, error);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get guidance for validation issues
|
|
153
|
+
*/
|
|
154
|
+
static getValidationGuidance(result, context) {
|
|
155
|
+
if (result.getTotalIssueCount() === 0) {
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
const lines = [];
|
|
159
|
+
// Add summary
|
|
160
|
+
lines.push(result.getSummary());
|
|
161
|
+
lines.push("");
|
|
162
|
+
// Add actionable suggestions
|
|
163
|
+
const suggestions = result.getSuggestions();
|
|
164
|
+
if (suggestions.length > 0) {
|
|
165
|
+
lines.push("💡 Suggestions:");
|
|
166
|
+
for (const suggestion of suggestions.slice(0, 5)) {
|
|
167
|
+
// Limit to top 5 suggestions
|
|
168
|
+
lines.push(` • ${suggestion}`);
|
|
169
|
+
}
|
|
170
|
+
lines.push("");
|
|
171
|
+
}
|
|
172
|
+
// Add format-specific guidance
|
|
173
|
+
const formatGuidance = this.getFormatSpecificGuidance(context.format, result);
|
|
174
|
+
if (formatGuidance) {
|
|
175
|
+
lines.push(formatGuidance);
|
|
176
|
+
}
|
|
177
|
+
return lines.join("\n");
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get format-specific guidance
|
|
181
|
+
*/
|
|
182
|
+
static getFormatSpecificGuidance(format, result) {
|
|
183
|
+
const errorCodes = result.errors.map((error) => error.code);
|
|
184
|
+
const warningCodes = result.warnings.map((warning) => warning.code);
|
|
185
|
+
const allCodes = [...errorCodes, ...warningCodes];
|
|
186
|
+
// Check for format-specific error patterns
|
|
187
|
+
if (format === "json" && allCodes.some((code) => code.includes("JSON"))) {
|
|
188
|
+
return UserGuidanceSystem.getFormattedGuidance("JSON_PARSE_ERROR");
|
|
189
|
+
}
|
|
190
|
+
if (format === "xliff" && allCodes.some((code) => code.includes("XLIFF"))) {
|
|
191
|
+
return UserGuidanceSystem.getFormattedGuidance("XLIFF_VALIDATION_ERROR");
|
|
192
|
+
}
|
|
193
|
+
if (format === "arb" && allCodes.some((code) => code.includes("ARB"))) {
|
|
194
|
+
return UserGuidanceSystem.getFormattedGuidance("ARB_VALIDATION_ERROR");
|
|
195
|
+
}
|
|
196
|
+
if (format === "po" && allCodes.some((code) => code.includes("PO"))) {
|
|
197
|
+
return UserGuidanceSystem.getFormattedGuidance("PO_VALIDATION_ERROR");
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Create a validation report for CLI or logging
|
|
203
|
+
*/
|
|
204
|
+
static createValidationReport(result) {
|
|
205
|
+
const lines = [];
|
|
206
|
+
// Header
|
|
207
|
+
lines.push("=".repeat(60));
|
|
208
|
+
lines.push("TRANSLATION FILE VALIDATION REPORT");
|
|
209
|
+
lines.push("=".repeat(60));
|
|
210
|
+
lines.push("");
|
|
211
|
+
// Overall status
|
|
212
|
+
const statusIcon = result.success ? "✅" : "❌";
|
|
213
|
+
lines.push(`Status: ${statusIcon} ${result.success ? "PASSED" : "FAILED"}`);
|
|
214
|
+
lines.push("");
|
|
215
|
+
// Parse information
|
|
216
|
+
if (result.parseError) {
|
|
217
|
+
lines.push("📄 Parse Status:");
|
|
218
|
+
lines.push(` ❌ Parse Error: ${result.parseError.message}`);
|
|
219
|
+
if (result.recoveryResult) {
|
|
220
|
+
const recoveryIcon = result.recoveryResult.success ? "✅" : "❌";
|
|
221
|
+
lines.push(` ${recoveryIcon} Recovery: ${result.recoveryResult.recoveryMethod}`);
|
|
222
|
+
if (result.recoveryResult.success) {
|
|
223
|
+
lines.push(` ${result.recoveryResult.partialRecovery ? "Partial" : "Full"} recovery successful`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
lines.push("");
|
|
227
|
+
}
|
|
228
|
+
// Validation results
|
|
229
|
+
if (result.validationResult) {
|
|
230
|
+
lines.push("🔍 Validation Results:");
|
|
231
|
+
lines.push(` Errors: ${result.validationResult.errors.length}`);
|
|
232
|
+
lines.push(` Warnings: ${result.validationResult.warnings.length}`);
|
|
233
|
+
lines.push("");
|
|
234
|
+
// Detailed issues
|
|
235
|
+
if (result.validationResult.errors.length > 0) {
|
|
236
|
+
lines.push("❌ Errors:");
|
|
237
|
+
for (const error of result.validationResult.errors) {
|
|
238
|
+
lines.push(` • [${error.code}] ${error.message}`);
|
|
239
|
+
}
|
|
240
|
+
lines.push("");
|
|
241
|
+
}
|
|
242
|
+
if (result.validationResult.warnings.length > 0) {
|
|
243
|
+
lines.push("⚠️ Warnings:");
|
|
244
|
+
for (const warning of result.validationResult.warnings) {
|
|
245
|
+
lines.push(` • [${warning.code}] ${warning.message}`);
|
|
246
|
+
}
|
|
247
|
+
lines.push("");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// Guidance
|
|
251
|
+
if (result.guidance) {
|
|
252
|
+
lines.push("📋 Guidance:");
|
|
253
|
+
lines.push(result.guidance
|
|
254
|
+
.split("\n")
|
|
255
|
+
.map((line) => ` ${line}`)
|
|
256
|
+
.join("\n"));
|
|
257
|
+
lines.push("");
|
|
258
|
+
}
|
|
259
|
+
lines.push("=".repeat(60));
|
|
260
|
+
return lines.join("\n");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|