@json-to-office/core-docx 0.16.0 → 0.17.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.
@@ -1,6 +1,6 @@
1
1
  import { type ThemeConfig } from '../styles';
2
2
  import type { ServicesConfig, FontRuntimeOpts } from '@json-to-office/shared';
3
- import type { DocumentGeneratorBuilder } from './types';
3
+ import type { DocumentGeneratorBuilder, GenerationValidationOptions } from './types';
4
4
  /**
5
5
  * Options for creating a document generator
6
6
  */
@@ -17,6 +17,12 @@ export interface DocumentGeneratorOptions {
17
17
  services?: ServicesConfig;
18
18
  /** Font resolution options — extraEntries, Google Fonts config, onResolved hook. */
19
19
  fonts?: FontRuntimeOpts;
20
+ /**
21
+ * Default validation behavior for every generate/generateBuffer/generateFile
22
+ * call. Per-call `options.validation` overrides these. Validation is on by
23
+ * default; pass `{ enabled: false }` to opt out.
24
+ */
25
+ validation?: GenerationValidationOptions;
20
26
  }
21
27
  /**
22
28
  * Create a document generator with chainable component registration.
@@ -1 +1 @@
1
- {"version":3,"file":"createDocumentGenerator.d.ts","sourceRoot":"","sources":["../../src/plugin/createDocumentGenerator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAwB,MAAM,WAAW,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9E,OAAO,KAAK,EAEV,wBAAwB,EAOzB,MAAM,SAAS,CAAC;AAejB;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,kEAAkE;IAClE,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3C,4CAA4C;IAC5C,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,oFAAoF;IACpF,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAiiBD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,wBAAwB,CAAC,SAAS,EAAE,CAAC,CAavC"}
1
+ {"version":3,"file":"createDocumentGenerator.d.ts","sourceRoot":"","sources":["../../src/plugin/createDocumentGenerator.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAwB,MAAM,WAAW,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAG9E,OAAO,KAAK,EAEV,wBAAwB,EAOxB,2BAA2B,EAC5B,MAAM,SAAS,CAAC;AAejB;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,kEAAkE;IAClE,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,kFAAkF;IAClF,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3C,4CAA4C;IAC5C,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qEAAqE;IACrE,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,oFAAoF;IACpF,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB;;;;OAIG;IACH,UAAU,CAAC,EAAE,2BAA2B,CAAC;CAC1C;AAojBD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,wBAAwB,GAChC,wBAAwB,CAAC,SAAS,EAAE,CAAC,CAcvC"}
@@ -1904,22 +1904,20 @@ import {
1904
1904
  ComponentValidationError as ComponentValidationError2,
1905
1905
  UnknownPreservedComponentError
1906
1906
  } from "@json-to-office/shared/plugin";
1907
- function validateComponentProps(schema, props, componentName) {
1907
+ function validateComponentProps(schema, props, componentName, opts) {
1908
1908
  return validateCustomComponentProps(schema.propsSchema, props, {
1909
- clean: true,
1910
- applyDefaults: true,
1909
+ clean: opts?.clean ?? true,
1910
+ applyDefaults: opts?.applyDefaults ?? true,
1911
1911
  componentName
1912
1912
  });
1913
1913
  }
1914
- function validateDocument(document, customComponents) {
1914
+ function validateDocument(document, customComponents, options) {
1915
+ const knownCustomNames = new Set(customComponents.map((c) => c.name));
1915
1916
  const documentResult = validateDocumentUnified(document, {
1916
- clean: true,
1917
- applyDefaults: true
1917
+ knownCustomNames,
1918
+ allowUnknownFields: options?.allowUnknownFields
1918
1919
  });
1919
- if (!documentResult.valid) {
1920
- return { ...documentResult, success: false };
1921
- }
1922
- const errors = [];
1920
+ const errors = [...documentResult.errors || []];
1923
1921
  function validateComponents(components, pathPrefix = "children") {
1924
1922
  components.forEach((componentData, index) => {
1925
1923
  const customComponent = customComponents.find(
@@ -1936,7 +1934,9 @@ function validateDocument(document, customComponents) {
1936
1934
  const validation = validateComponentProps(
1937
1935
  versionEntry,
1938
1936
  componentData.props,
1939
- customComponent.name
1937
+ customComponent.name,
1938
+ // Reject unknown custom props unless the caller allows them.
1939
+ { clean: options?.allowUnknownFields === true }
1940
1940
  );
1941
1941
  if (!validation.valid && validation.errors) {
1942
1942
  const indexedErrors = validation.errors.map(
@@ -7287,10 +7287,26 @@ function createBuilderImpl(state) {
7287
7287
  try {
7288
7288
  const preserveSet = resolvePreserveSet(options);
7289
7289
  const internalDocument = document;
7290
- validateDocument(
7291
- internalDocument,
7292
- state.components
7293
- );
7290
+ const vOpts = {
7291
+ ...state.validation,
7292
+ ...options?.validation
7293
+ };
7294
+ if (vOpts.enabled !== false) {
7295
+ const result = validateDocument(
7296
+ internalDocument,
7297
+ state.components,
7298
+ { allowUnknownFields: vOpts.allowUnknownFields }
7299
+ );
7300
+ if (!result.valid) {
7301
+ throw new ComponentValidationError2(
7302
+ (result.errors ?? []).map((e) => ({
7303
+ path: e.path ?? "",
7304
+ message: e.message
7305
+ })),
7306
+ internalDocument
7307
+ );
7308
+ }
7309
+ }
7294
7310
  const baseThemeName = internalDocument.props.theme || "minimal";
7295
7311
  const docTheme = resolveDocumentTheme(baseThemeName);
7296
7312
  const warnings = [];
@@ -7384,21 +7400,21 @@ function createBuilderImpl(state) {
7384
7400
  function validate(document) {
7385
7401
  try {
7386
7402
  const internalDocument = document;
7387
- validateDocument(
7403
+ const result = validateDocument(
7388
7404
  internalDocument,
7389
7405
  state.components
7390
7406
  );
7391
- return { valid: true };
7392
- } catch (error) {
7393
- if (error instanceof ComponentValidationError2) {
7394
- return {
7395
- valid: false,
7396
- errors: error.errors.map((e) => ({
7397
- path: e.path,
7398
- message: e.message
7399
- }))
7400
- };
7407
+ if (result.valid) {
7408
+ return { valid: true };
7401
7409
  }
7410
+ return {
7411
+ valid: false,
7412
+ errors: (result.errors ?? []).map((e) => ({
7413
+ path: e.path ?? "",
7414
+ message: e.message
7415
+ }))
7416
+ };
7417
+ } catch (error) {
7402
7418
  return {
7403
7419
  valid: false,
7404
7420
  errors: [
@@ -7448,7 +7464,8 @@ function createDocumentGenerator(options) {
7448
7464
  debug: options.debug ?? false,
7449
7465
  enableCache: options.enableCache ?? false,
7450
7466
  services: options.services,
7451
- fonts: options.fonts
7467
+ fonts: options.fonts,
7468
+ validation: options.validation
7452
7469
  };
7453
7470
  return createBuilderImpl(initialState);
7454
7471
  }