@palantir/pack.document-schema.type-gen 0.8.0 → 0.10.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 (173) hide show
  1. package/build/esm/commands/asset/__tests__/buildPrefixRewriteFetch.test.js +85 -0
  2. package/build/esm/commands/asset/__tests__/buildPrefixRewriteFetch.test.js.map +1 -0
  3. package/build/esm/commands/asset/assetDeployHandler.js +36 -1
  4. package/build/esm/commands/asset/assetDeployHandler.js.map +1 -1
  5. package/build/esm/commands/asset/registerAssetCommands.js +1 -1
  6. package/build/esm/commands/asset/registerAssetCommands.js.map +1 -1
  7. package/build/esm/commands/ir/irGenTypesHandler.js +49 -0
  8. package/build/esm/commands/ir/irGenTypesHandler.js.map +1 -0
  9. package/build/esm/commands/ir/registerIrCommands.js +2 -0
  10. package/build/esm/commands/ir/registerIrCommands.js.map +1 -1
  11. package/build/esm/commands/schema/registerSchemaCommands.js +2 -0
  12. package/build/esm/commands/schema/registerSchemaCommands.js.map +1 -1
  13. package/build/esm/commands/schema/schemaIrHandler.js +56 -0
  14. package/build/esm/commands/schema/schemaIrHandler.js.map +1 -0
  15. package/build/esm/index.js.map +1 -1
  16. package/build/esm/lib/pack-docschema-api/pack-docschema-ir/recordDef.js.map +1 -1
  17. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/custom-discriminant.js +1 -1
  18. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/custom-discriminant.js.map +1 -1
  19. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/external-refs.js +1 -1
  20. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/external-refs.js.map +1 -1
  21. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/simple-record.js +1 -1
  22. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/simple-record.js.map +1 -1
  23. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/union-types.js +1 -1
  24. package/build/esm/utils/ir/__tests__/__snapshots__/generateModelsFromIr/union-types.js.map +1 -1
  25. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/array-and-map-types.js +1 -1
  26. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/array-and-map-types.js.map +1 -1
  27. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/boolean-fields.js +1 -1
  28. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/boolean-fields.js.map +1 -1
  29. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/custom-discriminant.js +1 -1
  30. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/custom-discriminant.js.map +1 -1
  31. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/datetime-fields.js +1 -1
  32. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/datetime-fields.js.map +1 -1
  33. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/simple-records.js +1 -1
  34. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/simple-records.js.map +1 -1
  35. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/union-before-records.js +1 -1
  36. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/union-before-records.js.map +1 -1
  37. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/union-types.js +1 -1
  38. package/build/esm/utils/ir/__tests__/__snapshots__/generateZodSchemasFromIr/union-types.js.map +1 -1
  39. package/build/esm/utils/ir/__tests__/convertIrToWireSchema.test.js +27 -16
  40. package/build/esm/utils/ir/__tests__/convertIrToWireSchema.test.js.map +1 -1
  41. package/build/esm/utils/ir/__tests__/generateModelsFromIr.test.js +6 -24
  42. package/build/esm/utils/ir/__tests__/generateModelsFromIr.test.js.map +1 -1
  43. package/build/esm/utils/ir/__tests__/generateZodSchemasFromIr.test.js +10 -40
  44. package/build/esm/utils/ir/__tests__/generateZodSchemasFromIr.test.js.map +1 -1
  45. package/build/esm/utils/ir/convertIrToWireSchema.js +4 -2
  46. package/build/esm/utils/ir/convertIrToWireSchema.js.map +1 -1
  47. package/build/esm/utils/ir/generateZodSchemasFromIr.js +1 -1
  48. package/build/esm/utils/ir/generateZodSchemasFromIr.js.map +1 -1
  49. package/build/esm/utils/ir/irFieldHelpers.js +383 -0
  50. package/build/esm/utils/ir/irFieldHelpers.js.map +1 -0
  51. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/single-version.js +2 -0
  52. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/single-version.js.map +1 -0
  53. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/three-version-chain.js +2 -0
  54. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/three-version-chain.js.map +1 -0
  55. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/two-version.js +2 -0
  56. package/build/esm/utils/schema/__tests__/__snapshots__/generateVersionsFromSchema/two-version.js.map +1 -0
  57. package/build/esm/utils/schema/__tests__/convertSchemaToSteps.test.js +13 -0
  58. package/build/esm/utils/schema/__tests__/convertSchemaToSteps.test.js.map +1 -1
  59. package/build/esm/utils/schema/__tests__/fixtures.js +278 -0
  60. package/build/esm/utils/schema/__tests__/fixtures.js.map +1 -0
  61. package/build/esm/utils/schema/__tests__/generateInternalFromSchema.test.js +53 -0
  62. package/build/esm/utils/schema/__tests__/generateInternalFromSchema.test.js.map +1 -0
  63. package/build/esm/utils/schema/__tests__/generateModelMetadataFromSchema.test.js +42 -0
  64. package/build/esm/utils/schema/__tests__/generateModelMetadataFromSchema.test.js.map +1 -0
  65. package/build/esm/utils/schema/__tests__/generateScopeFromSchema.test.js +38 -0
  66. package/build/esm/utils/schema/__tests__/generateScopeFromSchema.test.js.map +1 -0
  67. package/build/esm/utils/schema/__tests__/generateVersionedTypesFromSchema.test.js +64 -0
  68. package/build/esm/utils/schema/__tests__/generateVersionedTypesFromSchema.test.js.map +1 -0
  69. package/build/esm/utils/schema/__tests__/generateVersionedZodFromSchema.test.js +60 -0
  70. package/build/esm/utils/schema/__tests__/generateVersionedZodFromSchema.test.js.map +1 -0
  71. package/build/esm/utils/schema/__tests__/generateVersionsFromSchema.test.js +38 -0
  72. package/build/esm/utils/schema/__tests__/generateVersionsFromSchema.test.js.map +1 -0
  73. package/build/esm/utils/schema/__tests__/snapshotUtils.js +106 -0
  74. package/build/esm/utils/schema/__tests__/snapshotUtils.js.map +1 -0
  75. package/build/esm/utils/schema/convertSchemaToSteps.js +6 -0
  76. package/build/esm/utils/schema/convertSchemaToSteps.js.map +1 -1
  77. package/build/esm/utils/schema/generateIndexFromSchema.js +74 -0
  78. package/build/esm/utils/schema/generateIndexFromSchema.js.map +1 -0
  79. package/build/esm/utils/schema/generateInternalFromSchema.js +264 -0
  80. package/build/esm/utils/schema/generateInternalFromSchema.js.map +1 -0
  81. package/build/esm/utils/schema/generateModelMetadataFromSchema.js +172 -0
  82. package/build/esm/utils/schema/generateModelMetadataFromSchema.js.map +1 -0
  83. package/build/esm/utils/schema/generateScopeFromSchema.js +151 -0
  84. package/build/esm/utils/schema/generateScopeFromSchema.js.map +1 -0
  85. package/build/esm/utils/schema/generateTypesFromSchema.js +20 -26
  86. package/build/esm/utils/schema/generateTypesFromSchema.js.map +1 -1
  87. package/build/esm/utils/schema/generateVersionedTypesFromSchema.js +187 -0
  88. package/build/esm/utils/schema/generateVersionedTypesFromSchema.js.map +1 -0
  89. package/build/esm/utils/schema/generateVersionedZodFromSchema.js +153 -0
  90. package/build/esm/utils/schema/generateVersionedZodFromSchema.js.map +1 -0
  91. package/build/esm/utils/schema/generateVersionsFromSchema.js +52 -0
  92. package/build/esm/utils/schema/generateVersionsFromSchema.js.map +1 -0
  93. package/build/esm/utils/schema/loadSchemaModule.js +53 -0
  94. package/build/esm/utils/schema/loadSchemaModule.js.map +1 -0
  95. package/build/esm/utils/schema/resolveSchemaChain.js +104 -0
  96. package/build/esm/utils/schema/resolveSchemaChain.js.map +1 -0
  97. package/build/esm/utils/schema/runtimeSchema.js +69 -0
  98. package/build/esm/utils/schema/runtimeSchema.js.map +1 -0
  99. package/build/esm/utils/schema/writeAllSdkFiles.js +111 -0
  100. package/build/esm/utils/schema/writeAllSdkFiles.js.map +1 -0
  101. package/build/esm/utils/steps/__tests__/__snapshots__/types/arrayFields.js.map +1 -1
  102. package/build/esm/utils/steps/__tests__/__snapshots__/types/customDiscriminant.js +1 -1
  103. package/build/esm/utils/steps/__tests__/__snapshots__/types/customDiscriminant.js.map +1 -1
  104. package/build/esm/utils/steps/__tests__/__snapshots__/types/generateTypeScriptInterfaces.js +1 -1
  105. package/build/esm/utils/steps/__tests__/__snapshots__/types/generateTypeScriptInterfaces.js.map +1 -1
  106. package/build/esm/utils/steps/__tests__/__snapshots__/types/refTypes.js.map +1 -1
  107. package/build/esm/utils/steps/__tests__/__snapshots__/types/simpleRecord.js.map +1 -1
  108. package/build/esm/utils/steps/__tests__/__snapshots__/types/simpleSteps.js +1 -1
  109. package/build/esm/utils/steps/__tests__/__snapshots__/types/simpleSteps.js.map +1 -1
  110. package/build/esm/utils/steps/__tests__/convertStepsToIr.test.js +93 -1
  111. package/build/esm/utils/steps/__tests__/convertStepsToIr.test.js.map +1 -1
  112. package/build/esm/utils/steps/convertStepsToIr.js +45 -40
  113. package/build/esm/utils/steps/convertStepsToIr.js.map +1 -1
  114. package/build/types/commands/asset/__tests__/buildPrefixRewriteFetch.test.d.ts +2 -0
  115. package/build/types/commands/asset/__tests__/buildPrefixRewriteFetch.test.d.ts.map +1 -0
  116. package/build/types/commands/asset/assetDeployHandler.d.ts +15 -0
  117. package/build/types/commands/asset/assetDeployHandler.d.ts.map +1 -1
  118. package/build/types/commands/asset/registerAssetCommands.d.ts.map +1 -1
  119. package/build/types/commands/ir/irGenTypesHandler.d.ts +8 -0
  120. package/build/types/commands/ir/irGenTypesHandler.d.ts.map +1 -0
  121. package/build/types/commands/ir/registerIrCommands.d.ts.map +1 -1
  122. package/build/types/commands/schema/registerSchemaCommands.d.ts.map +1 -1
  123. package/build/types/commands/schema/schemaIrHandler.d.ts +19 -0
  124. package/build/types/commands/schema/schemaIrHandler.d.ts.map +1 -0
  125. package/build/types/index.d.ts +1 -0
  126. package/build/types/index.d.ts.map +1 -1
  127. package/build/types/lib/pack-docschema-api/pack-docschema-ir/recordDef.d.ts +0 -2
  128. package/build/types/lib/pack-docschema-api/pack-docschema-ir/recordDef.d.ts.map +1 -1
  129. package/build/types/utils/ir/irFieldHelpers.d.ts +36 -0
  130. package/build/types/utils/ir/irFieldHelpers.d.ts.map +1 -0
  131. package/build/types/utils/schema/__tests__/fixtures.d.ts +23 -0
  132. package/build/types/utils/schema/__tests__/fixtures.d.ts.map +1 -0
  133. package/build/types/utils/schema/__tests__/generateInternalFromSchema.test.d.ts +2 -0
  134. package/build/types/utils/schema/__tests__/generateInternalFromSchema.test.d.ts.map +1 -0
  135. package/build/types/utils/schema/__tests__/generateModelMetadataFromSchema.test.d.ts +2 -0
  136. package/build/types/utils/schema/__tests__/generateModelMetadataFromSchema.test.d.ts.map +1 -0
  137. package/build/types/utils/schema/__tests__/generateScopeFromSchema.test.d.ts +2 -0
  138. package/build/types/utils/schema/__tests__/generateScopeFromSchema.test.d.ts.map +1 -0
  139. package/build/types/utils/schema/__tests__/generateVersionedTypesFromSchema.test.d.ts +2 -0
  140. package/build/types/utils/schema/__tests__/generateVersionedTypesFromSchema.test.d.ts.map +1 -0
  141. package/build/types/utils/schema/__tests__/generateVersionedZodFromSchema.test.d.ts +2 -0
  142. package/build/types/utils/schema/__tests__/generateVersionedZodFromSchema.test.d.ts.map +1 -0
  143. package/build/types/utils/schema/__tests__/generateVersionsFromSchema.test.d.ts +2 -0
  144. package/build/types/utils/schema/__tests__/generateVersionsFromSchema.test.d.ts.map +1 -0
  145. package/build/types/utils/schema/__tests__/snapshotUtils.d.ts +47 -0
  146. package/build/types/utils/schema/__tests__/snapshotUtils.d.ts.map +1 -0
  147. package/build/types/utils/schema/convertSchemaToSteps.d.ts.map +1 -1
  148. package/build/types/utils/schema/generateIndexFromSchema.d.ts +14 -0
  149. package/build/types/utils/schema/generateIndexFromSchema.d.ts.map +1 -0
  150. package/build/types/utils/schema/generateInternalFromSchema.d.ts +15 -0
  151. package/build/types/utils/schema/generateInternalFromSchema.d.ts.map +1 -0
  152. package/build/types/utils/schema/generateModelMetadataFromSchema.d.ts +10 -0
  153. package/build/types/utils/schema/generateModelMetadataFromSchema.d.ts.map +1 -0
  154. package/build/types/utils/schema/generateScopeFromSchema.d.ts +13 -0
  155. package/build/types/utils/schema/generateScopeFromSchema.d.ts.map +1 -0
  156. package/build/types/utils/schema/generateTypesFromSchema.d.ts.map +1 -1
  157. package/build/types/utils/schema/generateVersionedTypesFromSchema.d.ts +15 -0
  158. package/build/types/utils/schema/generateVersionedTypesFromSchema.d.ts.map +1 -0
  159. package/build/types/utils/schema/generateVersionedZodFromSchema.d.ts +12 -0
  160. package/build/types/utils/schema/generateVersionedZodFromSchema.d.ts.map +1 -0
  161. package/build/types/utils/schema/generateVersionsFromSchema.d.ts +13 -0
  162. package/build/types/utils/schema/generateVersionsFromSchema.d.ts.map +1 -0
  163. package/build/types/utils/schema/loadSchemaModule.d.ts +14 -0
  164. package/build/types/utils/schema/loadSchemaModule.d.ts.map +1 -0
  165. package/build/types/utils/schema/resolveSchemaChain.d.ts +39 -0
  166. package/build/types/utils/schema/resolveSchemaChain.d.ts.map +1 -0
  167. package/build/types/utils/schema/runtimeSchema.d.ts +27 -0
  168. package/build/types/utils/schema/runtimeSchema.d.ts.map +1 -0
  169. package/build/types/utils/schema/writeAllSdkFiles.d.ts +8 -0
  170. package/build/types/utils/schema/writeAllSdkFiles.d.ts.map +1 -0
  171. package/build/types/utils/steps/convertStepsToIr.d.ts +3 -3
  172. package/build/types/utils/steps/convertStepsToIr.d.ts.map +1 -1
  173. package/package.json +3 -3
@@ -0,0 +1,187 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { formatVariantName } from "../formatVariantName.js";
18
+ import { GENERATED_FILE_HEADER } from "../generatedFileHeader.js";
19
+ import { collectReferencedTypes, convertFieldTypeToTypeScript, detectUsedRefTypes } from "../ir/irFieldHelpers.js";
20
+ import { resolveMinVersion } from "./resolveSchemaChain.js";
21
+ import { typesFilePath, versionedTypeName, versionedWriteTypeName } from "./runtimeSchema.js";
22
+ /**
23
+ * Generate read types for a specific schema version.
24
+ */
25
+ function generateReadTypesForVersion(version, ir) {
26
+ const usedRefTypes = detectUsedRefTypes(ir);
27
+ let output = GENERATED_FILE_HEADER;
28
+ if (usedRefTypes.size > 0) {
29
+ const refTypesList = Array.from(usedRefTypes).sort().join(", ");
30
+ output += `import type { ${refTypesList} } from "@palantir/pack.document-schema.model-types";\n`;
31
+ }
32
+ output += "\n";
33
+
34
+ // Generate record interfaces
35
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
36
+ if (modelDef.type !== "record") continue;
37
+ const record = modelDef.record;
38
+ const typeName = versionedTypeName(modelKey, version);
39
+ if (record.description) {
40
+ output += `/**\n * ${record.description}\n */\n`;
41
+ }
42
+ output += `export interface ${typeName} {\n`;
43
+ for (const field of record.fields) {
44
+ const tsType = convertFieldTypeToTypeScript(field.fieldType, version);
45
+ const optional = field.isOptional ? "?" : "";
46
+ output += ` readonly ${field.key}${optional}: ${tsType};\n`;
47
+ }
48
+ output += "}\n\n";
49
+ }
50
+
51
+ // Generate union types
52
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
53
+ if (modelDef.type !== "union") continue;
54
+ const union = modelDef.union;
55
+ const typeName = versionedTypeName(modelKey, version);
56
+ const discriminantField = union.discriminant;
57
+ const variantInterfaces = [];
58
+ for (const [variantName, variantModelKey] of Object.entries(union.variants)) {
59
+ const interfaceName = `${typeName}${formatVariantName(variantName)}`;
60
+ variantInterfaces.push({
61
+ name: interfaceName,
62
+ discriminatorValue: variantName
63
+ });
64
+
65
+ // Look up the variant's model to check if it's a record
66
+ const variantModel = ir.models[variantModelKey];
67
+ if (variantModel != null && variantModel.type === "record") {
68
+ const recordTypeName = versionedTypeName(variantModelKey, version);
69
+ output += `export interface ${interfaceName} extends ${recordTypeName} {\n`;
70
+ output += ` readonly ${discriminantField}: "${variantName}";\n`;
71
+ output += "}\n\n";
72
+ } else {
73
+ // Non-record variant (e.g. another union): emit a value field referencing the type
74
+ const tsType = versionedTypeName(variantModelKey, version);
75
+ output += `export interface ${interfaceName} {\n`;
76
+ output += ` readonly ${discriminantField}: "${variantName}";\n`;
77
+ output += ` readonly value: ${tsType};\n`;
78
+ output += "}\n\n";
79
+ }
80
+ }
81
+ output += `export type ${typeName} = ${variantInterfaces.map(v => v.name).join(" | ")};\n\n`;
82
+ for (const variant of variantInterfaces) {
83
+ output += `export function is${variant.name}(value: ${typeName}): value is ${variant.name} {\n`;
84
+ output += ` return value.${discriminantField} === "${variant.discriminatorValue}";\n`;
85
+ output += "}\n\n";
86
+ }
87
+ }
88
+ return output;
89
+ }
90
+
91
+ /**
92
+ * Generate write types for a specific schema version.
93
+ * All fields are optional in write types.
94
+ */
95
+ function generateWriteTypesForVersion(version, ir) {
96
+ const usedRefTypes = detectUsedRefTypes(ir);
97
+
98
+ // Collect referenced schema-local types (refs to other records/unions)
99
+ const referencedLocalTypes = new Set();
100
+ for (const modelDef of Object.values(ir.models)) {
101
+ if (modelDef.type !== "record") continue;
102
+ for (const field of modelDef.record.fields) {
103
+ collectReferencedTypes(field.fieldType, version, referencedLocalTypes);
104
+ }
105
+ }
106
+ let output = GENERATED_FILE_HEADER;
107
+ if (usedRefTypes.size > 0) {
108
+ const refTypesList = Array.from(usedRefTypes).sort().join(", ");
109
+ output += `import type { ${refTypesList} } from "@palantir/pack.document-schema.model-types";\n`;
110
+ }
111
+ if (referencedLocalTypes.size > 0) {
112
+ const localTypesList = Array.from(referencedLocalTypes).sort().join(", ");
113
+ output += `import type { ${localTypesList} } from "${typesFilePath(version)}";\n`;
114
+ }
115
+ output += "\n";
116
+
117
+ // Generate record write types
118
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
119
+ if (modelDef.type !== "record") continue;
120
+ const typeName = versionedWriteTypeName(modelKey, version);
121
+ output += `export type ${typeName} = {\n`;
122
+ for (const field of modelDef.record.fields) {
123
+ // All fields are optional in write types
124
+ const tsType = convertFieldTypeToTypeScript(field.fieldType, version);
125
+ output += ` readonly ${field.key}?: ${tsType};\n`;
126
+ }
127
+ output += "};\n\n";
128
+ }
129
+ return output;
130
+ }
131
+
132
+ /**
133
+ * Generate the types.ts re-export file that maps unversioned names to the latest version.
134
+ */
135
+ function generateTypesReExport({
136
+ ir,
137
+ version
138
+ }) {
139
+ const reExports = [];
140
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
141
+ if (modelDef.type === "record") {
142
+ const versioned = versionedTypeName(modelKey, version);
143
+ reExports.push(`export type { ${versioned} as ${modelKey} } from "${typesFilePath(version)}";`);
144
+ } else if (modelDef.type === "union") {
145
+ const versioned = versionedTypeName(modelKey, version);
146
+ reExports.push(`export type { ${versioned} as ${modelKey} } from "${typesFilePath(version)}";`);
147
+
148
+ // Also re-export union variant types
149
+ for (const variantName of Object.keys(modelDef.union.variants)) {
150
+ const formattedVariant = formatVariantName(variantName);
151
+ const versionedVariant = `${versionedTypeName(modelKey, version)}${formattedVariant}`;
152
+ reExports.push(`export type { ${versionedVariant} as ${`${modelKey}${formattedVariant}`} } from "${typesFilePath(version)}";`);
153
+ }
154
+ }
155
+ }
156
+ return GENERATED_FILE_HEADER + reExports.join("\n") + "\n";
157
+ }
158
+
159
+ /**
160
+ * Generate per-version public types and write types from an already-resolved
161
+ * versioned IR chain.
162
+ */
163
+ export function generateVersionedTypesFromChain(resolved, minSupportedVersion) {
164
+ const {
165
+ chain
166
+ } = resolved;
167
+ const {
168
+ minVersion
169
+ } = resolveMinVersion(chain, minSupportedVersion);
170
+ const readTypes = new Map();
171
+ const writeTypes = new Map();
172
+ for (const {
173
+ version,
174
+ ir
175
+ } of chain) {
176
+ if (version < minVersion) continue;
177
+ readTypes.set(version, generateReadTypesForVersion(version, ir));
178
+ writeTypes.set(version, generateWriteTypesForVersion(version, ir));
179
+ }
180
+ const typesReExport = generateTypesReExport(chain[chain.length - 1]);
181
+ return {
182
+ readTypes,
183
+ writeTypes,
184
+ typesReExport
185
+ };
186
+ }
187
+ //# sourceMappingURL=generateVersionedTypesFromSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVersionedTypesFromSchema.js","names":["formatVariantName","GENERATED_FILE_HEADER","collectReferencedTypes","convertFieldTypeToTypeScript","detectUsedRefTypes","resolveMinVersion","typesFilePath","versionedTypeName","versionedWriteTypeName","generateReadTypesForVersion","version","ir","usedRefTypes","output","size","refTypesList","Array","from","sort","join","modelKey","modelDef","Object","entries","models","type","record","typeName","description","field","fields","tsType","fieldType","optional","isOptional","key","union","discriminantField","discriminant","variantInterfaces","variantName","variantModelKey","variants","interfaceName","push","name","discriminatorValue","variantModel","recordTypeName","map","v","variant","generateWriteTypesForVersion","referencedLocalTypes","Set","values","localTypesList","generateTypesReExport","reExports","versioned","keys","formattedVariant","versionedVariant","generateVersionedTypesFromChain","resolved","minSupportedVersion","chain","minVersion","readTypes","Map","writeTypes","set","typesReExport","length"],"sources":["generateVersionedTypesFromSchema.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { IRealTimeDocumentSchema } from \"../../lib/pack-docschema-api/pack-docschema-ir/index.js\";\nimport { formatVariantName } from \"../formatVariantName.js\";\nimport { GENERATED_FILE_HEADER } from \"../generatedFileHeader.js\";\nimport {\n collectReferencedTypes,\n convertFieldTypeToTypeScript,\n detectUsedRefTypes,\n} from \"../ir/irFieldHelpers.js\";\nimport type { ResolvedIrChain, VersionedIrEntry } from \"./resolveSchemaChain.js\";\nimport { resolveMinVersion } from \"./resolveSchemaChain.js\";\nimport { typesFilePath, versionedTypeName, versionedWriteTypeName } from \"./runtimeSchema.js\";\n\nexport interface VersionedTypesOutput {\n /** types_vN.ts files keyed by version number */\n readTypes: Map<number, string>;\n /** writeTypes_vN.ts files keyed by version number */\n writeTypes: Map<number, string>;\n /** types.ts re-export of latest version types under unversioned names */\n typesReExport: string;\n}\n\n/**\n * Generate read types for a specific schema version.\n */\nfunction generateReadTypesForVersion(\n version: number,\n ir: IRealTimeDocumentSchema,\n): string {\n const usedRefTypes = detectUsedRefTypes(ir);\n\n let output = GENERATED_FILE_HEADER;\n\n if (usedRefTypes.size > 0) {\n const refTypesList = Array.from(usedRefTypes).sort().join(\", \");\n output +=\n `import type { ${refTypesList} } from \"@palantir/pack.document-schema.model-types\";\\n`;\n }\n\n output += \"\\n\";\n\n // Generate record interfaces\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type !== \"record\") continue;\n\n const record = modelDef.record;\n const typeName = versionedTypeName(modelKey, version);\n\n if (record.description) {\n output += `/**\\n * ${record.description}\\n */\\n`;\n }\n\n output += `export interface ${typeName} {\\n`;\n\n for (const field of record.fields) {\n const tsType = convertFieldTypeToTypeScript(field.fieldType, version);\n const optional = field.isOptional ? \"?\" : \"\";\n output += ` readonly ${field.key}${optional}: ${tsType};\\n`;\n }\n\n output += \"}\\n\\n\";\n }\n\n // Generate union types\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type !== \"union\") continue;\n\n const union = modelDef.union;\n const typeName = versionedTypeName(modelKey, version);\n const discriminantField = union.discriminant;\n const variantInterfaces: Array<{ name: string; discriminatorValue: string }> = [];\n\n for (const [variantName, variantModelKey] of Object.entries(union.variants)) {\n const interfaceName = `${typeName}${formatVariantName(variantName)}`;\n variantInterfaces.push({ name: interfaceName, discriminatorValue: variantName });\n\n // Look up the variant's model to check if it's a record\n const variantModel = ir.models[variantModelKey];\n if (variantModel != null && variantModel.type === \"record\") {\n const recordTypeName = versionedTypeName(variantModelKey, version);\n output += `export interface ${interfaceName} extends ${recordTypeName} {\\n`;\n output += ` readonly ${discriminantField}: \"${variantName}\";\\n`;\n output += \"}\\n\\n\";\n } else {\n // Non-record variant (e.g. another union): emit a value field referencing the type\n const tsType = versionedTypeName(variantModelKey, version);\n output += `export interface ${interfaceName} {\\n`;\n output += ` readonly ${discriminantField}: \"${variantName}\";\\n`;\n output += ` readonly value: ${tsType};\\n`;\n output += \"}\\n\\n\";\n }\n }\n\n output += `export type ${typeName} = ${variantInterfaces.map(v => v.name).join(\" | \")};\\n\\n`;\n\n for (const variant of variantInterfaces) {\n output +=\n `export function is${variant.name}(value: ${typeName}): value is ${variant.name} {\\n`;\n output += ` return value.${discriminantField} === \"${variant.discriminatorValue}\";\\n`;\n output += \"}\\n\\n\";\n }\n }\n\n return output;\n}\n\n/**\n * Generate write types for a specific schema version.\n * All fields are optional in write types.\n */\nfunction generateWriteTypesForVersion(\n version: number,\n ir: IRealTimeDocumentSchema,\n): string {\n const usedRefTypes = detectUsedRefTypes(ir);\n\n // Collect referenced schema-local types (refs to other records/unions)\n const referencedLocalTypes = new Set<string>();\n for (const modelDef of Object.values(ir.models)) {\n if (modelDef.type !== \"record\") continue;\n for (const field of modelDef.record.fields) {\n collectReferencedTypes(field.fieldType, version, referencedLocalTypes);\n }\n }\n\n let output = GENERATED_FILE_HEADER;\n\n if (usedRefTypes.size > 0) {\n const refTypesList = Array.from(usedRefTypes).sort().join(\", \");\n output +=\n `import type { ${refTypesList} } from \"@palantir/pack.document-schema.model-types\";\\n`;\n }\n\n if (referencedLocalTypes.size > 0) {\n const localTypesList = Array.from(referencedLocalTypes).sort().join(\", \");\n output += `import type { ${localTypesList} } from \"${typesFilePath(version)}\";\\n`;\n }\n\n output += \"\\n\";\n\n // Generate record write types\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type !== \"record\") continue;\n\n const typeName = versionedWriteTypeName(modelKey, version);\n\n output += `export type ${typeName} = {\\n`;\n\n for (const field of modelDef.record.fields) {\n // All fields are optional in write types\n const tsType = convertFieldTypeToTypeScript(field.fieldType, version);\n output += ` readonly ${field.key}?: ${tsType};\\n`;\n }\n\n output += \"};\\n\\n\";\n }\n\n return output;\n}\n\n/**\n * Generate the types.ts re-export file that maps unversioned names to the latest version.\n */\nfunction generateTypesReExport({ ir, version }: VersionedIrEntry): string {\n const reExports: string[] = [];\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type === \"record\") {\n const versioned = versionedTypeName(modelKey, version);\n reExports.push(\n `export type { ${versioned} as ${modelKey} } from \"${typesFilePath(version)}\";`,\n );\n } else if (modelDef.type === \"union\") {\n const versioned = versionedTypeName(modelKey, version);\n reExports.push(\n `export type { ${versioned} as ${modelKey} } from \"${typesFilePath(version)}\";`,\n );\n\n // Also re-export union variant types\n for (const variantName of Object.keys(modelDef.union.variants)) {\n const formattedVariant = formatVariantName(variantName);\n const versionedVariant = `${versionedTypeName(modelKey, version)}${formattedVariant}`;\n const unversionedVariant = `${modelKey}${formattedVariant}`;\n reExports.push(\n `export type { ${versionedVariant} as ${unversionedVariant} } from \"${\n typesFilePath(version)\n }\";`,\n );\n }\n }\n }\n\n return GENERATED_FILE_HEADER + reExports.join(\"\\n\") + \"\\n\";\n}\n\n/**\n * Generate per-version public types and write types from an already-resolved\n * versioned IR chain.\n */\nexport function generateVersionedTypesFromChain(\n resolved: ResolvedIrChain,\n minSupportedVersion?: number,\n): VersionedTypesOutput {\n const { chain } = resolved;\n const { minVersion } = resolveMinVersion(chain, minSupportedVersion);\n\n const readTypes = new Map<number, string>();\n const writeTypes = new Map<number, string>();\n\n for (const { version, ir } of chain) {\n if (version < minVersion) continue;\n\n readTypes.set(version, generateReadTypesForVersion(version, ir));\n writeTypes.set(version, generateWriteTypesForVersion(version, ir));\n }\n\n const typesReExport = generateTypesReExport(chain[chain.length - 1]!);\n\n return { readTypes, writeTypes, typesReExport };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,SAASA,iBAAiB,QAAQ,yBAAyB;AAC3D,SAASC,qBAAqB,QAAQ,2BAA2B;AACjE,SACEC,sBAAsB,EACtBC,4BAA4B,EAC5BC,kBAAkB,QACb,yBAAyB;AAEhC,SAASC,iBAAiB,QAAQ,yBAAyB;AAC3D,SAASC,aAAa,EAAEC,iBAAiB,EAAEC,sBAAsB,QAAQ,oBAAoB;AAW7F;AACA;AACA;AACA,SAASC,2BAA2BA,CAClCC,OAAe,EACfC,EAA2B,EACnB;EACR,MAAMC,YAAY,GAAGR,kBAAkB,CAACO,EAAE,CAAC;EAE3C,IAAIE,MAAM,GAAGZ,qBAAqB;EAElC,IAAIW,YAAY,CAACE,IAAI,GAAG,CAAC,EAAE;IACzB,MAAMC,YAAY,GAAGC,KAAK,CAACC,IAAI,CAACL,YAAY,CAAC,CAACM,IAAI,CAAC,CAAC,CAACC,IAAI,CAAC,IAAI,CAAC;IAC/DN,MAAM,IACJ,iBAAiBE,YAAY,yDAAyD;EAC1F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,0 +1,153 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { formatVariantName } from "../formatVariantName.js";
18
+ import { GENERATED_FILE_HEADER } from "../generatedFileHeader.js";
19
+ import { convertFieldTypeToZodSchema } from "../ir/irFieldHelpers.js";
20
+ import { resolveMinVersion } from "./resolveSchemaChain.js";
21
+ import { typesFilePath, versionedSchemaName, versionedTypeName } from "./runtimeSchema.js";
22
+ /**
23
+ * Generate Zod schemas for a specific schema version.
24
+ * Uses `.passthrough()` to preserve unknown fields.
25
+ */
26
+ function generateZodSchemasForVersion(version, ir) {
27
+ let output = GENERATED_FILE_HEADER;
28
+
29
+ // Collect type names for import
30
+ const typeNames = [];
31
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
32
+ if (modelDef.type === "record") {
33
+ typeNames.push(versionedTypeName(modelKey, version));
34
+ } else if (modelDef.type === "union") {
35
+ typeNames.push(versionedTypeName(modelKey, version));
36
+ for (const variantName of Object.keys(modelDef.union.variants)) {
37
+ typeNames.push(`${versionedTypeName(modelKey, version)}${formatVariantName(variantName)}`);
38
+ }
39
+ }
40
+ }
41
+ output += "import type { ZodType } from \"zod\";\n";
42
+ output += "import { z } from \"zod\";\n";
43
+ if (typeNames.length > 0) {
44
+ output += `import type { ${typeNames.sort().join(", ")} } from "${typesFilePath(version)}";\n`;
45
+ }
46
+ output += "\n";
47
+
48
+ // Generate record schemas first (unions may depend on them)
49
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
50
+ if (modelDef.type !== "record") continue;
51
+ const zodSchemaName = versionedSchemaName(modelKey, version);
52
+ const typeName = versionedTypeName(modelKey, version);
53
+ const fieldLines = [];
54
+ for (const field of modelDef.record.fields) {
55
+ let zodType = convertFieldTypeToZodSchema(field.fieldType, version);
56
+ if (field.isOptional) {
57
+ zodType = `${zodType}.optional()`;
58
+ }
59
+ fieldLines.push(` ${field.key}: ${zodType}`);
60
+ }
61
+ output += `export const ${zodSchemaName} = z.object({\n${fieldLines.join(",\n")}\n}).passthrough() satisfies ZodType<${typeName}>;\n\n`;
62
+ }
63
+
64
+ // Generate union schemas
65
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
66
+ if (modelDef.type !== "union") continue;
67
+ const union = modelDef.union;
68
+ const discriminantField = union.discriminant;
69
+ const variantSchemaNames = [];
70
+ for (const [variantName, variantModelKey] of Object.entries(union.variants)) {
71
+ const formattedVariant = formatVariantName(variantName);
72
+ const variantZodSchemaName = `${versionedSchemaName(modelKey, version)}${formattedVariant}`;
73
+ const variantTypeName = `${versionedTypeName(modelKey, version)}${formattedVariant}`;
74
+ variantSchemaNames.push(variantZodSchemaName);
75
+
76
+ // Look up the variant's model to check if it's a record
77
+ const variantModel = ir.models[variantModelKey];
78
+ if (variantModel != null && variantModel.type === "record") {
79
+ const recordZodSchemaName = versionedSchemaName(variantModelKey, version);
80
+ output += `export const ${variantZodSchemaName} = ${recordZodSchemaName}.extend({\n`;
81
+ output += ` ${discriminantField}: z.literal("${variantName}")\n`;
82
+ output += `}) satisfies ZodType<${variantTypeName}>;\n\n`;
83
+ } else {
84
+ // Non-record variant (e.g. another union): emit a value field referencing the schema
85
+ const refZodSchemaName = versionedSchemaName(variantModelKey, version);
86
+ const refTypeName = versionedTypeName(variantModelKey, version);
87
+ output += `export const ${variantZodSchemaName} = z.object({\n`;
88
+ output += ` ${discriminantField}: z.literal("${variantName}"),\n`;
89
+ output += ` value: z.lazy((): ZodType<${refTypeName}> => ${refZodSchemaName})\n`;
90
+ output += `}) satisfies ZodType<${variantTypeName}>;\n\n`;
91
+ }
92
+ }
93
+ const unionZodSchemaName = versionedSchemaName(modelKey, version);
94
+ const typeName = versionedTypeName(modelKey, version);
95
+ output += `export const ${unionZodSchemaName} = z.discriminatedUnion("${discriminantField}", [\n ${variantSchemaNames.join(",\n ")}\n]) satisfies ZodType<${typeName}>;\n\n`;
96
+ }
97
+ return output;
98
+ }
99
+
100
+ /**
101
+ * Generate the schema.ts re-export file mapping unversioned names to latest version.
102
+ */
103
+ function generateSchemaReExport({
104
+ ir,
105
+ version
106
+ }) {
107
+ let output = GENERATED_FILE_HEADER;
108
+ const reExports = [];
109
+ for (const [modelKey, modelDef] of Object.entries(ir.models)) {
110
+ if (modelDef.type === "record") {
111
+ const versioned = versionedSchemaName(modelKey, version);
112
+ reExports.push(`export { ${versioned} as ${modelKey}Schema } from "./schema_v${version}.js";`);
113
+ } else if (modelDef.type === "union") {
114
+ const versioned = versionedSchemaName(modelKey, version);
115
+ reExports.push(`export { ${versioned} as ${modelKey}Schema } from "./schema_v${version}.js";`);
116
+
117
+ // Also re-export union variant schemas
118
+ for (const variantName of Object.keys(modelDef.union.variants)) {
119
+ const formattedVariant = formatVariantName(variantName);
120
+ const versionedVariant = `${versionedSchemaName(modelKey, version)}${formattedVariant}`;
121
+ reExports.push(`export { ${versionedVariant} as ${`${modelKey}${formattedVariant}Schema`} } from "./schema_v${version}.js";`);
122
+ }
123
+ }
124
+ }
125
+ output += reExports.join("\n") + "\n";
126
+ return output;
127
+ }
128
+
129
+ /**
130
+ * Generate per-version Zod schemas from an already-resolved versioned IR chain.
131
+ */
132
+ export function generateVersionedZodFromChain(resolved, minSupportedVersion) {
133
+ const {
134
+ chain
135
+ } = resolved;
136
+ const {
137
+ minVersion
138
+ } = resolveMinVersion(chain, minSupportedVersion);
139
+ const zodSchemas = new Map();
140
+ for (const {
141
+ version,
142
+ ir
143
+ } of chain) {
144
+ if (version < minVersion) continue;
145
+ zodSchemas.set(version, generateZodSchemasForVersion(version, ir));
146
+ }
147
+ const schemaReExport = generateSchemaReExport(chain[chain.length - 1]);
148
+ return {
149
+ zodSchemas,
150
+ schemaReExport
151
+ };
152
+ }
153
+ //# sourceMappingURL=generateVersionedZodFromSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVersionedZodFromSchema.js","names":["formatVariantName","GENERATED_FILE_HEADER","convertFieldTypeToZodSchema","resolveMinVersion","typesFilePath","versionedSchemaName","versionedTypeName","generateZodSchemasForVersion","version","ir","output","typeNames","modelKey","modelDef","Object","entries","models","type","push","variantName","keys","union","variants","length","sort","join","zodSchemaName","typeName","fieldLines","field","record","fields","zodType","fieldType","isOptional","key","discriminantField","discriminant","variantSchemaNames","variantModelKey","formattedVariant","variantZodSchemaName","variantTypeName","variantModel","recordZodSchemaName","refZodSchemaName","refTypeName","unionZodSchemaName","generateSchemaReExport","reExports","versioned","versionedVariant","generateVersionedZodFromChain","resolved","minSupportedVersion","chain","minVersion","zodSchemas","Map","set","schemaReExport"],"sources":["generateVersionedZodFromSchema.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { IRealTimeDocumentSchema } from \"../../lib/pack-docschema-api/pack-docschema-ir/index.js\";\nimport { formatVariantName } from \"../formatVariantName.js\";\nimport { GENERATED_FILE_HEADER } from \"../generatedFileHeader.js\";\nimport { convertFieldTypeToZodSchema } from \"../ir/irFieldHelpers.js\";\nimport type { ResolvedIrChain, VersionedIrEntry } from \"./resolveSchemaChain.js\";\nimport { resolveMinVersion } from \"./resolveSchemaChain.js\";\nimport { typesFilePath, versionedSchemaName, versionedTypeName } from \"./runtimeSchema.js\";\n\nexport interface VersionedZodOutput {\n /** schema_vN.ts files keyed by version number */\n zodSchemas: Map<number, string>;\n /** schema.ts re-export of latest version schemas under unversioned names */\n schemaReExport: string;\n}\n\n/**\n * Generate Zod schemas for a specific schema version.\n * Uses `.passthrough()` to preserve unknown fields.\n */\nfunction generateZodSchemasForVersion(\n version: number,\n ir: IRealTimeDocumentSchema,\n): string {\n let output = GENERATED_FILE_HEADER;\n\n // Collect type names for import\n const typeNames: string[] = [];\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type === \"record\") {\n typeNames.push(versionedTypeName(modelKey, version));\n } else if (modelDef.type === \"union\") {\n typeNames.push(versionedTypeName(modelKey, version));\n for (const variantName of Object.keys(modelDef.union.variants)) {\n typeNames.push(\n `${versionedTypeName(modelKey, version)}${formatVariantName(variantName)}`,\n );\n }\n }\n }\n\n output += \"import type { ZodType } from \\\"zod\\\";\\n\";\n output += \"import { z } from \\\"zod\\\";\\n\";\n\n if (typeNames.length > 0) {\n output += `import type { ${typeNames.sort().join(\", \")} } from \"${typesFilePath(version)}\";\\n`;\n }\n\n output += \"\\n\";\n\n // Generate record schemas first (unions may depend on them)\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type !== \"record\") continue;\n\n const zodSchemaName = versionedSchemaName(modelKey, version);\n const typeName = versionedTypeName(modelKey, version);\n const fieldLines: string[] = [];\n\n for (const field of modelDef.record.fields) {\n let zodType = convertFieldTypeToZodSchema(field.fieldType, version);\n if (field.isOptional) {\n zodType = `${zodType}.optional()`;\n }\n fieldLines.push(` ${field.key}: ${zodType}`);\n }\n\n output += `export const ${zodSchemaName} = z.object({\\n${\n fieldLines.join(\",\\n\")\n }\\n}).passthrough() satisfies ZodType<${typeName}>;\\n\\n`;\n }\n\n // Generate union schemas\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type !== \"union\") continue;\n\n const union = modelDef.union;\n const discriminantField = union.discriminant;\n const variantSchemaNames: string[] = [];\n\n for (const [variantName, variantModelKey] of Object.entries(union.variants)) {\n const formattedVariant = formatVariantName(variantName);\n const variantZodSchemaName = `${versionedSchemaName(modelKey, version)}${formattedVariant}`;\n const variantTypeName = `${versionedTypeName(modelKey, version)}${formattedVariant}`;\n variantSchemaNames.push(variantZodSchemaName);\n\n // Look up the variant's model to check if it's a record\n const variantModel = ir.models[variantModelKey];\n if (variantModel != null && variantModel.type === \"record\") {\n const recordZodSchemaName = versionedSchemaName(variantModelKey, version);\n output += `export const ${variantZodSchemaName} = ${recordZodSchemaName}.extend({\\n`;\n output += ` ${discriminantField}: z.literal(\"${variantName}\")\\n`;\n output += `}) satisfies ZodType<${variantTypeName}>;\\n\\n`;\n } else {\n // Non-record variant (e.g. another union): emit a value field referencing the schema\n const refZodSchemaName = versionedSchemaName(variantModelKey, version);\n const refTypeName = versionedTypeName(variantModelKey, version);\n output += `export const ${variantZodSchemaName} = z.object({\\n`;\n output += ` ${discriminantField}: z.literal(\"${variantName}\"),\\n`;\n output += ` value: z.lazy((): ZodType<${refTypeName}> => ${refZodSchemaName})\\n`;\n output += `}) satisfies ZodType<${variantTypeName}>;\\n\\n`;\n }\n }\n\n const unionZodSchemaName = versionedSchemaName(modelKey, version);\n const typeName = versionedTypeName(modelKey, version);\n output +=\n `export const ${unionZodSchemaName} = z.discriminatedUnion(\"${discriminantField}\", [\\n ${\n variantSchemaNames.join(\",\\n \")\n }\\n]) satisfies ZodType<${typeName}>;\\n\\n`;\n }\n\n return output;\n}\n\n/**\n * Generate the schema.ts re-export file mapping unversioned names to latest version.\n */\nfunction generateSchemaReExport(\n { ir, version }: VersionedIrEntry,\n): string {\n let output = GENERATED_FILE_HEADER;\n\n const reExports: string[] = [];\n\n for (const [modelKey, modelDef] of Object.entries(ir.models)) {\n if (modelDef.type === \"record\") {\n const versioned = versionedSchemaName(modelKey, version);\n reExports.push(\n `export { ${versioned} as ${modelKey}Schema } from \"./schema_v${version}.js\";`,\n );\n } else if (modelDef.type === \"union\") {\n const versioned = versionedSchemaName(modelKey, version);\n reExports.push(\n `export { ${versioned} as ${modelKey}Schema } from \"./schema_v${version}.js\";`,\n );\n\n // Also re-export union variant schemas\n for (const variantName of Object.keys(modelDef.union.variants)) {\n const formattedVariant = formatVariantName(variantName);\n const versionedVariant = `${versionedSchemaName(modelKey, version)}${formattedVariant}`;\n const unversionedVariant = `${modelKey}${formattedVariant}Schema`;\n reExports.push(\n `export { ${versionedVariant} as ${unversionedVariant} } from \"./schema_v${version}.js\";`,\n );\n }\n }\n }\n\n output += reExports.join(\"\\n\") + \"\\n\";\n\n return output;\n}\n\n/**\n * Generate per-version Zod schemas from an already-resolved versioned IR chain.\n */\nexport function generateVersionedZodFromChain(\n resolved: ResolvedIrChain,\n minSupportedVersion?: number,\n): VersionedZodOutput {\n const { chain } = resolved;\n const { minVersion } = resolveMinVersion(chain, minSupportedVersion);\n\n const zodSchemas = new Map<number, string>();\n\n for (const { version, ir } of chain) {\n if (version < minVersion) continue;\n\n zodSchemas.set(version, generateZodSchemasForVersion(version, ir));\n }\n\n const schemaReExport = generateSchemaReExport(chain[chain.length - 1]!);\n\n return { zodSchemas, schemaReExport };\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,SAASA,iBAAiB,QAAQ,yBAAyB;AAC3D,SAASC,qBAAqB,QAAQ,2BAA2B;AACjE,SAASC,2BAA2B,QAAQ,yBAAyB;AAErE,SAASC,iBAAiB,QAAQ,yBAAyB;AAC3D,SAASC,aAAa,EAAEC,mBAAmB,EAAEC,iBAAiB,QAAQ,oBAAoB;AAS1F;AACA;AACA;AACA;AACA,SAASC,4BAA4BA,CACnCC,OAAe,EACfC,EAA2B,EACnB;EACR,IAAIC,MAAM,GAAGT,qBAAqB;;EAElC;EACA,MAAMU,SAAmB,GAAG,EAAE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AAC;EAEpE,MAAMG,UAAU,GAAG,IAAIC,GAAG,CAAiB,CAAC;EAE5C,KAAK,MAAM;IAAElD,OAAO;IAAEC;EAAG,CAAC,IAAI8C,KAAK,EAAE;IACnC,IAAI/C,OAAO,GAAGgD,UAAU,EAAE;IAE1BC,UAAU,CAACE,GAAG,CAACnD,OAAO,EAAED,4BAA4B,CAACC,OAAO,EAAEC,EAAE,CAAC,CAAC;EACpE;EAEA,MAAMmD,cAAc,GAAGZ,sBAAsB,CAACO,KAAK,CAACA,KAAK,CAAChC,MAAM,GAAG,CAAC,CAAE,CAAC;EAEvE,OAAO;IAAEkC,UAAU;IAAEG;EAAe,CAAC;AACvC","ignoreList":[]}
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { GENERATED_FILE_HEADER } from "../generatedFileHeader.js";
18
+ import { resolveMinVersion } from "./resolveSchemaChain.js";
19
+
20
+ /**
21
+ * Generate versions.ts from an already-resolved versioned IR chain.
22
+ *
23
+ * Output:
24
+ * ```
25
+ * export type SupportedVersions = 1 | 2;
26
+ * export type LatestVersion = 2;
27
+ * export type MinSupportedVersion = 1;
28
+ * ```
29
+ */
30
+ export function generateVersionsFromChain(resolved, minSupportedVersion) {
31
+ const {
32
+ chain
33
+ } = resolved;
34
+ const {
35
+ latestVersion,
36
+ minVersion
37
+ } = resolveMinVersion(chain, minSupportedVersion);
38
+ const supportedVersions = [];
39
+ for (const {
40
+ version
41
+ } of chain) {
42
+ if (version >= minVersion) {
43
+ supportedVersions.push(version);
44
+ }
45
+ }
46
+ let output = GENERATED_FILE_HEADER;
47
+ output += `export type SupportedVersions = ${supportedVersions.join(" | ")};\n`;
48
+ output += `export type LatestVersion = ${latestVersion};\n`;
49
+ output += `export type MinSupportedVersion = ${minVersion};\n`;
50
+ return output;
51
+ }
52
+ //# sourceMappingURL=generateVersionsFromSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateVersionsFromSchema.js","names":["GENERATED_FILE_HEADER","resolveMinVersion","generateVersionsFromChain","resolved","minSupportedVersion","chain","latestVersion","minVersion","supportedVersions","version","push","output","join"],"sources":["generateVersionsFromSchema.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { GENERATED_FILE_HEADER } from \"../generatedFileHeader.js\";\nimport type { ResolvedIrChain } from \"./resolveSchemaChain.js\";\nimport { resolveMinVersion } from \"./resolveSchemaChain.js\";\n\n/**\n * Generate versions.ts from an already-resolved versioned IR chain.\n *\n * Output:\n * ```\n * export type SupportedVersions = 1 | 2;\n * export type LatestVersion = 2;\n * export type MinSupportedVersion = 1;\n * ```\n */\nexport function generateVersionsFromChain(\n resolved: ResolvedIrChain,\n minSupportedVersion?: number,\n): string {\n const { chain } = resolved;\n const { latestVersion, minVersion } = resolveMinVersion(chain, minSupportedVersion);\n\n const supportedVersions: number[] = [];\n for (const { version } of chain) {\n if (version >= minVersion) {\n supportedVersions.push(version);\n }\n }\n\n let output = GENERATED_FILE_HEADER;\n output += `export type SupportedVersions = ${supportedVersions.join(\" | \")};\\n`;\n output += `export type LatestVersion = ${latestVersion};\\n`;\n output += `export type MinSupportedVersion = ${minVersion};\\n`;\n\n return output;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA,SAASA,qBAAqB,QAAQ,2BAA2B;AAEjE,SAASC,iBAAiB,QAAQ,yBAAyB;;AAE3D;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CACvCC,QAAyB,EACzBC,mBAA4B,EACpB;EACR,MAAM;IAAEC;EAAM,CAAC,GAAGF,QAAQ;EAC1B,MAAM;IAAEG,aAAa;IAAEC;EAAW,CAAC,GAAGN,iBAAiB,CAACI,KAAK,EAAED,mBAAmB,CAAC;EAEnF,MAAMI,iBAA2B,GAAG,EAAE;EACtC,KAAK,MAAM;IAAEC;EAAQ,CAAC,IAAIJ,KAAK,EAAE;IAC/B,IAAII,OAAO,IAAIF,UAAU,EAAE;MACzBC,iBAAiB,CAACE,IAAI,CAACD,OAAO,CAAC;IACjC;EACF;EAEA,IAAIE,MAAM,GAAGX,qBAAqB;EAClCW,MAAM,IAAI,mCAAmCH,iBAAiB,CAACI,IAAI,CAAC,KAAK,CAAC,KAAK;EAC/ED,MAAM,IAAI,+BAA+BL,aAAa,KAAK;EAC3DK,MAAM,IAAI,qCAAqCJ,UAAU,KAAK;EAE9D,OAAOI,MAAM;AACf","ignoreList":[]}
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import fs from "fs-extra";
18
+ import path from "path";
19
+ import { pathToFileURL } from "url";
20
+ import { extractValidSchema } from "./validateSchemaModule.js";
21
+
22
+ /**
23
+ * Pull a `SchemaDefinition` out of an imported module. Accepts either a chain
24
+ * built via `defineSchemaUpdate(...)` (`type: "initial" | "versioned"`) or a
25
+ * plain `ModelDefs` object exported as `default` (which is wrapped in an
26
+ * `InitialSchema` at version 1).
27
+ */
28
+ export function extractSchemaDefinition(schemaModule) {
29
+ const defaultExport = schemaModule.default;
30
+ if (typeof defaultExport === "object" && defaultExport != null && "type" in defaultExport && (defaultExport.type === "initial" || defaultExport.type === "versioned")) {
31
+ return defaultExport;
32
+ }
33
+ const models = extractValidSchema(schemaModule);
34
+ return {
35
+ type: "initial",
36
+ version: 1,
37
+ models
38
+ };
39
+ }
40
+
41
+ /**
42
+ * Resolve and import a schema module from a file path, returning a
43
+ * `SchemaDefinition` ready for IR resolution.
44
+ */
45
+ export async function loadSchemaModule(filePath) {
46
+ const inputPath = path.resolve(filePath);
47
+ if (!(await fs.pathExists(inputPath))) {
48
+ throw new Error(`Input file not found: ${inputPath}`);
49
+ }
50
+ const schemaModule = await import(pathToFileURL(inputPath).href);
51
+ return extractSchemaDefinition(schemaModule);
52
+ }
53
+ //# sourceMappingURL=loadSchemaModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadSchemaModule.js","names":["fs","path","pathToFileURL","extractValidSchema","extractSchemaDefinition","schemaModule","defaultExport","default","type","models","version","loadSchemaModule","filePath","inputPath","resolve","pathExists","Error","href"],"sources":["loadSchemaModule.ts"],"sourcesContent":["/*\n * Copyright 2026 Palantir Technologies, Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { InitialSchema, SchemaDefinition } from \"@palantir/pack.schema\";\nimport fs from \"fs-extra\";\nimport path from \"path\";\nimport { pathToFileURL } from \"url\";\nimport { extractValidSchema } from \"./validateSchemaModule.js\";\n\n/**\n * Pull a `SchemaDefinition` out of an imported module. Accepts either a chain\n * built via `defineSchemaUpdate(...)` (`type: \"initial\" | \"versioned\"`) or a\n * plain `ModelDefs` object exported as `default` (which is wrapped in an\n * `InitialSchema` at version 1).\n */\nexport function extractSchemaDefinition(schemaModule: unknown): SchemaDefinition {\n const mod = schemaModule as Record<string, unknown>;\n const defaultExport = mod.default;\n\n if (\n typeof defaultExport === \"object\" && defaultExport != null\n && \"type\" in defaultExport\n && (defaultExport.type === \"initial\" || defaultExport.type === \"versioned\")\n ) {\n return defaultExport as SchemaDefinition;\n }\n\n const models = extractValidSchema(schemaModule);\n return { type: \"initial\", version: 1, models } satisfies InitialSchema;\n}\n\n/**\n * Resolve and import a schema module from a file path, returning a\n * `SchemaDefinition` ready for IR resolution.\n */\nexport async function loadSchemaModule(filePath: string): Promise<SchemaDefinition> {\n const inputPath = path.resolve(filePath);\n if (!(await fs.pathExists(inputPath))) {\n throw new Error(`Input file not found: ${inputPath}`);\n }\n const schemaModule: unknown = await import(pathToFileURL(inputPath).href);\n return extractSchemaDefinition(schemaModule);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA,OAAOA,EAAE,MAAM,UAAU;AACzB,OAAOC,IAAI,MAAM,MAAM;AACvB,SAASC,aAAa,QAAQ,KAAK;AACnC,SAASC,kBAAkB,QAAQ,2BAA2B;;AAE9D;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,uBAAuBA,CAACC,YAAqB,EAAoB;EAE/E,MAAMC,aAAa,GADPD,YAAY,CACEE,OAAO;EAEjC,IACE,OAAOD,aAAa,KAAK,QAAQ,IAAIA,aAAa,IAAI,IAAI,IACvD,MAAM,IAAIA,aAAa,KACtBA,aAAa,CAACE,IAAI,KAAK,SAAS,IAAIF,aAAa,CAACE,IAAI,KAAK,WAAW,CAAC,EAC3E;IACA,OAAOF,aAAa;EACtB;EAEA,MAAMG,MAAM,GAAGN,kBAAkB,CAACE,YAAY,CAAC;EAC/C,OAAO;IAAEG,IAAI,EAAE,SAAS;IAAEE,OAAO,EAAE,CAAC;IAAED;EAAO,CAAC;AAChD;;AAEA;AACA;AACA;AACA;AACA,OAAO,eAAeE,gBAAgBA,CAACC,QAAgB,EAA6B;EAClF,MAAMC,SAAS,GAAGZ,IAAI,CAACa,OAAO,CAACF,QAAQ,CAAC;EACxC,IAAI,EAAE,MAAMZ,EAAE,CAACe,UAAU,CAACF,SAAS,CAAC,CAAC,EAAE;IACrC,MAAM,IAAIG,KAAK,CAAC,yBAAyBH,SAAS,EAAE,CAAC;EACvD;EACA,MAAMR,YAAqB,GAAG,MAAM,MAAM,CAACH,aAAa,CAACW,SAAS,CAAC,CAACI,IAAI,CAAC;EACzE,OAAOb,uBAAuB,CAACC,YAAY,CAAC;AAC9C","ignoreList":[]}
@@ -0,0 +1,104 @@
1
+ /*
2
+ * Copyright 2026 Palantir Technologies, Inc. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { convertSchemaToIr } from "../steps/convertStepsToIr.js";
18
+
19
+ /**
20
+ * JSON-serializable form of a single field migration. The `forward` callback
21
+ * from the schema builder is captured as its source string so the entry can be
22
+ * round-tripped through a JSON IR file.
23
+ */
24
+
25
+ /** Serialized migrations keyed by `[recordModelName][fieldName]`. */
26
+
27
+ function collectVersionedIrChain(input) {
28
+ const chain = [];
29
+ let current = input;
30
+ while (current.type === "versioned") {
31
+ chain.unshift({
32
+ version: current.version,
33
+ ir: convertSchemaToIr(current.models, {
34
+ version: current.version
35
+ }),
36
+ migrations: serializeMigrations(current.migrations)
37
+ });
38
+ current = current.previous;
39
+ }
40
+ chain.unshift({
41
+ version: current.version,
42
+ ir: convertSchemaToIr(current.models, {
43
+ version: current.version
44
+ })
45
+ });
46
+ return chain;
47
+ }
48
+
49
+ /**
50
+ * Capture each `forward` callback as its source string so the migrations object
51
+ * is plain JSON-serializable data.
52
+ */
53
+ function serializeMigrations(migrations) {
54
+ if (migrations == null) return undefined;
55
+ const result = {};
56
+ for (const [modelKey, fields] of Object.entries(migrations)) {
57
+ const fieldEntries = {};
58
+ for (const [fieldName, migration] of Object.entries(fields)) {
59
+ fieldEntries[fieldName] = {
60
+ derivedFrom: [...migration.derivedFrom],
61
+ forwardSource: migration.forward.toString()
62
+ };
63
+ }
64
+ result[modelKey] = fieldEntries;
65
+ }
66
+ return result;
67
+ }
68
+
69
+ /**
70
+ * Validate `minSupportedVersion` against an already-resolved chain and return
71
+ * the effective `minVersion`. Throws on invalid input.
72
+ */
73
+ export function resolveMinVersion(chain, minSupportedVersion) {
74
+ if (chain.length === 0) {
75
+ throw new Error("Schema version chain is empty");
76
+ }
77
+ const latestVersion = chain[chain.length - 1].version;
78
+ if (minSupportedVersion != null && !chain.some(c => c.version === minSupportedVersion)) {
79
+ throw new Error(`minSupportedVersion ${minSupportedVersion} is not in the schema chain ` + `(available versions: ${chain.map(c => c.version).join(", ")})`);
80
+ }
81
+ return {
82
+ latestVersion,
83
+ minVersion: minSupportedVersion ?? latestVersion
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Collect the version chain and resolve min/latest versions.
89
+ * Each version's models are converted to IR (IRealTimeDocumentSchema).
90
+ * Throws on empty chain or invalid minSupportedVersion.
91
+ */
92
+ export function resolveSchemaChain(schema, minSupportedVersion) {
93
+ const chain = collectVersionedIrChain(schema);
94
+ const {
95
+ latestVersion,
96
+ minVersion
97
+ } = resolveMinVersion(chain, minSupportedVersion);
98
+ return {
99
+ chain,
100
+ latestVersion,
101
+ minVersion
102
+ };
103
+ }
104
+ //# sourceMappingURL=resolveSchemaChain.js.map