@plyaz/types 1.25.2 → 1.27.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.
@@ -4,8 +4,8 @@
4
4
  */
5
5
  import type { LoggerInterface } from '../logger/types';
6
6
  import type { StorageErrorCode } from '../errors/codes';
7
- import type { ADAPTER_HEALTH_STATUS, ENTITY_TYPE, FILE_ACCESS_LEVEL, FILE_CATEGORY, OUTPUT_FORMAT, PATH_GENERATION_STRATEGY, RETRY_STRATEGY, STORAGE_ADAPTER_TYPE, STORAGE_DEVICE_TYPE, STORAGE_EVENT_TYPE, STORAGE_QUEUE_PRIORITY, STORAGE_RENDERER_TYPE, TEMPLATE_OUTPUT_FORMAT, UPLOAD_STATUS, DOCUMENT_TYPE, MEDIA_ENTITY, STORAGE_VISIBILITY, STORAGE_ENVIRONMENT, BUSINESS_MODEL, ORGANIZATION_TIER, BUCKET_PURPOSE, PATH_STRATEGY } from './enums';
8
- import type { StorageSoftDeleteMetadata, StorageDeleteComplianceOptions, StorageComplianceConfig } from './compliance';
7
+ import type { ADAPTER_HEALTH_STATUS, ENTITY_TYPE, FILE_ACCESS_LEVEL, FILE_CATEGORY, OUTPUT_FORMAT, PATH_GENERATION_STRATEGY, RETRY_STRATEGY, STORAGE_ADAPTER_TYPE, STORAGE_DEVICE_TYPE, STORAGE_EVENT_TYPE, STORAGE_QUEUE_PRIORITY, STORAGE_RENDERER_TYPE, TEMPLATE_OUTPUT_FORMAT, UPLOAD_STATUS, DOCUMENT_TYPE, MEDIA_ENTITY, STORAGE_VISIBILITY, STORAGE_ENVIRONMENT, BUSINESS_MODEL, ORGANIZATION_TIER, BUCKET_PURPOSE, PATH_STRATEGY, TEMPLATE_VARIABLE_TYPE, TEMPLATE_DOCUMENT_TYPE } from './enums';
8
+ import type { StorageSoftDeleteMetadata, StorageDeleteComplianceOptions, StorageComplianceConfig, StorageRetentionPolicy } from './compliance';
9
9
  import type { PluginRegistryConfig, StorageKnownPluginName, StoragePlugin } from './plugins';
10
10
  import type { StorageBaseWebhookAdapterConfig, StorageWebhookAdapter, StorageWebhookManagerConfig } from './webhooks';
11
11
  /**
@@ -205,6 +205,11 @@ export interface UploadParams {
205
205
  excelOptions?: StorageExcelRenderOptions;
206
206
  /** Word render options (when outputFormat === 'word') */
207
207
  wordOptions?: StorageWordRenderOptions;
208
+ /**
209
+ * Validation options for template data
210
+ * When provided, validates templateData against schema before rendering
211
+ */
212
+ validation?: TemplateValidationOptions;
208
213
  /** Use queue for this operation (requires queue to be enabled in service config) */
209
214
  useQueue?: boolean;
210
215
  /** Queue priority (only used if useQueue is true) */
@@ -469,6 +474,64 @@ export interface StorageResolveVariantsOptions {
469
474
  /** Generate for all devices (ignores device parameter) */
470
475
  allDevices?: boolean;
471
476
  }
477
+ /**
478
+ * Custom validation function type (Zod-like)
479
+ * Can be a Zod schema, custom validator function, or any object with parse/safeParse
480
+ */
481
+ export interface CustomValidationSchema {
482
+ /**
483
+ * Parse method - throws on validation failure
484
+ * Compatible with Zod's parse() method
485
+ */
486
+ parse?: (data: unknown) => unknown;
487
+ /**
488
+ * Safe parse method - returns result object instead of throwing
489
+ * Compatible with Zod's safeParse() method
490
+ */
491
+ safeParse?: (data: unknown) => {
492
+ success: boolean;
493
+ data?: unknown;
494
+ error?: {
495
+ issues?: Array<{
496
+ path: (string | number)[];
497
+ message: string;
498
+ }>;
499
+ };
500
+ };
501
+ /**
502
+ * Custom validation function
503
+ * Returns validation result with errors and coerced values
504
+ */
505
+ validate?: (data: Record<string, unknown>) => TemplateVariableValidationResult;
506
+ }
507
+ /**
508
+ * Validation options for template rendering
509
+ */
510
+ export interface TemplateValidationOptions {
511
+ /**
512
+ * Enable validation (default: true if schema exists in frontmatter)
513
+ * Set to false to skip validation entirely
514
+ */
515
+ enabled?: boolean;
516
+ /**
517
+ * Strict mode - throw error if validation fails (default: false)
518
+ * When false, continues rendering with warnings
519
+ */
520
+ strict?: boolean;
521
+ /**
522
+ * Custom validation schema (Zod-like)
523
+ * If provided, uses this instead of frontmatter variables
524
+ */
525
+ schema?: CustomValidationSchema;
526
+ /**
527
+ * Additional variable definitions to merge with frontmatter
528
+ */
529
+ additionalVariables?: TemplateVariableDefinition[];
530
+ /**
531
+ * Skip validation for specific fields
532
+ */
533
+ skipFields?: string[];
534
+ }
472
535
  /**
473
536
  * Generate file parameters (to buffer, without uploading)
474
537
  */
@@ -485,6 +548,11 @@ export interface GenerateFileParams {
485
548
  rendererName?: STORAGE_RENDERER_TYPE | string;
486
549
  /** PDF render options */
487
550
  pdfOptions?: PDFRenderOptions;
551
+ /**
552
+ * Validation options
553
+ * When provided, validates templateData against schema before rendering
554
+ */
555
+ validation?: TemplateValidationOptions;
488
556
  }
489
557
  /**
490
558
  * Generate file to path parameters
@@ -530,6 +598,10 @@ export interface UpdateFileParams {
530
598
  skipPlugins?: readonly StorageKnownPluginName[];
531
599
  /** Only run these specific plugins for this operation */
532
600
  onlyPlugins?: readonly StorageKnownPluginName[];
601
+ /**
602
+ * Validation options for template data (if regenerating from template)
603
+ */
604
+ validation?: TemplateValidationOptions;
533
605
  }
534
606
  /**
535
607
  * Validation rule
@@ -1412,6 +1484,25 @@ export interface StorageTemplateFrontmatter {
1412
1484
  /** Base spacing unit (e.g., '4mm') */
1413
1485
  spacingUnit?: string;
1414
1486
  };
1487
+ /** Document type (simple, financial, legal, etc.) */
1488
+ documentType?: TEMPLATE_DOCUMENT_TYPE;
1489
+ /** Calculation configuration for financial documents */
1490
+ calculations?: {
1491
+ enabled: boolean;
1492
+ taxField?: string;
1493
+ discountField?: string;
1494
+ shippingField?: string;
1495
+ itemsField?: string;
1496
+ };
1497
+ /** Variable definitions with types and validation */
1498
+ variables?: TemplateVariableDefinition[];
1499
+ /** Variable groups for UI organization */
1500
+ variableGroups?: Array<{
1501
+ name: string;
1502
+ label: string;
1503
+ description?: string;
1504
+ collapsed?: boolean;
1505
+ }>;
1415
1506
  /** Additional custom fields */
1416
1507
  [key: string]: unknown;
1417
1508
  }
@@ -2277,3 +2368,210 @@ export interface StorageChunkUploadPartResult {
2277
2368
  /** ETag from upload */
2278
2369
  ETag: string;
2279
2370
  }
2371
+ /**
2372
+ * Validation rule for template variables
2373
+ * Supports regex patterns, min/max values, and custom validators
2374
+ */
2375
+ export interface TemplateVariableValidation {
2376
+ /** Whether the variable is required */
2377
+ required?: boolean;
2378
+ /** Regex pattern for string validation */
2379
+ pattern?: string;
2380
+ /** Human-readable pattern description */
2381
+ patternMessage?: string;
2382
+ /** Minimum value (for numbers) or minimum length (for strings) */
2383
+ min?: number;
2384
+ /** Maximum value (for numbers) or maximum length (for strings) */
2385
+ max?: number;
2386
+ /** Minimum length for strings */
2387
+ minLength?: number;
2388
+ /** Maximum length for strings */
2389
+ maxLength?: number;
2390
+ /** Allowed values (for select/multiselect) */
2391
+ enum?: string[];
2392
+ /** Date format (for date type, e.g., 'YYYY-MM-DD') */
2393
+ dateFormat?: string;
2394
+ /** Minimum date (for date type) */
2395
+ minDate?: string;
2396
+ /** Maximum date (for date type) */
2397
+ maxDate?: string;
2398
+ /** Custom error message */
2399
+ errorMessage?: string;
2400
+ }
2401
+ /**
2402
+ * Definition for a template variable
2403
+ * Specifies type, validation rules, and display information
2404
+ */
2405
+ export interface TemplateVariableDefinition {
2406
+ /** Variable name (matches {{variableName}} in template) */
2407
+ name: string;
2408
+ /** Variable type */
2409
+ type: TEMPLATE_VARIABLE_TYPE;
2410
+ /** Human-readable label for UI */
2411
+ label?: string;
2412
+ /** Description/help text */
2413
+ description?: string;
2414
+ /** Default value */
2415
+ defaultValue?: unknown;
2416
+ /** Placeholder text for input */
2417
+ placeholder?: string;
2418
+ /** Validation rules (optional) */
2419
+ validation?: TemplateVariableValidation;
2420
+ /** For ARRAY type: definition of array item structure */
2421
+ items?: TemplateVariableDefinition;
2422
+ /** For OBJECT type: nested variable definitions */
2423
+ properties?: TemplateVariableDefinition[];
2424
+ /** For SELECT/MULTISELECT: list of options */
2425
+ options?: Array<{
2426
+ label: string;
2427
+ value: string;
2428
+ }>;
2429
+ /** Group name for organizing variables in UI */
2430
+ group?: string;
2431
+ /** Display order within group */
2432
+ order?: number;
2433
+ /** Whether this variable is hidden from editor UI */
2434
+ hidden?: boolean;
2435
+ /** Whether this variable is read-only in editor */
2436
+ readOnly?: boolean;
2437
+ /** Conditional visibility expression (e.g., "documentType === 'financial'") */
2438
+ showIf?: string;
2439
+ }
2440
+ /**
2441
+ * Template variable validation result
2442
+ */
2443
+ export interface TemplateVariableValidationResult {
2444
+ /** Whether validation passed */
2445
+ valid: boolean;
2446
+ /** Validation errors (variable name -> error messages) */
2447
+ errors: Record<string, string[]>;
2448
+ /** Warnings (non-blocking issues) */
2449
+ warnings?: Record<string, string[]>;
2450
+ /** Coerced/transformed values (after type coercion) */
2451
+ coercedValues?: Record<string, unknown>;
2452
+ }
2453
+ /**
2454
+ * Extended template frontmatter with variable definitions
2455
+ */
2456
+ export interface TemplateVariablesFrontmatter {
2457
+ /** Document type (simple, financial, legal, etc.) */
2458
+ documentType?: TEMPLATE_DOCUMENT_TYPE;
2459
+ /** Whether calculations are enabled */
2460
+ calculations?: {
2461
+ enabled: boolean;
2462
+ taxField?: string;
2463
+ discountField?: string;
2464
+ shippingField?: string;
2465
+ itemsField?: string;
2466
+ };
2467
+ /** Variable definitions */
2468
+ variables?: TemplateVariableDefinition[];
2469
+ /** Variable groups for UI organization */
2470
+ variableGroups?: Array<{
2471
+ name: string;
2472
+ label: string;
2473
+ description?: string;
2474
+ collapsed?: boolean;
2475
+ }>;
2476
+ }
2477
+ /**
2478
+ * Extended render result with validation information
2479
+ */
2480
+ export interface StorageTemplateRenderResultWithValidation extends StorageTemplateRenderResult {
2481
+ /** Validation result if validation was performed */
2482
+ validationResult?: TemplateVariableValidationResult;
2483
+ }
2484
+ /**
2485
+ * Result from template data validation
2486
+ * @internal Used by TemplateEngine.validateTemplateData
2487
+ */
2488
+ export interface StorageTemplateValidationDataResult {
2489
+ /** Data to use for rendering (may be coerced) */
2490
+ renderData: Record<string, unknown>;
2491
+ /** Validation result if validation was performed */
2492
+ validationResult?: TemplateVariableValidationResult;
2493
+ }
2494
+ /**
2495
+ * Options for rendering a template to HTML
2496
+ * @internal Used by TemplateEngine.renderTemplateToHtml
2497
+ */
2498
+ export interface StorageTemplateRenderToHtmlOptions {
2499
+ /** Cache key for compiled template lookup */
2500
+ cacheKey: string;
2501
+ /** Loaded template entry with frontmatter and body */
2502
+ templateEntry: StorageTemplateCacheEntry;
2503
+ /** Data to render (may be coerced from validation) */
2504
+ renderData: Record<string, unknown>;
2505
+ /** Original data before validation/coercion */
2506
+ originalData: Record<string, unknown>;
2507
+ /** Locale for rendering */
2508
+ locale: string;
2509
+ }
2510
+ /**
2511
+ * Options for building template upload metadata
2512
+ * @internal Used by StorageService.buildTemplateMetadata
2513
+ */
2514
+ export interface StorageTemplateBuildMetadataOptions {
2515
+ /** Original upload parameters */
2516
+ params: UploadParams;
2517
+ /** Generated document buffer */
2518
+ documentBuffer: globalThis.Buffer;
2519
+ /** Renderer that was used */
2520
+ renderer: {
2521
+ name: string;
2522
+ type: string;
2523
+ };
2524
+ /** Locale used for generation */
2525
+ locale: string;
2526
+ /** Output format */
2527
+ outputFormat: OUTPUT_FORMAT;
2528
+ /** Compliance info if applicable */
2529
+ complianceInfo?: StorageRetentionPolicy;
2530
+ }
2531
+ /**
2532
+ * Result from type coercion during validation
2533
+ * @internal Used by TemplateEngine validation methods
2534
+ */
2535
+ export interface StorageTemplateTypeCoercionResult {
2536
+ /** The coerced value (may be same as input if no coercion needed) */
2537
+ coercedValue: unknown;
2538
+ /** Type-related errors */
2539
+ typeErrors: string[];
2540
+ /** Type-related warnings (non-blocking) */
2541
+ typeWarnings: string[];
2542
+ }
2543
+ /**
2544
+ * Invoice line item for calculation helpers
2545
+ */
2546
+ export interface StorageTemplateInvoiceLineItem {
2547
+ /** Quantity of items */
2548
+ quantity: number;
2549
+ /** Unit price per item */
2550
+ unitPrice: number;
2551
+ }
2552
+ /**
2553
+ * Options for invoice total calculation helper
2554
+ */
2555
+ export interface StorageTemplateInvoiceTotalOptions {
2556
+ /** Array of invoice line items */
2557
+ items: StorageTemplateInvoiceLineItem[];
2558
+ /** Tax rate as percentage (0-100) */
2559
+ taxRate?: number;
2560
+ /** Discount percentage (0-100) */
2561
+ discountPercent?: number;
2562
+ /** Shipping cost */
2563
+ shippingCost?: number;
2564
+ /** Calculation type: 'subtotal', 'discount', 'tax', or 'total' */
2565
+ calcType?: 'subtotal' | 'discount' | 'tax' | 'total';
2566
+ }
2567
+ /**
2568
+ * Handlebars helper arguments for invoiceTotal
2569
+ * Handlebars passes arguments positionally, so we need to parse them
2570
+ */
2571
+ export type StorageTemplateInvoiceTotalArgs = [
2572
+ items: StorageTemplateInvoiceLineItem[] | unknown,
2573
+ taxRate?: number | unknown,
2574
+ discountPercent?: number | unknown,
2575
+ shippingCost?: number | unknown,
2576
+ calcType?: string | unknown
2577
+ ];
@@ -222,3 +222,173 @@ export declare const SupabaseWebhookPayloadSchema: z.ZodObject<{
222
222
  }, z.core.$strip>;
223
223
  export type SupabaseWebhookPayload = z.infer<typeof SupabaseWebhookPayloadSchema>;
224
224
  export type SupabaseStorageObject = z.infer<typeof SupabaseStorageObjectSchema>;
225
+ /**
226
+ * ============================================================================
227
+ * Template Variable Schemas
228
+ * ============================================================================
229
+ * Validation schemas for template variable definitions
230
+ */
231
+ /**
232
+ * Template variable types enum for schema validation
233
+ */
234
+ export declare enum TEMPLATE_VARIABLE_TYPE_SCHEMA {
235
+ STRING = "string",
236
+ NUMBER = "number",
237
+ BOOLEAN = "boolean",
238
+ DATE = "date",
239
+ EMAIL = "email",
240
+ PHONE = "phone",
241
+ URL = "url",
242
+ CURRENCY = "currency",
243
+ PERCENTAGE = "percentage",
244
+ ARRAY = "array",
245
+ OBJECT = "object",
246
+ RICHTEXT = "richtext",
247
+ IMAGE = "image",
248
+ SELECT = "select",
249
+ MULTISELECT = "multiselect"
250
+ }
251
+ /**
252
+ * Template document types enum for schema validation
253
+ */
254
+ export declare enum TEMPLATE_DOCUMENT_TYPE_SCHEMA {
255
+ SIMPLE = "simple",
256
+ FINANCIAL = "financial",
257
+ LEGAL = "legal",
258
+ REPORT = "report",
259
+ FORM = "form",
260
+ LETTER = "letter",
261
+ CERTIFICATE = "certificate"
262
+ }
263
+ /**
264
+ * Template variable validation schema
265
+ */
266
+ export declare const TemplateVariableValidationSchema: z.ZodObject<{
267
+ required: z.ZodOptional<z.ZodBoolean>;
268
+ pattern: z.ZodOptional<z.ZodString>;
269
+ patternMessage: z.ZodOptional<z.ZodString>;
270
+ min: z.ZodOptional<z.ZodNumber>;
271
+ max: z.ZodOptional<z.ZodNumber>;
272
+ minLength: z.ZodOptional<z.ZodNumber>;
273
+ maxLength: z.ZodOptional<z.ZodNumber>;
274
+ enum: z.ZodOptional<z.ZodArray<z.ZodString>>;
275
+ dateFormat: z.ZodOptional<z.ZodString>;
276
+ minDate: z.ZodOptional<z.ZodString>;
277
+ maxDate: z.ZodOptional<z.ZodString>;
278
+ errorMessage: z.ZodOptional<z.ZodString>;
279
+ }, z.core.$strip>;
280
+ export type TemplateVariableValidationType = z.infer<typeof TemplateVariableValidationSchema>;
281
+ /**
282
+ * Template variable option schema (for select/multiselect)
283
+ */
284
+ export declare const TemplateVariableOptionSchema: z.ZodObject<{
285
+ label: z.ZodString;
286
+ value: z.ZodString;
287
+ }, z.core.$strip>;
288
+ export type TemplateVariableOptionType = z.infer<typeof TemplateVariableOptionSchema>;
289
+ /**
290
+ * Template variable definition schema (with recursive properties support)
291
+ * Note: Due to Zod limitations with recursive types, we use z.lazy()
292
+ */
293
+ export declare const TemplateVariableDefinitionSchema: z.ZodType<{
294
+ name: string;
295
+ type: TEMPLATE_VARIABLE_TYPE_SCHEMA;
296
+ label?: string;
297
+ description?: string;
298
+ defaultValue?: unknown;
299
+ placeholder?: string;
300
+ validation?: TemplateVariableValidationType;
301
+ items?: unknown;
302
+ properties?: unknown[];
303
+ options?: TemplateVariableOptionType[];
304
+ group?: string;
305
+ order?: number;
306
+ hidden?: boolean;
307
+ readOnly?: boolean;
308
+ showIf?: string;
309
+ }>;
310
+ export type TemplateVariableDefinitionType = z.infer<typeof TemplateVariableDefinitionSchema>;
311
+ /**
312
+ * Variable group schema for UI organization
313
+ */
314
+ export declare const TemplateVariableGroupSchema: z.ZodObject<{
315
+ name: z.ZodString;
316
+ label: z.ZodString;
317
+ description: z.ZodOptional<z.ZodString>;
318
+ collapsed: z.ZodOptional<z.ZodBoolean>;
319
+ }, z.core.$strip>;
320
+ export type TemplateVariableGroupType = z.infer<typeof TemplateVariableGroupSchema>;
321
+ /**
322
+ * Calculations configuration schema
323
+ */
324
+ export declare const TemplateCalculationsConfigSchema: z.ZodObject<{
325
+ enabled: z.ZodBoolean;
326
+ taxField: z.ZodOptional<z.ZodString>;
327
+ discountField: z.ZodOptional<z.ZodString>;
328
+ shippingField: z.ZodOptional<z.ZodString>;
329
+ itemsField: z.ZodOptional<z.ZodString>;
330
+ }, z.core.$strip>;
331
+ export type TemplateCalculationsConfigType = z.infer<typeof TemplateCalculationsConfigSchema>;
332
+ /**
333
+ * Extended template frontmatter schema with variable definitions
334
+ */
335
+ export declare const TemplateVariablesFrontmatterSchema: z.ZodObject<{
336
+ documentType: z.ZodOptional<z.ZodEnum<typeof TEMPLATE_DOCUMENT_TYPE_SCHEMA>>;
337
+ calculations: z.ZodOptional<z.ZodObject<{
338
+ enabled: z.ZodBoolean;
339
+ taxField: z.ZodOptional<z.ZodString>;
340
+ discountField: z.ZodOptional<z.ZodString>;
341
+ shippingField: z.ZodOptional<z.ZodString>;
342
+ itemsField: z.ZodOptional<z.ZodString>;
343
+ }, z.core.$strip>>;
344
+ variables: z.ZodOptional<z.ZodArray<z.ZodType<{
345
+ name: string;
346
+ type: TEMPLATE_VARIABLE_TYPE_SCHEMA;
347
+ label?: string;
348
+ description?: string;
349
+ defaultValue?: unknown;
350
+ placeholder?: string;
351
+ validation?: TemplateVariableValidationType;
352
+ items?: unknown;
353
+ properties?: unknown[];
354
+ options?: TemplateVariableOptionType[];
355
+ group?: string;
356
+ order?: number;
357
+ hidden?: boolean;
358
+ readOnly?: boolean;
359
+ showIf?: string;
360
+ }, unknown, z.core.$ZodTypeInternals<{
361
+ name: string;
362
+ type: TEMPLATE_VARIABLE_TYPE_SCHEMA;
363
+ label?: string;
364
+ description?: string;
365
+ defaultValue?: unknown;
366
+ placeholder?: string;
367
+ validation?: TemplateVariableValidationType;
368
+ items?: unknown;
369
+ properties?: unknown[];
370
+ options?: TemplateVariableOptionType[];
371
+ group?: string;
372
+ order?: number;
373
+ hidden?: boolean;
374
+ readOnly?: boolean;
375
+ showIf?: string;
376
+ }, unknown>>>>;
377
+ variableGroups: z.ZodOptional<z.ZodArray<z.ZodObject<{
378
+ name: z.ZodString;
379
+ label: z.ZodString;
380
+ description: z.ZodOptional<z.ZodString>;
381
+ collapsed: z.ZodOptional<z.ZodBoolean>;
382
+ }, z.core.$strip>>>;
383
+ }, z.core.$strip>;
384
+ export type TemplateVariablesFrontmatterType = z.infer<typeof TemplateVariablesFrontmatterSchema>;
385
+ /**
386
+ * Validation result schema
387
+ */
388
+ export declare const TemplateVariableValidationResultSchema: z.ZodObject<{
389
+ valid: z.ZodBoolean;
390
+ errors: z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>;
391
+ warnings: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
392
+ coercedValues: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
393
+ }, z.core.$strip>;
394
+ export type TemplateVariableValidationResultType = z.infer<typeof TemplateVariableValidationResultSchema>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plyaz/types",
3
- "version": "1.25.2",
3
+ "version": "1.27.0",
4
4
  "author": "Redeemer Pace",
5
5
  "license": "ISC",
6
6
  "description": "Provides shared TypeScript types and schema utilities for validation and parsing in the @playz ecosystem.",