@json-to-office/shared-docx 0.1.0

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 (59) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +9 -0
  3. package/dist/chunk-5533WPL6.js +1895 -0
  4. package/dist/chunk-5533WPL6.js.map +1 -0
  5. package/dist/chunk-AA4HIXRG.js +34 -0
  6. package/dist/chunk-AA4HIXRG.js.map +1 -0
  7. package/dist/chunk-BEPLK76M.js +454 -0
  8. package/dist/chunk-BEPLK76M.js.map +1 -0
  9. package/dist/chunk-BOUURUYX.js +364 -0
  10. package/dist/chunk-BOUURUYX.js.map +1 -0
  11. package/dist/chunk-EKWM2CWA.js +97 -0
  12. package/dist/chunk-EKWM2CWA.js.map +1 -0
  13. package/dist/chunk-ET6YMNMF.js +35 -0
  14. package/dist/chunk-ET6YMNMF.js.map +1 -0
  15. package/dist/chunk-F5LVWDTY.js +57 -0
  16. package/dist/chunk-F5LVWDTY.js.map +1 -0
  17. package/dist/chunk-HHMK2RWF.js +274 -0
  18. package/dist/chunk-HHMK2RWF.js.map +1 -0
  19. package/dist/chunk-LOE6BZQG.js +750 -0
  20. package/dist/chunk-LOE6BZQG.js.map +1 -0
  21. package/dist/chunk-LWR4TFZ5.js +121 -0
  22. package/dist/chunk-LWR4TFZ5.js.map +1 -0
  23. package/dist/chunk-VP3X6DBP.js +44 -0
  24. package/dist/chunk-VP3X6DBP.js.map +1 -0
  25. package/dist/document-validator-CiaGiy1v.d.ts +119 -0
  26. package/dist/index.d.ts +938 -0
  27. package/dist/index.js +1129 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/schemas/api.d.ts +46 -0
  30. package/dist/schemas/api.js +16 -0
  31. package/dist/schemas/api.js.map +1 -0
  32. package/dist/schemas/component-registry.d.ts +90 -0
  33. package/dist/schemas/component-registry.js +24 -0
  34. package/dist/schemas/component-registry.js.map +1 -0
  35. package/dist/schemas/components.d.ts +755 -0
  36. package/dist/schemas/components.js +94 -0
  37. package/dist/schemas/components.js.map +1 -0
  38. package/dist/schemas/custom-components.d.ts +59 -0
  39. package/dist/schemas/custom-components.js +11 -0
  40. package/dist/schemas/custom-components.js.map +1 -0
  41. package/dist/schemas/document.d.ts +43 -0
  42. package/dist/schemas/document.js +19 -0
  43. package/dist/schemas/document.js.map +1 -0
  44. package/dist/schemas/export.d.ts +67 -0
  45. package/dist/schemas/export.js +19 -0
  46. package/dist/schemas/export.js.map +1 -0
  47. package/dist/schemas/font.d.ts +57 -0
  48. package/dist/schemas/font.js +11 -0
  49. package/dist/schemas/font.js.map +1 -0
  50. package/dist/schemas/generator.d.ts +80 -0
  51. package/dist/schemas/generator.js +9 -0
  52. package/dist/schemas/generator.js.map +1 -0
  53. package/dist/schemas/theme.d.ts +3354 -0
  54. package/dist/schemas/theme.js +42 -0
  55. package/dist/schemas/theme.js.map +1 -0
  56. package/dist/validation/unified/index.d.ts +720 -0
  57. package/dist/validation/unified/index.js +154 -0
  58. package/dist/validation/unified/index.js.map +1 -0
  59. package/package.json +80 -0
@@ -0,0 +1,750 @@
1
+ import {
2
+ ComponentDefinitionSchema,
3
+ StandardComponentDefinitionSchema
4
+ } from "./chunk-ET6YMNMF.js";
5
+ import {
6
+ ColumnsPropsSchema,
7
+ FooterPropsSchema,
8
+ HeaderPropsSchema,
9
+ HeadingPropsSchema,
10
+ ImagePropsSchema,
11
+ ListPropsSchema,
12
+ ParagraphPropsSchema,
13
+ ReportPropsSchema,
14
+ SectionPropsSchema,
15
+ StatisticPropsSchema,
16
+ TablePropsSchema
17
+ } from "./chunk-5533WPL6.js";
18
+ import {
19
+ CustomComponentDefinitionSchema
20
+ } from "./chunk-VP3X6DBP.js";
21
+
22
+ // src/validation/unified/error-transformer.ts
23
+ import {
24
+ isObjectSchema,
25
+ getObjectSchemaPropertyNames,
26
+ getLiteralValue,
27
+ createErrorConfig,
28
+ formatErrorMessage,
29
+ ERROR_EMOJIS
30
+ } from "@json-to-office/shared";
31
+ function generateEnhancedMessage(error, _config) {
32
+ const typeStr = String(error.type || "");
33
+ const path = error.path || "root";
34
+ if (typeStr === "62" || typeStr === "union") {
35
+ return generateUnionErrorMessage(error);
36
+ }
37
+ if (error.message?.includes("additionalProperties")) {
38
+ return generateAdditionalPropertiesMessage(error);
39
+ }
40
+ if (error.message?.includes("Required property")) {
41
+ return generateRequiredPropertyMessage(error);
42
+ }
43
+ if (typeStr === "string" || typeStr === "number" || typeStr === "boolean" || typeStr === "array" || typeStr === "object") {
44
+ return generateTypeMismatchMessage(error);
45
+ }
46
+ if (typeStr === "literal") {
47
+ return generateLiteralErrorMessage(error);
48
+ }
49
+ if (typeStr === "pattern" || typeStr === "RegExp") {
50
+ return generatePatternErrorMessage(error);
51
+ }
52
+ return `At ${path}: ${error.message}`;
53
+ }
54
+ function generateUnionErrorMessage(error) {
55
+ const path = error.path || "root";
56
+ const value = error.value;
57
+ if (path === "root" || path === "/" || path === "/jsonDefinition") {
58
+ if (value && typeof value === "object") {
59
+ if ("name" in value) {
60
+ const name = value.name;
61
+ if (name === "docx") {
62
+ return "Document structure appears valid but contains invalid component configurations. Check each component for errors.";
63
+ }
64
+ return `Unknown document name '${name}'. Expected 'docx'.`;
65
+ }
66
+ const valueAny = value;
67
+ if ("children" in value && Array.isArray(valueAny.children)) {
68
+ return `Document is missing required 'name' field. Add "name": "docx" at the root level.`;
69
+ }
70
+ if ("name" in value || "styles" in value) {
71
+ return "This appears to be a theme configuration. Use --type theme or ensure proper document structure.";
72
+ }
73
+ }
74
+ return 'Invalid document structure. Expected a document with name="docx" or a theme configuration.';
75
+ }
76
+ if (path.includes("/children/")) {
77
+ if (value && typeof value === "object" && "name" in value) {
78
+ const componentType = value.name;
79
+ return `Invalid component configuration for type '${componentType}'. Check that all required fields are present and correctly formatted.`;
80
+ }
81
+ return 'Invalid component structure. Each component must have a "name" field and valid configuration.';
82
+ }
83
+ return `Value at ${path} doesn't match any of the expected formats. Check the structure and required fields.`;
84
+ }
85
+ function generateAdditionalPropertiesMessage(error) {
86
+ const path = error.path || "root";
87
+ const value = error.value;
88
+ if (typeof value === "object" && value !== null) {
89
+ const schema = error.schema;
90
+ if (schema && isObjectSchema(schema)) {
91
+ const knownProps = getObjectSchemaPropertyNames(schema);
92
+ const actualProps = Object.keys(value);
93
+ const unknownProps = actualProps.filter((p) => !knownProps.includes(p));
94
+ if (unknownProps.length > 0) {
95
+ return `Unknown properties at ${path}: ${unknownProps.join(", ")}. Allowed properties are: ${knownProps.join(", ")}`;
96
+ }
97
+ }
98
+ }
99
+ return `Additional properties not allowed at ${path}. Check for typos or unsupported fields.`;
100
+ }
101
+ function generateRequiredPropertyMessage(error) {
102
+ const path = error.path || "root";
103
+ const match = error.message?.match(/Required property '([^']+)'/);
104
+ if (match) {
105
+ const propName = match[1];
106
+ return `Missing required field '${propName}' at ${path}. This field is mandatory for this configuration.`;
107
+ }
108
+ return `Missing required property at ${path}. Check that all mandatory fields are present.`;
109
+ }
110
+ function generateTypeMismatchMessage(error) {
111
+ const path = error.path || "root";
112
+ const expectedType = String(error.type);
113
+ const actualType = Array.isArray(error.value) ? "array" : typeof error.value;
114
+ if (path.includes("alignment")) {
115
+ return `Invalid alignment value at ${path}. Expected one of: left, center, right, justify`;
116
+ }
117
+ if (path.includes("color")) {
118
+ return `Invalid color value at ${path}. Use hex format (#RRGGBB), rgb(r,g,b), or a named color`;
119
+ }
120
+ if (path.includes("fontSize") || path.includes("size")) {
121
+ return `Invalid size value at ${path}. Expected a number (in points)`;
122
+ }
123
+ if (path.includes("margin") || path.includes("padding") || path.includes("spacing")) {
124
+ return `Invalid spacing value at ${path}. Expected a number or spacing object with top/bottom/left/right`;
125
+ }
126
+ return `Type mismatch at ${path}: Expected ${expectedType} but got ${actualType}`;
127
+ }
128
+ function generateLiteralErrorMessage(error) {
129
+ const path = error.path || "root";
130
+ const expected = error.schema ? JSON.stringify(getLiteralValue(error.schema)) : "specific value";
131
+ const actual = JSON.stringify(error.value);
132
+ return `Invalid value at ${path}: Expected exactly ${expected} but got ${actual}`;
133
+ }
134
+ function generatePatternErrorMessage(error) {
135
+ const path = error.path || "root";
136
+ if (path.includes("email")) {
137
+ return `Invalid email format at ${path}. Use format: user@example.com`;
138
+ }
139
+ if (path.includes("url") || path.includes("link")) {
140
+ return `Invalid URL format at ${path}. Use format: https://example.com`;
141
+ }
142
+ if (path.includes("date")) {
143
+ return `Invalid date format at ${path}. Use ISO format: YYYY-MM-DD`;
144
+ }
145
+ if (path.includes("phone")) {
146
+ return `Invalid phone number format at ${path}`;
147
+ }
148
+ return `Value at ${path} doesn't match the required pattern`;
149
+ }
150
+ function transformValueError(error, jsonString, config) {
151
+ const formatterConfig = createErrorConfig(config);
152
+ const enhancedMessage = generateEnhancedMessage(error, formatterConfig);
153
+ const baseError = {
154
+ path: error.path || "root",
155
+ message: formatErrorMessage(
156
+ enhancedMessage || error.message,
157
+ formatterConfig
158
+ ),
159
+ code: String(error.type || "validation_error"),
160
+ value: error.value
161
+ };
162
+ if (formatterConfig.includeSuggestions) {
163
+ const suggestion = getSuggestion(error, formatterConfig);
164
+ if (suggestion) {
165
+ baseError.suggestion = formatErrorMessage(suggestion, formatterConfig);
166
+ }
167
+ }
168
+ if (jsonString && error.path) {
169
+ const position = calculatePosition(jsonString, error.path);
170
+ if (position) {
171
+ baseError.line = position.line;
172
+ baseError.column = position.column;
173
+ }
174
+ }
175
+ return baseError;
176
+ }
177
+ function transformValueErrors(errors, options) {
178
+ const maxErrors = options?.maxErrors ?? Number.MAX_SAFE_INTEGER;
179
+ const result = [];
180
+ const seenPaths = /* @__PURE__ */ new Set();
181
+ for (const error of errors) {
182
+ if (result.length >= maxErrors) break;
183
+ const errorKey = `${error.path}:${error.type}`;
184
+ if (!seenPaths.has(errorKey)) {
185
+ seenPaths.add(errorKey);
186
+ result.push(transformValueError(error, options?.jsonString, void 0));
187
+ }
188
+ }
189
+ return result;
190
+ }
191
+ function calculatePosition(jsonString, path) {
192
+ try {
193
+ const pathParts = path.split("/").filter(Boolean);
194
+ if (pathParts.length === 0) {
195
+ return { line: 1, column: 1 };
196
+ }
197
+ const lastPart = pathParts[pathParts.length - 1];
198
+ const searchPattern = `"${lastPart}"`;
199
+ const index = jsonString.indexOf(searchPattern);
200
+ if (index === -1) {
201
+ return { line: 1, column: 1 };
202
+ }
203
+ const beforeError = jsonString.substring(0, index);
204
+ const lines = beforeError.split("\n");
205
+ const line = lines.length;
206
+ const column = lines[lines.length - 1].length + 1;
207
+ return { line, column };
208
+ } catch {
209
+ return null;
210
+ }
211
+ }
212
+ function getSuggestion(error, config) {
213
+ const { type, path, value } = error;
214
+ const typeStr = String(type);
215
+ if (typeStr === "62" || typeStr === "union") {
216
+ if (path === "root" || path === "/") {
217
+ if (value && typeof value === "object") {
218
+ if (!("name" in value)) {
219
+ const msg = 'Add a "name" field with value "docx" for documents';
220
+ return config.includeEmojis ? `${ERROR_EMOJIS.FIX} ${msg}` : msg;
221
+ }
222
+ if ("props" in value && typeof value.props === "object") {
223
+ const knownFields = getObjectSchemaPropertyNames(ReportPropsSchema);
224
+ return `Review the props section for unsupported fields. Allowed fields: ${knownFields.join(", ")}`;
225
+ }
226
+ }
227
+ return 'Ensure the document has proper structure: { "name": "docx", "props": {...}, "children": [...] }';
228
+ }
229
+ if (path?.includes("/children/")) {
230
+ return 'Check that the component has a valid "name" and all required fields for that component type';
231
+ }
232
+ return "Review the structure and ensure all required fields are present with correct types";
233
+ }
234
+ if (error.message?.includes("additionalProperties")) {
235
+ return "Remove any unknown or unsupported fields. Check documentation for allowed properties.";
236
+ }
237
+ if (error.message?.includes("Required property")) {
238
+ return "Add the missing required field to fix this error";
239
+ }
240
+ if (typeStr === "string") {
241
+ if (path?.includes("alignment")) {
242
+ return "Use one of: left, center, right, justify";
243
+ }
244
+ if (path?.includes("color")) {
245
+ return "Use a valid color format (hex: #RRGGBB, rgb: rgb(r,g,b), or named color)";
246
+ }
247
+ return "Provide a text string value";
248
+ }
249
+ if (typeStr === "number") {
250
+ if (path?.includes("fontSize") || path?.includes("size")) {
251
+ return "Use a number in points (e.g., 12, 14, 16)";
252
+ }
253
+ if (path?.includes("margin") || path?.includes("padding")) {
254
+ return "Use a number for spacing in points";
255
+ }
256
+ return "Provide a numeric value";
257
+ }
258
+ if (typeStr === "boolean") {
259
+ return "Use true or false (without quotes)";
260
+ }
261
+ if (typeStr === "array") {
262
+ if (path?.includes("children") || path?.includes("modules")) {
263
+ return 'Provide an array of component objects, each with a "name" field';
264
+ }
265
+ return "Provide an array/list of values using square brackets []";
266
+ }
267
+ if (typeStr === "object") {
268
+ return "Provide an object with key-value pairs using curly braces {}";
269
+ }
270
+ if (typeStr === "literal") {
271
+ const expected = error.schema ? JSON.stringify(getLiteralValue(error.schema)) : "specific value";
272
+ return `Use exactly this value: ${expected}`;
273
+ }
274
+ if (typeStr === "pattern" || typeStr === "RegExp") {
275
+ if (path?.includes("email")) {
276
+ return "Use valid email format: user@example.com";
277
+ }
278
+ if (path?.includes("url")) {
279
+ return "Use valid URL format: https://example.com";
280
+ }
281
+ if (path?.includes("date")) {
282
+ return "Use ISO date format: YYYY-MM-DD";
283
+ }
284
+ return "Ensure the value matches the required format";
285
+ }
286
+ if (path?.includes("name") || path?.includes("type")) {
287
+ return "Use a valid component name: docx, section, columns, heading, paragraph, image, statistic, table, header, footer, list, toc, text-box, or highcharts";
288
+ }
289
+ return void 0;
290
+ }
291
+
292
+ // src/validation/unified/deep-validator.ts
293
+ import { Value } from "@sinclair/typebox/value";
294
+ var COMPONENT_SCHEMAS = {
295
+ report: ReportPropsSchema,
296
+ section: SectionPropsSchema,
297
+ heading: HeadingPropsSchema,
298
+ paragraph: ParagraphPropsSchema,
299
+ table: TablePropsSchema,
300
+ image: ImagePropsSchema,
301
+ list: ListPropsSchema,
302
+ statistic: StatisticPropsSchema,
303
+ columns: ColumnsPropsSchema,
304
+ header: HeaderPropsSchema,
305
+ footer: FooterPropsSchema,
306
+ custom: CustomComponentDefinitionSchema
307
+ };
308
+ function deepValidateDocument(data) {
309
+ const allErrors = [];
310
+ if (!data || typeof data !== "object") {
311
+ allErrors.push({
312
+ path: "root",
313
+ message: "Document must be an object",
314
+ code: "invalid_type"
315
+ });
316
+ return allErrors;
317
+ }
318
+ if (!data.name) {
319
+ allErrors.push({
320
+ path: "/name",
321
+ message: 'Missing required field "name"',
322
+ code: "required_property"
323
+ });
324
+ } else if (data.name !== "docx") {
325
+ allErrors.push({
326
+ path: "/name",
327
+ message: `Invalid name "${data.name}". Expected "docx"`,
328
+ code: "invalid_value"
329
+ });
330
+ }
331
+ if (data.name === "docx" && data.props) {
332
+ const propsErrors = validateComponentProps("docx", data.props, "/props");
333
+ allErrors.push(...propsErrors);
334
+ }
335
+ if (!data.children) {
336
+ allErrors.push({
337
+ path: "/children",
338
+ message: 'Missing required field "children"',
339
+ code: "required_property"
340
+ });
341
+ } else if (!Array.isArray(data.children)) {
342
+ allErrors.push({
343
+ path: "/children",
344
+ message: 'Field "children" must be an array',
345
+ code: "invalid_type"
346
+ });
347
+ } else {
348
+ data.children.forEach((child, index) => {
349
+ const childPath = `/children/${index}`;
350
+ if (!child || typeof child !== "object") {
351
+ allErrors.push({
352
+ path: childPath,
353
+ message: "Component must be an object",
354
+ code: "invalid_type"
355
+ });
356
+ return;
357
+ }
358
+ if (!child.name) {
359
+ allErrors.push({
360
+ path: `${childPath}/name`,
361
+ message: 'Component missing required field "name"',
362
+ code: "required_property"
363
+ });
364
+ return;
365
+ }
366
+ if (child.props) {
367
+ const componentErrors = validateComponentProps(
368
+ child.name,
369
+ child.props,
370
+ `${childPath}/props`
371
+ );
372
+ allErrors.push(...componentErrors);
373
+ } else if (child.name !== "custom") {
374
+ allErrors.push({
375
+ path: `${childPath}/props`,
376
+ message: 'Component missing required field "props"',
377
+ code: "required_property"
378
+ });
379
+ }
380
+ if (child.name === "section" && child.children) {
381
+ if (!Array.isArray(child.children)) {
382
+ allErrors.push({
383
+ path: `${childPath}/children`,
384
+ message: "Section children must be an array",
385
+ code: "invalid_type"
386
+ });
387
+ } else {
388
+ child.children.forEach((nestedChild, nestedIndex) => {
389
+ const nestedChildPath = `${childPath}/children/${nestedIndex}`;
390
+ if (!nestedChild || typeof nestedChild !== "object") {
391
+ allErrors.push({
392
+ path: nestedChildPath,
393
+ message: "Nested component must be an object",
394
+ code: "invalid_type"
395
+ });
396
+ return;
397
+ }
398
+ if (!nestedChild.name) {
399
+ allErrors.push({
400
+ path: `${nestedChildPath}/name`,
401
+ message: 'Nested component missing required field "name"',
402
+ code: "required_property"
403
+ });
404
+ } else {
405
+ if (nestedChild.props) {
406
+ const nestedErrors = validateComponentProps(
407
+ nestedChild.name,
408
+ nestedChild.props,
409
+ `${nestedChildPath}/props`
410
+ );
411
+ allErrors.push(...nestedErrors);
412
+ } else if (nestedChild.name !== "custom") {
413
+ allErrors.push({
414
+ path: `${nestedChildPath}/props`,
415
+ message: 'Component missing required field "props"',
416
+ code: "required_property"
417
+ });
418
+ }
419
+ }
420
+ });
421
+ }
422
+ }
423
+ });
424
+ }
425
+ return allErrors;
426
+ }
427
+ function validateComponentProps(componentName, props, basePath) {
428
+ const errors = [];
429
+ const schema = COMPONENT_SCHEMAS[componentName];
430
+ if (!schema) {
431
+ errors.push({
432
+ path: basePath.replace("/props", "/name"),
433
+ message: `Unknown component "${componentName}"`,
434
+ code: "unknown_component"
435
+ });
436
+ return errors;
437
+ }
438
+ if (!Value.Check(schema, props)) {
439
+ const valueErrors = [...Value.Errors(schema, props)];
440
+ const transformedErrors = transformValueErrors(valueErrors, {
441
+ maxErrors: 100
442
+ });
443
+ transformedErrors.forEach((error) => {
444
+ const fullPath = error.path === "root" ? basePath : `${basePath}${error.path.startsWith("/") ? error.path : "/" + error.path}`;
445
+ errors.push({
446
+ ...error,
447
+ path: fullPath
448
+ });
449
+ });
450
+ }
451
+ return errors;
452
+ }
453
+ function comprehensiveValidateDocument(data, existingErrors = []) {
454
+ const deepErrors = deepValidateDocument(data);
455
+ if (deepErrors.length > 0) {
456
+ const filteredExisting = existingErrors.filter(
457
+ (e) => !e.message.includes("invalid module configurations") && !e.message.includes("invalid component configurations")
458
+ );
459
+ const allErrors = [...filteredExisting, ...deepErrors];
460
+ const uniqueErrors = deduplicateErrors(allErrors);
461
+ return uniqueErrors;
462
+ }
463
+ return existingErrors;
464
+ }
465
+ function deduplicateErrors(errors) {
466
+ const seen = /* @__PURE__ */ new Set();
467
+ const unique = [];
468
+ for (const error of errors) {
469
+ const key = `${error.path}:${error.message}`;
470
+ if (!seen.has(key)) {
471
+ seen.add(key);
472
+ unique.push(error);
473
+ }
474
+ }
475
+ return unique;
476
+ }
477
+
478
+ // src/validation/unified/base-validator.ts
479
+ import { Value as Value2 } from "@sinclair/typebox/value";
480
+ import {
481
+ transformValueErrors as transformValueErrors2,
482
+ createJsonParseError,
483
+ formatErrorSummary
484
+ } from "@json-to-office/shared";
485
+ function validateAgainstSchema(schema, data, options) {
486
+ try {
487
+ if (!Value2.Check(schema, data)) {
488
+ const errors = [...Value2.Errors(schema, data)];
489
+ const transformedErrors = transformValueErrors2(errors, {
490
+ jsonString: options?.jsonString,
491
+ maxErrors: options?.maxErrors || 100
492
+ // Default to 100 if not specified
493
+ });
494
+ return {
495
+ valid: false,
496
+ errors: transformedErrors
497
+ };
498
+ }
499
+ let processedData = data;
500
+ if (options?.clean) {
501
+ processedData = Value2.Clean(schema, Value2.Clone(processedData));
502
+ }
503
+ if (options?.applyDefaults) {
504
+ processedData = Value2.Default(schema, processedData);
505
+ }
506
+ return {
507
+ valid: true,
508
+ data: processedData
509
+ };
510
+ } catch (error) {
511
+ return {
512
+ valid: false,
513
+ errors: [
514
+ {
515
+ path: "root",
516
+ message: error instanceof Error ? error.message : "Unknown validation error",
517
+ code: "validation_exception"
518
+ }
519
+ ]
520
+ };
521
+ }
522
+ }
523
+ function validateJson(schema, jsonInput, options) {
524
+ if (typeof jsonInput === "string") {
525
+ if (!jsonInput.trim()) {
526
+ return {
527
+ valid: false,
528
+ errors: [
529
+ {
530
+ path: "root",
531
+ message: "Input must be a non-empty string",
532
+ code: "empty_input"
533
+ }
534
+ ],
535
+ isJsonError: true
536
+ };
537
+ }
538
+ let parsed;
539
+ try {
540
+ parsed = JSON.parse(jsonInput);
541
+ } catch (error) {
542
+ if (error instanceof Error) {
543
+ return {
544
+ valid: false,
545
+ errors: [createJsonParseError(error, jsonInput)],
546
+ isJsonError: true
547
+ };
548
+ }
549
+ return {
550
+ valid: false,
551
+ errors: [
552
+ {
553
+ path: "root",
554
+ message: "Failed to parse JSON",
555
+ code: "json_parse_error"
556
+ }
557
+ ],
558
+ isJsonError: true
559
+ };
560
+ }
561
+ const result2 = validateAgainstSchema(schema, parsed, {
562
+ ...options,
563
+ jsonString: jsonInput,
564
+ calculatePosition: true
565
+ });
566
+ return {
567
+ ...result2,
568
+ parsed,
569
+ isJsonError: false
570
+ };
571
+ }
572
+ const result = validateAgainstSchema(schema, jsonInput, options);
573
+ return {
574
+ ...result,
575
+ parsed: jsonInput,
576
+ isJsonError: false
577
+ };
578
+ }
579
+ function validateBatch(schema, items, options) {
580
+ const results = [];
581
+ const allErrors = [];
582
+ let hasErrors = false;
583
+ for (let i = 0; i < items.length; i++) {
584
+ const result = validateAgainstSchema(schema, items[i], options);
585
+ if (result.valid && result.data) {
586
+ results.push(result.data);
587
+ } else {
588
+ hasErrors = true;
589
+ if (result.errors) {
590
+ const prefixedErrors = result.errors.map((e) => ({
591
+ ...e,
592
+ path: `[${i}]${e.path ? "/" + e.path : ""}`
593
+ }));
594
+ allErrors.push(...prefixedErrors);
595
+ }
596
+ }
597
+ if (options?.maxErrors && allErrors.length >= options.maxErrors) {
598
+ break;
599
+ }
600
+ }
601
+ if (hasErrors) {
602
+ return {
603
+ valid: false,
604
+ errors: allErrors.slice(0, options?.maxErrors)
605
+ };
606
+ }
607
+ return {
608
+ valid: true,
609
+ data: results
610
+ };
611
+ }
612
+ function validateWithEnhancement(schema, data, enhancer, options) {
613
+ const result = validateAgainstSchema(schema, data, options);
614
+ if (!result.valid && result.errors) {
615
+ result.errors = enhancer(result.errors);
616
+ }
617
+ return result;
618
+ }
619
+ function createValidator(schema, defaultOptions) {
620
+ return (data, options) => {
621
+ return validateAgainstSchema(schema, data, {
622
+ ...defaultOptions,
623
+ ...options
624
+ });
625
+ };
626
+ }
627
+ function createJsonValidator(schema, defaultOptions) {
628
+ return (jsonInput, options) => {
629
+ return validateJson(schema, jsonInput, {
630
+ ...defaultOptions,
631
+ ...options
632
+ });
633
+ };
634
+ }
635
+ function isValidationSuccess(result) {
636
+ return result.valid === true && result.data !== void 0;
637
+ }
638
+ function getValidationSummary(result) {
639
+ if (result.valid) {
640
+ return "Validation successful";
641
+ }
642
+ if (!result.errors || result.errors.length === 0) {
643
+ return "Validation failed with unknown error";
644
+ }
645
+ return formatErrorSummary(result.errors);
646
+ }
647
+
648
+ // src/validation/unified/document-validator.ts
649
+ import { extractStandardComponentNames } from "@json-to-office/shared";
650
+ var JsonComponentDefinitionSchema = ComponentDefinitionSchema;
651
+ function validateDocument(data, options) {
652
+ const result = validateAgainstSchema(ComponentDefinitionSchema, data, options);
653
+ let finalErrors = result.errors || [];
654
+ if (!result.valid && data) {
655
+ finalErrors = comprehensiveValidateDocument(data, result.errors);
656
+ }
657
+ const documentResult = {
658
+ ...result,
659
+ documentType: "docx",
660
+ errors: finalErrors
661
+ // Use the comprehensive errors
662
+ };
663
+ if (result.valid && result.data) {
664
+ const doc = result.data;
665
+ if (doc.children && Array.isArray(doc.children)) {
666
+ const hasCustom = doc.children.some(
667
+ (c) => !isStandardComponentName(c.name)
668
+ );
669
+ documentResult.hasCustomComponents = hasCustom;
670
+ }
671
+ }
672
+ return documentResult;
673
+ }
674
+ function validateJsonDocument(jsonInput, options) {
675
+ const result = validateJson(JsonComponentDefinitionSchema, jsonInput, options);
676
+ let finalErrors = result.errors || [];
677
+ if (!result.valid && result.parsed) {
678
+ finalErrors = comprehensiveValidateDocument(result.parsed, result.errors);
679
+ }
680
+ const documentResult = {
681
+ ...result,
682
+ documentType: "docx",
683
+ errors: finalErrors
684
+ // Use the comprehensive errors
685
+ };
686
+ if (result.valid && result.data) {
687
+ const doc = result.data;
688
+ if (doc.children && Array.isArray(doc.children)) {
689
+ const hasCustom = doc.children.some(
690
+ (c) => !isStandardComponentName(c.name)
691
+ );
692
+ documentResult.hasCustomComponents = hasCustom;
693
+ }
694
+ }
695
+ return documentResult;
696
+ }
697
+ function isValidDocument(result) {
698
+ return result.valid === true && result.data !== void 0;
699
+ }
700
+ function getStandardComponentNames() {
701
+ return extractStandardComponentNames(StandardComponentDefinitionSchema);
702
+ }
703
+ function isStandardComponentName(name) {
704
+ const standardNames = getStandardComponentNames();
705
+ return standardNames.includes(name);
706
+ }
707
+ function createDocumentValidator(defaultOptions) {
708
+ return {
709
+ validate: (data, options) => validateDocument(data, { ...defaultOptions, ...options }),
710
+ validateJson: (jsonInput, options) => validateJsonDocument(jsonInput, { ...defaultOptions, ...options })
711
+ };
712
+ }
713
+ var documentValidator = createDocumentValidator({
714
+ clean: true,
715
+ applyDefaults: true,
716
+ maxErrors: 100
717
+ // Collect up to 100 errors to show all validation issues
718
+ });
719
+ var strictDocumentValidator = createDocumentValidator({
720
+ clean: false,
721
+ applyDefaults: false,
722
+ maxErrors: 100
723
+ // Increased from 10 to show more errors
724
+ });
725
+ var validateJsonComponent = validateJsonDocument;
726
+ var validateDocumentWithSchema = validateDocument;
727
+
728
+ export {
729
+ transformValueError,
730
+ transformValueErrors,
731
+ deepValidateDocument,
732
+ comprehensiveValidateDocument,
733
+ validateAgainstSchema,
734
+ validateJson,
735
+ validateBatch,
736
+ validateWithEnhancement,
737
+ createValidator,
738
+ createJsonValidator,
739
+ isValidationSuccess,
740
+ getValidationSummary,
741
+ validateDocument,
742
+ validateJsonDocument,
743
+ isValidDocument,
744
+ createDocumentValidator,
745
+ documentValidator,
746
+ strictDocumentValidator,
747
+ validateJsonComponent,
748
+ validateDocumentWithSchema
749
+ };
750
+ //# sourceMappingURL=chunk-LOE6BZQG.js.map