|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Base Field Mapping Protocol |
| 5 | + * |
| 6 | + * Shared by: ETL, Sync, Connector, External Lookup |
| 7 | + * |
| 8 | + * This module provides the canonical field mapping schema used across |
| 9 | + * ObjectStack for data transformation and synchronization. |
| 10 | + * |
| 11 | + * **Use Cases:** |
| 12 | + * - ETL pipelines (data/mapping.zod.ts) |
| 13 | + * - Data synchronization (automation/sync.zod.ts) |
| 14 | + * - Integration connectors (integration/connector.zod.ts) |
| 15 | + * - External lookups (data/external-lookup.zod.ts) |
| 16 | + * |
| 17 | + * @example Basic field mapping |
| 18 | + * ```typescript |
| 19 | + * const mapping: FieldMapping = { |
| 20 | + * source: 'external_user_id', |
| 21 | + * target: 'user_id', |
| 22 | + * }; |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * @example With transformation |
| 26 | + * ```typescript |
| 27 | + * const mapping: FieldMapping = { |
| 28 | + * source: 'user_name', |
| 29 | + * target: 'name', |
| 30 | + * transform: { type: 'cast', targetType: 'string' }, |
| 31 | + * defaultValue: 'Unknown' |
| 32 | + * }; |
| 33 | + * ``` |
| 34 | + */ |
| 35 | + |
| 36 | +/** |
| 37 | + * Transform Type Schema |
| 38 | + * |
| 39 | + * Defines the type of transformation to apply to a field value. |
| 40 | + * Implementations can extend this for domain-specific transforms. |
| 41 | + */ |
| 42 | +export const TransformTypeSchema = z.discriminatedUnion('type', [ |
| 43 | + z.object({ |
| 44 | + type: z.literal('constant'), |
| 45 | + value: z.any().describe('Constant value to use'), |
| 46 | + }).describe('Set a constant value'), |
| 47 | + |
| 48 | + z.object({ |
| 49 | + type: z.literal('cast'), |
| 50 | + targetType: z.enum(['string', 'number', 'boolean', 'date']).describe('Target data type'), |
| 51 | + }).describe('Cast to a specific data type'), |
| 52 | + |
| 53 | + z.object({ |
| 54 | + type: z.literal('lookup'), |
| 55 | + table: z.string().describe('Lookup table name'), |
| 56 | + keyField: z.string().describe('Field to match on'), |
| 57 | + valueField: z.string().describe('Field to retrieve'), |
| 58 | + }).describe('Lookup value from another table'), |
| 59 | + |
| 60 | + z.object({ |
| 61 | + type: z.literal('javascript'), |
| 62 | + expression: z.string().describe('JavaScript expression (e.g., "value.toUpperCase()")'), |
| 63 | + }).describe('Custom JavaScript transformation'), |
| 64 | + |
| 65 | + z.object({ |
| 66 | + type: z.literal('map'), |
| 67 | + mappings: z.record(z.any()).describe('Value mappings (e.g., {"Active": "active"})'), |
| 68 | + }).describe('Map values using a dictionary'), |
| 69 | +]); |
| 70 | + |
| 71 | +export type TransformType = z.infer<typeof TransformTypeSchema>; |
| 72 | + |
| 73 | +/** |
| 74 | + * Field Mapping Schema |
| 75 | + * |
| 76 | + * Base schema for mapping fields between source and target systems. |
| 77 | + * |
| 78 | + * **NAMING CONVENTION:** |
| 79 | + * - source: Field name in the source system |
| 80 | + * - target: Field name in the target system (should be snake_case for ObjectStack) |
| 81 | + * |
| 82 | + * @example |
| 83 | + * ```typescript |
| 84 | + * { |
| 85 | + * source: 'FirstName', |
| 86 | + * target: 'first_name', |
| 87 | + * transform: { type: 'cast', targetType: 'string' }, |
| 88 | + * defaultValue: '' |
| 89 | + * } |
| 90 | + * ``` |
| 91 | + */ |
| 92 | +export const FieldMappingSchema = z.object({ |
| 93 | + /** |
| 94 | + * Source field name |
| 95 | + */ |
| 96 | + source: z.string().describe('Source field name'), |
| 97 | + |
| 98 | + /** |
| 99 | + * Target field name (should be snake_case for ObjectStack) |
| 100 | + */ |
| 101 | + target: z.string().describe('Target field name'), |
| 102 | + |
| 103 | + /** |
| 104 | + * Transformation to apply |
| 105 | + */ |
| 106 | + transform: TransformTypeSchema.optional().describe('Transformation to apply'), |
| 107 | + |
| 108 | + /** |
| 109 | + * Default value if source is null/undefined |
| 110 | + */ |
| 111 | + defaultValue: z.any().optional().describe('Default if source is null/undefined'), |
| 112 | +}); |
| 113 | + |
| 114 | +export type FieldMapping = z.infer<typeof FieldMappingSchema>; |
0 commit comments