@toolproof-core/schema 1.0.1 → 1.0.3
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.
- package/dist/generated/dependencyMap.json +292 -0
- package/dist/generated/normalized/Genesis.d.ts +2 -0
- package/dist/generated/normalized/Genesis.js +2 -0
- package/dist/generated/normalized/Genesis.json +2000 -0
- package/dist/generated/resources/Genesis.d.ts +2 -0
- package/dist/generated/resources/Genesis.js +2 -0
- package/dist/generated/resources/Genesis.json +2899 -0
- package/dist/generated/schemas/Genesis.d.ts +2 -0
- package/dist/generated/schemas/Genesis.js +2 -0
- package/dist/generated/schemas/Genesis.json +1458 -0
- package/dist/generated/schemas/standalone/Goal.d.ts +2 -0
- package/dist/generated/schemas/standalone/Goal.js +2 -0
- package/dist/generated/schemas/standalone/Goal.json +86 -0
- package/dist/generated/schemas/standalone/Job.d.ts +2 -0
- package/dist/generated/schemas/standalone/Job.js +2 -0
- package/dist/generated/schemas/standalone/Job.json +236 -0
- package/dist/generated/schemas/standalone/RawStrategy.d.ts +2 -0
- package/dist/generated/schemas/standalone/RawStrategy.js +2 -0
- package/dist/generated/schemas/standalone/RawStrategy.json +642 -0
- package/dist/generated/schemas/standalone/ResourceType.d.ts +2 -0
- package/dist/generated/schemas/standalone/ResourceType.js +2 -0
- package/dist/generated/schemas/standalone/ResourceType.json +140 -0
- package/dist/generated/schemas/standalone/RunnableStrategy.d.ts +2 -0
- package/dist/generated/schemas/standalone/RunnableStrategy.js +2 -0
- package/dist/generated/schemas/standalone/RunnableStrategy.json +712 -0
- package/dist/generated/schemas/standalone/StrategyRun.d.ts +2 -0
- package/dist/generated/schemas/standalone/StrategyRun.js +2 -0
- package/dist/generated/schemas/standalone/StrategyRun.json +994 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/package.json +9 -8
- package/src/Genesis.json +1999 -1999
- package/src/generated/types/standalone/Resource_Genesis.js +1 -1
- package/src/generated/types/standalone/Resource_Job.js +1 -1
- package/src/generated/types/standalone/Resource_RawStrategy.js +1 -1
- package/src/generated/types/standalone/Resource_ResourceType.js +1 -1
- package/src/generated/types/standalone/Resource_RunnableStrategy.js +1 -1
- package/src/index.ts +93 -1
- package/src/scripts/_lib/config.ts +205 -205
- package/src/scripts/extractSchemasFromResourceTypeShells.ts +218 -218
- package/src/scripts/generateDependencies.ts +120 -120
- package/src/scripts/generateSchemaShims.ts +135 -135
- package/src/scripts/generateStandaloneSchema.ts +175 -175
- package/src/scripts/generateStandaloneType.ts +119 -119
- package/src/scripts/generateTypes.ts +614 -614
- package/src/scripts/normalizeAnchorsToPointers.ts +123 -123
- package/src/scripts/wrapResourceTypesWithResourceShells.ts +84 -84
|
@@ -1,218 +1,218 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
|
-
import { getConfig } from "./_lib/config.js";
|
|
5
|
-
|
|
6
|
-
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
7
|
-
|
|
8
|
-
interface ExtractOptions {
|
|
9
|
-
inPath: string;
|
|
10
|
-
outPath: string;
|
|
11
|
-
topLevelId?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function parseArgs(): ExtractOptions {
|
|
15
|
-
const config = getConfig();
|
|
16
|
-
const argv = process.argv.slice(2);
|
|
17
|
-
let inPath = "";
|
|
18
|
-
let outPath = "";
|
|
19
|
-
let topLevelId: string | undefined;
|
|
20
|
-
for (let i = 0; i < argv.length; i++) {
|
|
21
|
-
const a = argv[i];
|
|
22
|
-
if (a === "--in" && i + 1 < argv.length) inPath = argv[++i];
|
|
23
|
-
else if (a === "--out" && i + 1 < argv.length) outPath = argv[++i];
|
|
24
|
-
else if (a === "--id" && i + 1 < argv.length) {
|
|
25
|
-
let v = argv[++i];
|
|
26
|
-
// Strip accidental surrounding quotes from PowerShell/cmd
|
|
27
|
-
if ((v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))) {
|
|
28
|
-
v = v.slice(1, -1);
|
|
29
|
-
}
|
|
30
|
-
topLevelId = v;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// Use config defaults if not provided via CLI
|
|
35
|
-
if (!inPath) {
|
|
36
|
-
// Use generated/normalized version with anchor refs rewritten to pointers
|
|
37
|
-
inPath = config.getNormalizedSourcePath();
|
|
38
|
-
}
|
|
39
|
-
if (!outPath) {
|
|
40
|
-
outPath = config.getSchemaPath(config.getSourceFile());
|
|
41
|
-
}
|
|
42
|
-
if (!topLevelId) {
|
|
43
|
-
topLevelId = config.getSchemaId('Genesis');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Resolve to absolute paths from project root
|
|
47
|
-
const cwd = config.getRoot();
|
|
48
|
-
const wasInRelative = !path.isAbsolute(inPath);
|
|
49
|
-
const wasOutRelative = !path.isAbsolute(outPath);
|
|
50
|
-
if (wasInRelative) inPath = path.join(cwd, inPath);
|
|
51
|
-
if (wasOutRelative) outPath = path.join(cwd, outPath);
|
|
52
|
-
// Fallback: resolve relative to script directory if not found
|
|
53
|
-
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
54
|
-
if (!fs.existsSync(inPath) && wasInRelative) inPath = path.resolve(scriptDir, inPath);
|
|
55
|
-
const outDir = path.dirname(outPath);
|
|
56
|
-
if (!fs.existsSync(outDir) && wasOutRelative) {
|
|
57
|
-
// Try making directory relative to script dir
|
|
58
|
-
const altOut = path.resolve(scriptDir, outPath);
|
|
59
|
-
const altOutDir = path.dirname(altOut);
|
|
60
|
-
if (!fs.existsSync(path.dirname(outPath))) {
|
|
61
|
-
// Prefer creating outDir at cwd location if possible; otherwise fallback below when writing
|
|
62
|
-
} else {
|
|
63
|
-
outPath = altOut;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return { inPath, outPath, topLevelId };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// ATTENTION: we don't want heuristic behavior.
|
|
70
|
-
// Heuristic: determine if a node is a Type shell
|
|
71
|
-
function isTypeShell(node: any): boolean {
|
|
72
|
-
return (
|
|
73
|
-
node && typeof node === "object" && !Array.isArray(node) &&
|
|
74
|
-
// Treat any object that has an 'nucleusSchema' AND 'identity' as a Type shell
|
|
75
|
-
// This prevents false positives where 'nucleusSchema' is just a regular schema property
|
|
76
|
-
node.nucleusSchema && typeof node.nucleusSchema === "object" &&
|
|
77
|
-
node.identity && typeof node.identity === "string"
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Merge $defs into target, without overwriting existing keys unless identical
|
|
82
|
-
function mergeDefs(target: Record<string, JSONValue>, source?: any, label?: string) {
|
|
83
|
-
if (!source || typeof source !== "object") return;
|
|
84
|
-
const src = (source as any)["$defs"];
|
|
85
|
-
if (!src || typeof src !== "object") return;
|
|
86
|
-
for (const [k, v] of Object.entries(src)) {
|
|
87
|
-
if (!(k in target)) {
|
|
88
|
-
target[k] = v as JSONValue;
|
|
89
|
-
} else {
|
|
90
|
-
// Best-effort: if duplicate key, require structural equality; otherwise, namespace
|
|
91
|
-
const existing = JSON.stringify(target[k]);
|
|
92
|
-
const incoming = JSON.stringify(v);
|
|
93
|
-
if (existing !== incoming) {
|
|
94
|
-
const altKey = `${k}__from_${(label || "defs").replace(/[^A-Za-z0-9_]+/g, "_")}`;
|
|
95
|
-
if (!(altKey in target)) target[altKey] = v as JSONValue;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Deeply traverse an object replacing any Type shell with its nucleusSchema,
|
|
102
|
-
// and hoist its inner $defs to topDefs. Prevent infinite recursion with a visited set.
|
|
103
|
-
function unwrapTypes(node: JSONValue, topDefs: Record<string, JSONValue>, labelPath: string[] = [], visited = new Set<any>()): JSONValue {
|
|
104
|
-
if (node && typeof node === "object") {
|
|
105
|
-
if (visited.has(node)) return node; // avoid cycles
|
|
106
|
-
visited.add(node);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (isTypeShell(node)) {
|
|
110
|
-
const env = node as any;
|
|
111
|
-
const inner = env.nucleusSchema;
|
|
112
|
-
// Hoist inner $defs before stripping
|
|
113
|
-
mergeDefs(topDefs, inner, labelPath.join("_"));
|
|
114
|
-
// Return the inner schema itself, after also unwrapping any nested shells it may contain
|
|
115
|
-
const unwrappedInner = unwrapTypes(inner as JSONValue, topDefs, labelPath.concat([String(env.identity || "env")]), visited);
|
|
116
|
-
return unwrappedInner;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (Array.isArray(node)) {
|
|
120
|
-
return node.map((v, i) => unwrapTypes(v, topDefs, labelPath.concat([String(i)]), visited)) as JSONValue;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (node && typeof node === "object") {
|
|
124
|
-
const out: Record<string, JSONValue> = {};
|
|
125
|
-
for (const [k, v] of Object.entries(node)) {
|
|
126
|
-
if (k === "$defs" && v && typeof v === "object" && !Array.isArray(v)) {
|
|
127
|
-
// Process nested $defs: unwrap each entry value if it's a Type shell
|
|
128
|
-
const defsOut: Record<string, JSONValue> = {};
|
|
129
|
-
for (const [dk, dv] of Object.entries(v as any)) {
|
|
130
|
-
const unwrapped = unwrapTypes(dv as JSONValue, topDefs, labelPath.concat(["$defs", dk]), visited);
|
|
131
|
-
defsOut[dk] = unwrapped;
|
|
132
|
-
}
|
|
133
|
-
out[k] = defsOut;
|
|
134
|
-
} else {
|
|
135
|
-
out[k] = unwrapTypes(v as JSONValue, topDefs, labelPath.concat([k]), visited);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return out;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return node;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Pure function that takes a schema document and options, and returns the flattened schema.
|
|
146
|
-
* Performs no I/O operations.
|
|
147
|
-
*/
|
|
148
|
-
function extractSchemaLogic(doc: any, topLevelId?: string): any {
|
|
149
|
-
if (!doc || typeof doc !== "object" || !doc.nucleusSchema) {
|
|
150
|
-
throw new Error("Input must be a Type JSON with an nucleusSchema at the top level");
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const topSchema = (doc as any).nucleusSchema;
|
|
154
|
-
|
|
155
|
-
// Collect $defs so that any '#/$defs/...' pointers can be resolved from the root.
|
|
156
|
-
const outDefs: Record<string, JSONValue> = {};
|
|
157
|
-
// Seed with top-level $defs (if any) before unwrapping
|
|
158
|
-
mergeDefs(outDefs, topSchema, "top");
|
|
159
|
-
|
|
160
|
-
// Unwrap the entire top schema tree so that any nested Type shells become raw schemas
|
|
161
|
-
const flattened = unwrapTypes(topSchema as JSONValue, outDefs, ["nucleusSchema"]);
|
|
162
|
-
|
|
163
|
-
// Assemble output: force $schema, optionally set $id, hoist collected $defs
|
|
164
|
-
let base: any;
|
|
165
|
-
if (flattened && typeof flattened === "object" && !Array.isArray(flattened)) {
|
|
166
|
-
base = { ...(flattened as any) };
|
|
167
|
-
} else {
|
|
168
|
-
// If flattened is not an object (should be rare for a top-level schema), wrap it
|
|
169
|
-
base = { const: flattened };
|
|
170
|
-
}
|
|
171
|
-
// Assemble, but avoid duplicating $id: if the flattened base already has $id, prefer it.
|
|
172
|
-
const output: Record<string, JSONValue> = {
|
|
173
|
-
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
174
|
-
...base,
|
|
175
|
-
};
|
|
176
|
-
if (topLevelId && !(output as any).$id) {
|
|
177
|
-
(output as any).$id = topLevelId;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Enforce presence of $id: schema must declare an absolute identity.
|
|
181
|
-
if (!(output as any).$id) {
|
|
182
|
-
throw new Error(
|
|
183
|
-
"Flattened schema must define $id. Provide it via CLI --id or include $id in the source nucleusSchema."
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Hoist collected defs into output.$defs, taking care not to clobber any existing
|
|
188
|
-
if (!("$defs" in output)) output.$defs = {} as any;
|
|
189
|
-
const finalDefs: Record<string, JSONValue> = (output.$defs as any) || {};
|
|
190
|
-
for (const [k, v] of Object.entries(outDefs)) {
|
|
191
|
-
if (!(k in finalDefs)) finalDefs[k] = v;
|
|
192
|
-
}
|
|
193
|
-
output.$defs = finalDefs as any;
|
|
194
|
-
|
|
195
|
-
// Preserve natural key ordering (do not reorder for readability)
|
|
196
|
-
return output;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function main() {
|
|
200
|
-
const { inPath, outPath, topLevelId } = parseArgs();
|
|
201
|
-
|
|
202
|
-
if (!fs.existsSync(inPath)) {
|
|
203
|
-
console.error(`Input file not found at ${inPath}`);
|
|
204
|
-
process.exit(1);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const raw = fs.readFileSync(inPath, "utf8");
|
|
208
|
-
const doc = JSON.parse(raw);
|
|
209
|
-
|
|
210
|
-
// Core logic is now in a pure function
|
|
211
|
-
const ordered = extractSchemaLogic(doc, topLevelId);
|
|
212
|
-
|
|
213
|
-
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
214
|
-
fs.writeFileSync(outPath, JSON.stringify(ordered, null, 4), "utf8");
|
|
215
|
-
console.log(`Wrote flattened schema to ${outPath}`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
main();
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
import { getConfig } from "./_lib/config.js";
|
|
5
|
+
|
|
6
|
+
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
7
|
+
|
|
8
|
+
interface ExtractOptions {
|
|
9
|
+
inPath: string;
|
|
10
|
+
outPath: string;
|
|
11
|
+
topLevelId?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function parseArgs(): ExtractOptions {
|
|
15
|
+
const config = getConfig();
|
|
16
|
+
const argv = process.argv.slice(2);
|
|
17
|
+
let inPath = "";
|
|
18
|
+
let outPath = "";
|
|
19
|
+
let topLevelId: string | undefined;
|
|
20
|
+
for (let i = 0; i < argv.length; i++) {
|
|
21
|
+
const a = argv[i];
|
|
22
|
+
if (a === "--in" && i + 1 < argv.length) inPath = argv[++i];
|
|
23
|
+
else if (a === "--out" && i + 1 < argv.length) outPath = argv[++i];
|
|
24
|
+
else if (a === "--id" && i + 1 < argv.length) {
|
|
25
|
+
let v = argv[++i];
|
|
26
|
+
// Strip accidental surrounding quotes from PowerShell/cmd
|
|
27
|
+
if ((v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))) {
|
|
28
|
+
v = v.slice(1, -1);
|
|
29
|
+
}
|
|
30
|
+
topLevelId = v;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Use config defaults if not provided via CLI
|
|
35
|
+
if (!inPath) {
|
|
36
|
+
// Use generated/normalized version with anchor refs rewritten to pointers
|
|
37
|
+
inPath = config.getNormalizedSourcePath();
|
|
38
|
+
}
|
|
39
|
+
if (!outPath) {
|
|
40
|
+
outPath = config.getSchemaPath(config.getSourceFile());
|
|
41
|
+
}
|
|
42
|
+
if (!topLevelId) {
|
|
43
|
+
topLevelId = config.getSchemaId('Genesis');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Resolve to absolute paths from project root
|
|
47
|
+
const cwd = config.getRoot();
|
|
48
|
+
const wasInRelative = !path.isAbsolute(inPath);
|
|
49
|
+
const wasOutRelative = !path.isAbsolute(outPath);
|
|
50
|
+
if (wasInRelative) inPath = path.join(cwd, inPath);
|
|
51
|
+
if (wasOutRelative) outPath = path.join(cwd, outPath);
|
|
52
|
+
// Fallback: resolve relative to script directory if not found
|
|
53
|
+
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
|
54
|
+
if (!fs.existsSync(inPath) && wasInRelative) inPath = path.resolve(scriptDir, inPath);
|
|
55
|
+
const outDir = path.dirname(outPath);
|
|
56
|
+
if (!fs.existsSync(outDir) && wasOutRelative) {
|
|
57
|
+
// Try making directory relative to script dir
|
|
58
|
+
const altOut = path.resolve(scriptDir, outPath);
|
|
59
|
+
const altOutDir = path.dirname(altOut);
|
|
60
|
+
if (!fs.existsSync(path.dirname(outPath))) {
|
|
61
|
+
// Prefer creating outDir at cwd location if possible; otherwise fallback below when writing
|
|
62
|
+
} else {
|
|
63
|
+
outPath = altOut;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return { inPath, outPath, topLevelId };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ATTENTION: we don't want heuristic behavior.
|
|
70
|
+
// Heuristic: determine if a node is a Type shell
|
|
71
|
+
function isTypeShell(node: any): boolean {
|
|
72
|
+
return (
|
|
73
|
+
node && typeof node === "object" && !Array.isArray(node) &&
|
|
74
|
+
// Treat any object that has an 'nucleusSchema' AND 'identity' as a Type shell
|
|
75
|
+
// This prevents false positives where 'nucleusSchema' is just a regular schema property
|
|
76
|
+
node.nucleusSchema && typeof node.nucleusSchema === "object" &&
|
|
77
|
+
node.identity && typeof node.identity === "string"
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Merge $defs into target, without overwriting existing keys unless identical
|
|
82
|
+
function mergeDefs(target: Record<string, JSONValue>, source?: any, label?: string) {
|
|
83
|
+
if (!source || typeof source !== "object") return;
|
|
84
|
+
const src = (source as any)["$defs"];
|
|
85
|
+
if (!src || typeof src !== "object") return;
|
|
86
|
+
for (const [k, v] of Object.entries(src)) {
|
|
87
|
+
if (!(k in target)) {
|
|
88
|
+
target[k] = v as JSONValue;
|
|
89
|
+
} else {
|
|
90
|
+
// Best-effort: if duplicate key, require structural equality; otherwise, namespace
|
|
91
|
+
const existing = JSON.stringify(target[k]);
|
|
92
|
+
const incoming = JSON.stringify(v);
|
|
93
|
+
if (existing !== incoming) {
|
|
94
|
+
const altKey = `${k}__from_${(label || "defs").replace(/[^A-Za-z0-9_]+/g, "_")}`;
|
|
95
|
+
if (!(altKey in target)) target[altKey] = v as JSONValue;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Deeply traverse an object replacing any Type shell with its nucleusSchema,
|
|
102
|
+
// and hoist its inner $defs to topDefs. Prevent infinite recursion with a visited set.
|
|
103
|
+
function unwrapTypes(node: JSONValue, topDefs: Record<string, JSONValue>, labelPath: string[] = [], visited = new Set<any>()): JSONValue {
|
|
104
|
+
if (node && typeof node === "object") {
|
|
105
|
+
if (visited.has(node)) return node; // avoid cycles
|
|
106
|
+
visited.add(node);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (isTypeShell(node)) {
|
|
110
|
+
const env = node as any;
|
|
111
|
+
const inner = env.nucleusSchema;
|
|
112
|
+
// Hoist inner $defs before stripping
|
|
113
|
+
mergeDefs(topDefs, inner, labelPath.join("_"));
|
|
114
|
+
// Return the inner schema itself, after also unwrapping any nested shells it may contain
|
|
115
|
+
const unwrappedInner = unwrapTypes(inner as JSONValue, topDefs, labelPath.concat([String(env.identity || "env")]), visited);
|
|
116
|
+
return unwrappedInner;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (Array.isArray(node)) {
|
|
120
|
+
return node.map((v, i) => unwrapTypes(v, topDefs, labelPath.concat([String(i)]), visited)) as JSONValue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (node && typeof node === "object") {
|
|
124
|
+
const out: Record<string, JSONValue> = {};
|
|
125
|
+
for (const [k, v] of Object.entries(node)) {
|
|
126
|
+
if (k === "$defs" && v && typeof v === "object" && !Array.isArray(v)) {
|
|
127
|
+
// Process nested $defs: unwrap each entry value if it's a Type shell
|
|
128
|
+
const defsOut: Record<string, JSONValue> = {};
|
|
129
|
+
for (const [dk, dv] of Object.entries(v as any)) {
|
|
130
|
+
const unwrapped = unwrapTypes(dv as JSONValue, topDefs, labelPath.concat(["$defs", dk]), visited);
|
|
131
|
+
defsOut[dk] = unwrapped;
|
|
132
|
+
}
|
|
133
|
+
out[k] = defsOut;
|
|
134
|
+
} else {
|
|
135
|
+
out[k] = unwrapTypes(v as JSONValue, topDefs, labelPath.concat([k]), visited);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return node;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Pure function that takes a schema document and options, and returns the flattened schema.
|
|
146
|
+
* Performs no I/O operations.
|
|
147
|
+
*/
|
|
148
|
+
function extractSchemaLogic(doc: any, topLevelId?: string): any {
|
|
149
|
+
if (!doc || typeof doc !== "object" || !doc.nucleusSchema) {
|
|
150
|
+
throw new Error("Input must be a Type JSON with an nucleusSchema at the top level");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const topSchema = (doc as any).nucleusSchema;
|
|
154
|
+
|
|
155
|
+
// Collect $defs so that any '#/$defs/...' pointers can be resolved from the root.
|
|
156
|
+
const outDefs: Record<string, JSONValue> = {};
|
|
157
|
+
// Seed with top-level $defs (if any) before unwrapping
|
|
158
|
+
mergeDefs(outDefs, topSchema, "top");
|
|
159
|
+
|
|
160
|
+
// Unwrap the entire top schema tree so that any nested Type shells become raw schemas
|
|
161
|
+
const flattened = unwrapTypes(topSchema as JSONValue, outDefs, ["nucleusSchema"]);
|
|
162
|
+
|
|
163
|
+
// Assemble output: force $schema, optionally set $id, hoist collected $defs
|
|
164
|
+
let base: any;
|
|
165
|
+
if (flattened && typeof flattened === "object" && !Array.isArray(flattened)) {
|
|
166
|
+
base = { ...(flattened as any) };
|
|
167
|
+
} else {
|
|
168
|
+
// If flattened is not an object (should be rare for a top-level schema), wrap it
|
|
169
|
+
base = { const: flattened };
|
|
170
|
+
}
|
|
171
|
+
// Assemble, but avoid duplicating $id: if the flattened base already has $id, prefer it.
|
|
172
|
+
const output: Record<string, JSONValue> = {
|
|
173
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
174
|
+
...base,
|
|
175
|
+
};
|
|
176
|
+
if (topLevelId && !(output as any).$id) {
|
|
177
|
+
(output as any).$id = topLevelId;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Enforce presence of $id: schema must declare an absolute identity.
|
|
181
|
+
if (!(output as any).$id) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
"Flattened schema must define $id. Provide it via CLI --id or include $id in the source nucleusSchema."
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Hoist collected defs into output.$defs, taking care not to clobber any existing
|
|
188
|
+
if (!("$defs" in output)) output.$defs = {} as any;
|
|
189
|
+
const finalDefs: Record<string, JSONValue> = (output.$defs as any) || {};
|
|
190
|
+
for (const [k, v] of Object.entries(outDefs)) {
|
|
191
|
+
if (!(k in finalDefs)) finalDefs[k] = v;
|
|
192
|
+
}
|
|
193
|
+
output.$defs = finalDefs as any;
|
|
194
|
+
|
|
195
|
+
// Preserve natural key ordering (do not reorder for readability)
|
|
196
|
+
return output;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function main() {
|
|
200
|
+
const { inPath, outPath, topLevelId } = parseArgs();
|
|
201
|
+
|
|
202
|
+
if (!fs.existsSync(inPath)) {
|
|
203
|
+
console.error(`Input file not found at ${inPath}`);
|
|
204
|
+
process.exit(1);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const raw = fs.readFileSync(inPath, "utf8");
|
|
208
|
+
const doc = JSON.parse(raw);
|
|
209
|
+
|
|
210
|
+
// Core logic is now in a pure function
|
|
211
|
+
const ordered = extractSchemaLogic(doc, topLevelId);
|
|
212
|
+
|
|
213
|
+
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
214
|
+
fs.writeFileSync(outPath, JSON.stringify(ordered, null, 4), "utf8");
|
|
215
|
+
console.log(`Wrote flattened schema to ${outPath}`);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
main();
|