@toolproof-core/schema 1.0.16 → 1.0.17

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 (34) hide show
  1. package/dist/generated/schemas/zod/Goal.d.ts +2 -0
  2. package/dist/generated/schemas/zod/Goal.js +5 -0
  3. package/dist/generated/schemas/zod/Job.d.ts +6 -0
  4. package/dist/generated/schemas/zod/Job.js +25 -0
  5. package/dist/generated/schemas/zod/RawStrategy.d.ts +12 -0
  6. package/dist/generated/schemas/zod/RawStrategy.js +66 -0
  7. package/dist/generated/schemas/zod/ResourceType.d.ts +2 -0
  8. package/dist/generated/schemas/zod/ResourceType.js +18 -0
  9. package/dist/generated/schemas/zod/RunnableStrategy.d.ts +12 -0
  10. package/dist/generated/schemas/zod/RunnableStrategy.js +74 -0
  11. package/dist/generated/schemas/zod/StrategyRun.d.ts +12 -0
  12. package/dist/generated/schemas/zod/StrategyRun.js +88 -0
  13. package/dist/generated/schemas/zod/index.d.ts +6 -0
  14. package/dist/generated/schemas/zod/index.js +7 -0
  15. package/dist/index.d.ts +8 -1
  16. package/dist/index.js +8 -0
  17. package/dist/scripts/_lib/config.d.ts +3 -0
  18. package/dist/scripts/_lib/config.js +9 -0
  19. package/dist/scripts/_lib/utils/jsonSchemaToZod.d.ts +18 -0
  20. package/dist/scripts/_lib/utils/jsonSchemaToZod.js +607 -0
  21. package/dist/scripts/generateStandaloneZodSchema.d.ts +1 -0
  22. package/dist/scripts/generateStandaloneZodSchema.js +116 -0
  23. package/package.json +6 -2
  24. package/src/generated/schemas/zod/Goal.ts +8 -0
  25. package/src/generated/schemas/zod/Job.ts +26 -0
  26. package/src/generated/schemas/zod/RawStrategy.ts +64 -0
  27. package/src/generated/schemas/zod/ResourceType.ts +13 -0
  28. package/src/generated/schemas/zod/RunnableStrategy.ts +69 -0
  29. package/src/generated/schemas/zod/StrategyRun.ts +83 -0
  30. package/src/generated/schemas/zod/index.ts +7 -0
  31. package/src/index.ts +10 -0
  32. package/src/scripts/_lib/config.ts +12 -0
  33. package/src/scripts/_lib/utils/jsonSchemaToZod.ts +646 -0
  34. package/src/scripts/generateStandaloneZodSchema.ts +139 -0
@@ -0,0 +1,139 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { getConfig } from './_lib/config.js';
4
+ import { jsonSchemaToZodExpressions } from './_lib/utils/jsonSchemaToZod.js';
5
+
6
+ /**
7
+ * Generate a Zod v4 validator module from a standalone schema JSON file.
8
+ *
9
+ * Usage:
10
+ * node ./dist/scripts/generateStandaloneZodSchema.js --name <DefName>
11
+ */
12
+
13
+ function stripSurroundingQuotes(value: string): string {
14
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
15
+ return value.slice(1, -1);
16
+ }
17
+ return value;
18
+ }
19
+
20
+ function parseArgs(args: string[]): { name?: string } {
21
+ let name: string | undefined;
22
+ for (let i = 0; i < args.length; i++) {
23
+ const a = args[i];
24
+ if (a === '--name') {
25
+ name = args[i + 1];
26
+ i++;
27
+ } else if (a.startsWith('--name=')) {
28
+ name = a.split('=')[1];
29
+ }
30
+ }
31
+ const normalized = typeof name === 'string' ? stripSurroundingQuotes(name) : undefined;
32
+ return { name: normalized?.trim() ? normalized : undefined };
33
+ }
34
+
35
+ function isValidIdentifier(name: string): boolean {
36
+ return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name);
37
+ }
38
+
39
+ function toIdentifier(name: string): string {
40
+ // Standalone def names in Genesis are already identifier-friendly.
41
+ // We still sanitize minimally to avoid codegen footguns.
42
+ let out = name.replace(/[^A-Za-z0-9_]/g, '_');
43
+ if (!/^[A-Za-z_]/.test(out)) out = '_' + out;
44
+ return out;
45
+ }
46
+
47
+ function emitHeader(name: string): string {
48
+ return (
49
+ `// Auto-generated from standalone schema '${name}'. Do not edit.\n` +
50
+ `import { z } from 'zod/v4';\n\n`
51
+ );
52
+ }
53
+
54
+ function emitModule(name: string, expressionsByName: Record<string, string>, warnings: Array<{ path: string; message: string }>): string {
55
+ const lines: string[] = [];
56
+ lines.push(emitHeader(name));
57
+
58
+ if (warnings.length > 0) {
59
+ const warningsName = `${toIdentifier(name)}ZodGenerationWarnings`;
60
+ lines.push(`export const ${warningsName} = ` + JSON.stringify(warnings, null, 2) + ' as const;\n');
61
+ }
62
+
63
+ const rootIdent = toIdentifier(name);
64
+
65
+ // Emit file-local defs first (not exported) to avoid name collisions across modules.
66
+ const defNames = Object.keys(expressionsByName)
67
+ .filter((n) => n !== name)
68
+ .sort((a, b) => a.localeCompare(b));
69
+
70
+ for (const schemaName of defNames) {
71
+ const ident = toIdentifier(schemaName);
72
+ if (!isValidIdentifier(ident)) {
73
+ throw new Error(`Invalid generated identifier for schema '${schemaName}': '${ident}'`);
74
+ }
75
+ const expr = expressionsByName[schemaName];
76
+ lines.push(`const ${ident}Schema: z.ZodTypeAny = z.lazy(() => ${expr});`);
77
+ }
78
+
79
+ lines.push('');
80
+
81
+ // Export only the root schema.
82
+ const rootExpr = expressionsByName[name];
83
+ lines.push(`export const ${rootIdent}Schema: z.ZodTypeAny = z.lazy(() => ${rootExpr});`);
84
+
85
+ return lines.join('\n') + '\n';
86
+ }
87
+
88
+ function writeBarrelFile(zodDir: string) {
89
+ const entries = fs
90
+ .readdirSync(zodDir)
91
+ .filter((f) => f.endsWith('.ts') && f !== 'index.ts' && !f.startsWith('.'))
92
+ .sort((a, b) => a.localeCompare(b));
93
+
94
+ const exports = entries.map((f) => {
95
+ const base = path.basename(f, '.ts');
96
+ const ident = toIdentifier(base);
97
+ // Export only the root schema to avoid name collisions across modules.
98
+ return `export { ${ident}Schema } from './${base}.js';`;
99
+ });
100
+ const content = `// Auto-generated barrel file. Do not edit.\n${exports.join('\n')}\n`;
101
+ fs.writeFileSync(path.join(zodDir, 'index.ts'), content, 'utf8');
102
+ }
103
+
104
+ async function main() {
105
+ try {
106
+ const config = getConfig();
107
+ const { name } = parseArgs(process.argv.slice(2));
108
+ if (!name) throw new Error('Missing --name <DefName> argument');
109
+
110
+ const inPath = config.getStandaloneSchemaPath(`${name}.json`);
111
+ if (!fs.existsSync(inPath)) {
112
+ throw new Error(`Standalone schema not found at ${inPath}. Run generateStandaloneSchema -- --name ${name} first.`);
113
+ }
114
+
115
+ const raw = fs.readFileSync(inPath, 'utf8');
116
+ const parsed = JSON.parse(raw);
117
+
118
+ const { expressionsByName, warnings } = jsonSchemaToZodExpressions(parsed, name);
119
+
120
+ const zodDir = config.getZodSchemasDir();
121
+ const outPath = config.getZodSchemaPath(`${name}.ts`);
122
+ fs.mkdirSync(zodDir, { recursive: true });
123
+
124
+ const content = emitModule(name, expressionsByName, warnings);
125
+ fs.writeFileSync(outPath, content, 'utf8');
126
+
127
+ writeBarrelFile(zodDir);
128
+
129
+ if (warnings.length > 0) {
130
+ console.warn(`Generated Zod schema '${name}' with ${warnings.length} warning(s).`);
131
+ }
132
+ console.log(`Wrote ${outPath}`);
133
+ } catch (e: any) {
134
+ console.error(e?.message ?? e);
135
+ process.exitCode = 1;
136
+ }
137
+ }
138
+
139
+ void main();