@omnidotdev/odk 0.0.0-stage → 0.1.1
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/README.md +122 -2
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +141 -0
- package/dist/cli.js.map +1 -0
- package/dist/compile.d.ts +152 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +11 -0
- package/dist/compile.js.map +1 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonSchemaExtract.d.ts +40 -0
- package/dist/jsonSchemaExtract.d.ts.map +1 -0
- package/dist/jsonSchemaExtract.js +144 -0
- package/dist/jsonSchemaExtract.js.map +1 -0
- package/dist/odk.d.ts +169 -0
- package/dist/odk.d.ts.map +1 -0
- package/dist/odk.js +186 -0
- package/dist/odk.js.map +1 -0
- package/dist/odk.test.d.ts +2 -0
- package/dist/odk.test.d.ts.map +1 -0
- package/dist/odk.test.js +36 -0
- package/dist/odk.test.js.map +1 -0
- package/dist/standardSchemaAdapter.d.ts +16 -0
- package/dist/standardSchemaAdapter.d.ts.map +1 -0
- package/dist/standardSchemaAdapter.js +277 -0
- package/dist/standardSchemaAdapter.js.map +1 -0
- package/dist/typespecBridge.d.ts +27 -0
- package/dist/typespecBridge.d.ts.map +1 -0
- package/dist/typespecBridge.js +274 -0
- package/dist/typespecBridge.js.map +1 -0
- package/package.json +63 -4
- package/src/cli.ts +160 -0
- package/src/compile.ts +172 -0
- package/src/index.ts +63 -0
- package/src/jsonSchemaExtract.ts +226 -0
- package/src/odk.test.ts +49 -0
- package/src/odk.ts +219 -0
- package/src/standardSchemaAdapter.ts +355 -0
- package/src/typespecBridge.ts +340 -0
package/src/odk.test.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
|
|
5
|
+
import { odk } from "./index.js";
|
|
6
|
+
|
|
7
|
+
const User = z.object({
|
|
8
|
+
id: z.string(),
|
|
9
|
+
email: z.string(),
|
|
10
|
+
name: z.string(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("odk", () => {
|
|
14
|
+
test("schema() compiles Standard Schema types into a named model", () => {
|
|
15
|
+
const schema = odk.schema("TestAPI", { User });
|
|
16
|
+
|
|
17
|
+
expect(schema.compiled.name).toBe("TestAPI");
|
|
18
|
+
expect(schema.types.User).toBe(User);
|
|
19
|
+
|
|
20
|
+
const user = schema.compiled.models.find((model) => model.name === "User");
|
|
21
|
+
const fieldNames = user?.fields.map((field) => field.name) ?? [];
|
|
22
|
+
expect(fieldNames).toContain("id");
|
|
23
|
+
expect(fieldNames).toContain("email");
|
|
24
|
+
expect(fieldNames).toContain("name");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("toJSON() round-trips the compiled schema", () => {
|
|
28
|
+
const schema = odk.schema("TestAPI", { User });
|
|
29
|
+
|
|
30
|
+
expect(JSON.parse(odk.toJSON(schema))).toEqual(odk.getCompiled(schema));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("toTypeSpec() renders the namespace and model", () => {
|
|
34
|
+
const tsp = odk.toTypeSpec(odk.schema("TestAPI", { User }));
|
|
35
|
+
|
|
36
|
+
expect(tsp).toContain("TestAPI");
|
|
37
|
+
expect(tsp).toContain("User");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("addImport() adds an import without duplicating it", () => {
|
|
41
|
+
const schema = odk.schema("TestAPI", { User });
|
|
42
|
+
odk.addImport(schema, "@typespec/http");
|
|
43
|
+
odk.addImport(schema, "@typespec/http");
|
|
44
|
+
|
|
45
|
+
expect(
|
|
46
|
+
schema.compiled.imports?.filter((i) => i === "@typespec/http"),
|
|
47
|
+
).toEqual(["@typespec/http"]);
|
|
48
|
+
});
|
|
49
|
+
});
|
package/src/odk.ts
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ODK - Omni Development Kit
|
|
3
|
+
*
|
|
4
|
+
* Main API for defining schemas and emitting to various targets.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { odk } from "@omnidotdev/odk";
|
|
9
|
+
* import { z } from "zod";
|
|
10
|
+
*
|
|
11
|
+
* const User = z.object({
|
|
12
|
+
* id: z.string().uuid(),
|
|
13
|
+
* email: z.string().email(),
|
|
14
|
+
* name: z.string().optional(),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* const schema = odk.schema("MyAPI", { User });
|
|
18
|
+
*
|
|
19
|
+
* await odk.emit(schema, {
|
|
20
|
+
* targets: ["@typespec/openapi3"],
|
|
21
|
+
* outDir: "./generated",
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Escape hatches for advanced use cases
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Add raw TypeSpec code
|
|
28
|
+
* const schema = odk.schema("MyAPI", { User });
|
|
29
|
+
* odk.raw(schema, `
|
|
30
|
+
* @route("/api")
|
|
31
|
+
* interface CustomApi {
|
|
32
|
+
* @get op health(): void;
|
|
33
|
+
* }
|
|
34
|
+
* `);
|
|
35
|
+
*
|
|
36
|
+
* // Add custom imports
|
|
37
|
+
* odk.addImport(schema, "@typespec/http");
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
import { standardSchemaToCompiled } from "./standardSchemaAdapter.js";
|
|
42
|
+
import { compiledSchemaToTypeSpec, emitToTargets } from "./typespecBridge.js";
|
|
43
|
+
|
|
44
|
+
import type { CompiledSchema, RawTypeSpecBlock } from "./compile.js";
|
|
45
|
+
import type { AdapterOptions } from "./standardSchemaAdapter.js";
|
|
46
|
+
import type { EmitOptions } from "./typespecBridge.js";
|
|
47
|
+
|
|
48
|
+
export interface SchemaOptions extends AdapterOptions {
|
|
49
|
+
/** Description for the schema namespace */
|
|
50
|
+
description?: string;
|
|
51
|
+
/** TypeSpec imports to include */
|
|
52
|
+
imports?: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ODKSchema {
|
|
56
|
+
/** The compiled schema representation */
|
|
57
|
+
compiled: CompiledSchema;
|
|
58
|
+
/** The original types passed to schema() */
|
|
59
|
+
types: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create an ODK schema from Standard Schema-compliant types.
|
|
64
|
+
*
|
|
65
|
+
* @param name - Name for the schema namespace
|
|
66
|
+
* @param types - Record of named types (Zod, ArkType, or Valibot schemas)
|
|
67
|
+
* @param options - Optional configuration
|
|
68
|
+
* @returns ODK schema object
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const User = z.object({ id: z.string(), name: z.string() });
|
|
73
|
+
* const Post = z.object({ title: z.string(), authorId: z.string() });
|
|
74
|
+
*
|
|
75
|
+
* const schema = odk.schema("Blog", { User, Post });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
function schema(
|
|
79
|
+
name: string,
|
|
80
|
+
types: Record<string, unknown>,
|
|
81
|
+
options: SchemaOptions = {},
|
|
82
|
+
): ODKSchema {
|
|
83
|
+
const compiled = standardSchemaToCompiled(name, types, options);
|
|
84
|
+
|
|
85
|
+
// Add imports from options
|
|
86
|
+
if (options.imports) {
|
|
87
|
+
compiled.imports = [...(compiled.imports ?? []), ...options.imports];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return { compiled, types };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Add a raw TypeSpec block to the schema.
|
|
95
|
+
*
|
|
96
|
+
* Use this escape hatch when you need to write TypeSpec code that
|
|
97
|
+
* ODK's schema API doesn't support.
|
|
98
|
+
*
|
|
99
|
+
* @param schema - ODK schema to modify
|
|
100
|
+
* @param content - Raw TypeSpec code
|
|
101
|
+
* @param description - Optional description/comment
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* odk.raw(schema, `
|
|
106
|
+
* @route("/api")
|
|
107
|
+
* interface CustomApi {
|
|
108
|
+
* @get op health(): void;
|
|
109
|
+
* }
|
|
110
|
+
* `);
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
function raw(schema: ODKSchema, content: string, description?: string): void {
|
|
114
|
+
const block: RawTypeSpecBlock = { content };
|
|
115
|
+
if (description) {
|
|
116
|
+
block.description = description;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!schema.compiled.rawBlocks) {
|
|
120
|
+
schema.compiled.rawBlocks = [];
|
|
121
|
+
}
|
|
122
|
+
schema.compiled.rawBlocks.push(block);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Add a TypeSpec import to the schema.
|
|
127
|
+
*
|
|
128
|
+
* @param schema - ODK schema to modify
|
|
129
|
+
* @param importPath - Import path (e.g., "@typespec/http")
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* odk.addImport(schema, "@typespec/http");
|
|
134
|
+
* odk.addImport(schema, "@omnidotdev/typespec-drizzle");
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function addImport(schema: ODKSchema, importPath: string): void {
|
|
138
|
+
if (!schema.compiled.imports) {
|
|
139
|
+
schema.compiled.imports = [];
|
|
140
|
+
}
|
|
141
|
+
if (!schema.compiled.imports.includes(importPath)) {
|
|
142
|
+
schema.compiled.imports.push(importPath);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Emit an ODK schema to target formats.
|
|
148
|
+
*
|
|
149
|
+
* @param schema - ODK schema created with odk.schema()
|
|
150
|
+
* @param options - Emission options (targets, output directory)
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* await odk.emit(schema, {
|
|
155
|
+
* targets: ["@typespec/openapi3", "@typespec/protobuf"],
|
|
156
|
+
* outDir: "./generated",
|
|
157
|
+
* });
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
async function emit(schema: ODKSchema, options: EmitOptions): Promise<void> {
|
|
161
|
+
await emitToTargets(schema.compiled, options);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Generate TypeSpec source code from an ODK schema.
|
|
166
|
+
*
|
|
167
|
+
* @param schema - ODK schema created with odk.schema()
|
|
168
|
+
* @param namespace - Optional namespace override
|
|
169
|
+
* @returns TypeSpec source code as a string
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```typescript
|
|
173
|
+
* const tspSource = odk.toTypeSpec(schema);
|
|
174
|
+
* console.log(tspSource);
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function toTypeSpec(schema: ODKSchema, namespace?: string): string {
|
|
178
|
+
return compiledSchemaToTypeSpec(schema.compiled, namespace);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get the compiled schema representation (useful for the Rust CLI).
|
|
183
|
+
*
|
|
184
|
+
* @param schema - ODK schema created with odk.schema()
|
|
185
|
+
* @returns CompiledSchema object
|
|
186
|
+
*/
|
|
187
|
+
function getCompiled(schema: ODKSchema): CompiledSchema {
|
|
188
|
+
return schema.compiled;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Serialize an ODK schema to JSON (for the Rust CLI).
|
|
193
|
+
*
|
|
194
|
+
* @param schema - ODK schema created with odk.schema()
|
|
195
|
+
* @returns JSON string of the compiled schema
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const json = odk.toJSON(schema);
|
|
200
|
+
* await Bun.write("schema.json", json);
|
|
201
|
+
* // Then: tspgen schema.json -o output.tsp
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
function toJSON(schema: ODKSchema): string {
|
|
205
|
+
return JSON.stringify(schema.compiled, null, 2);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* ODK namespace - main entry point for the library.
|
|
210
|
+
*/
|
|
211
|
+
export const odk = {
|
|
212
|
+
schema,
|
|
213
|
+
emit,
|
|
214
|
+
toTypeSpec,
|
|
215
|
+
toJSON,
|
|
216
|
+
getCompiled,
|
|
217
|
+
raw,
|
|
218
|
+
addImport,
|
|
219
|
+
} as const;
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard Schema Adapter
|
|
3
|
+
*
|
|
4
|
+
* Converts any Standard Schema-compliant library (Zod, ArkType, Valibot)
|
|
5
|
+
* to ODK's CompiledSchema IR format.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { extractJsonSchema } from "./jsonSchemaExtract.js";
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
CompiledField,
|
|
12
|
+
CompiledModel,
|
|
13
|
+
CompiledSchema,
|
|
14
|
+
CompiledType,
|
|
15
|
+
CompiledUnion,
|
|
16
|
+
FieldConstraints,
|
|
17
|
+
ScalarType,
|
|
18
|
+
} from "./compile.js";
|
|
19
|
+
import type { JSONSchema } from "./jsonSchemaExtract.js";
|
|
20
|
+
|
|
21
|
+
export interface AdapterOptions {
|
|
22
|
+
/** Suppress warnings about non-serializable constraints */
|
|
23
|
+
suppressWarnings?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Convert a collection of Standard Schema types to CompiledSchema.
|
|
28
|
+
*/
|
|
29
|
+
export function standardSchemaToCompiled(
|
|
30
|
+
name: string,
|
|
31
|
+
types: Record<string, unknown>,
|
|
32
|
+
options: AdapterOptions = {},
|
|
33
|
+
): CompiledSchema {
|
|
34
|
+
const models: CompiledModel[] = [];
|
|
35
|
+
const unions: CompiledUnion[] = [];
|
|
36
|
+
const warnings: string[] = [];
|
|
37
|
+
const processedDefs = new Set<string>();
|
|
38
|
+
|
|
39
|
+
for (const [typeName, schema] of Object.entries(types)) {
|
|
40
|
+
// Check for non-serializable constraints
|
|
41
|
+
checkSerializability(schema, typeName, warnings);
|
|
42
|
+
|
|
43
|
+
// Extract JSON Schema representation
|
|
44
|
+
const jsonSchema = extractJsonSchema(schema);
|
|
45
|
+
|
|
46
|
+
// Process any $defs/definitions first
|
|
47
|
+
const defs = { ...jsonSchema.$defs, ...jsonSchema.definitions };
|
|
48
|
+
for (const [defName, defSchema] of Object.entries(defs)) {
|
|
49
|
+
if (processedDefs.has(defName)) continue;
|
|
50
|
+
processedDefs.add(defName);
|
|
51
|
+
|
|
52
|
+
const result = jsonSchemaToType(defName, defSchema);
|
|
53
|
+
if (result.model) models.push(result.model);
|
|
54
|
+
if (result.union) unions.push(result.union);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Process the main type
|
|
58
|
+
const result = jsonSchemaToType(typeName, jsonSchema);
|
|
59
|
+
if (result.model) models.push(result.model);
|
|
60
|
+
if (result.union) unions.push(result.union);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Emit warnings
|
|
64
|
+
if (!options.suppressWarnings) {
|
|
65
|
+
for (const warning of warnings) {
|
|
66
|
+
console.warn(`\u26A0\uFE0F ${warning}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return { name, models, unions, warnings };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface TypeConversionResult {
|
|
74
|
+
model?: CompiledModel;
|
|
75
|
+
union?: CompiledUnion;
|
|
76
|
+
type?: CompiledType;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function jsonSchemaToType(
|
|
80
|
+
name: string,
|
|
81
|
+
schema: JSONSchema,
|
|
82
|
+
): TypeConversionResult {
|
|
83
|
+
// Handle enums as unions
|
|
84
|
+
if (schema.enum) {
|
|
85
|
+
return {
|
|
86
|
+
union: {
|
|
87
|
+
name,
|
|
88
|
+
variants: schema.enum.map((v) => ({
|
|
89
|
+
name: String(v),
|
|
90
|
+
type: { kind: "literal", value: v },
|
|
91
|
+
})),
|
|
92
|
+
description: schema.description,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Handle oneOf/anyOf as unions
|
|
98
|
+
const variants = schema.oneOf ?? schema.anyOf;
|
|
99
|
+
if (variants) {
|
|
100
|
+
return {
|
|
101
|
+
union: {
|
|
102
|
+
name,
|
|
103
|
+
variants: variants.map((v, i) => {
|
|
104
|
+
const variantName = v.title || `Variant${i + 1}`;
|
|
105
|
+
return {
|
|
106
|
+
name: variantName,
|
|
107
|
+
type: jsonSchemaPropertyToType(v),
|
|
108
|
+
};
|
|
109
|
+
}),
|
|
110
|
+
description: schema.description,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Handle objects as models
|
|
116
|
+
if (schema.type === "object" || schema.properties) {
|
|
117
|
+
const fields: CompiledField[] = [];
|
|
118
|
+
const required = new Set(schema.required || []);
|
|
119
|
+
|
|
120
|
+
for (const [propName, propSchema] of Object.entries(
|
|
121
|
+
schema.properties || {},
|
|
122
|
+
)) {
|
|
123
|
+
fields.push({
|
|
124
|
+
name: propName,
|
|
125
|
+
type: jsonSchemaPropertyToType(propSchema),
|
|
126
|
+
optional: !required.has(propName),
|
|
127
|
+
description: propSchema.description,
|
|
128
|
+
constraints: extractConstraints(propSchema),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
model: {
|
|
134
|
+
name,
|
|
135
|
+
fields,
|
|
136
|
+
description: schema.description,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Handle arrays
|
|
142
|
+
if (schema.type === "array") {
|
|
143
|
+
return {
|
|
144
|
+
type: {
|
|
145
|
+
kind: "array",
|
|
146
|
+
items: schema.items
|
|
147
|
+
? jsonSchemaPropertyToType(schema.items)
|
|
148
|
+
: { kind: "scalar", name: "unknown" },
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Handle refs
|
|
154
|
+
if (schema.$ref) {
|
|
155
|
+
const refName = schema.$ref.split("/").pop() || "Unknown";
|
|
156
|
+
return {
|
|
157
|
+
type: { kind: "ref", name: refName },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Handle primitives - create an alias
|
|
162
|
+
return {
|
|
163
|
+
type: jsonSchemaPropertyToType(schema),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function jsonSchemaPropertyToType(schema: JSONSchema): CompiledType {
|
|
168
|
+
// Handle refs
|
|
169
|
+
if (schema.$ref) {
|
|
170
|
+
const refName = schema.$ref.split("/").pop() || "Unknown";
|
|
171
|
+
return { kind: "ref", name: refName };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Handle const
|
|
175
|
+
if (schema.const !== undefined) {
|
|
176
|
+
return {
|
|
177
|
+
kind: "literal",
|
|
178
|
+
value: schema.const as string | number | boolean,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Handle enum (inline)
|
|
183
|
+
if (schema.enum) {
|
|
184
|
+
return {
|
|
185
|
+
kind: "union",
|
|
186
|
+
variants: schema.enum.map((v) => ({
|
|
187
|
+
kind: "literal" as const,
|
|
188
|
+
value: v,
|
|
189
|
+
})),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Handle oneOf/anyOf
|
|
194
|
+
const unionVariants = schema.oneOf ?? schema.anyOf;
|
|
195
|
+
if (unionVariants) {
|
|
196
|
+
return {
|
|
197
|
+
kind: "union",
|
|
198
|
+
variants: unionVariants.map((v) => jsonSchemaPropertyToType(v)),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Handle arrays
|
|
203
|
+
if (schema.type === "array") {
|
|
204
|
+
return {
|
|
205
|
+
kind: "array",
|
|
206
|
+
items: schema.items
|
|
207
|
+
? jsonSchemaPropertyToType(schema.items)
|
|
208
|
+
: { kind: "scalar", name: "unknown" },
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Handle objects (inline)
|
|
213
|
+
if (schema.type === "object" || schema.properties) {
|
|
214
|
+
const fields: CompiledField[] = [];
|
|
215
|
+
const required = new Set(schema.required || []);
|
|
216
|
+
|
|
217
|
+
for (const [propName, propSchema] of Object.entries(
|
|
218
|
+
schema.properties || {},
|
|
219
|
+
)) {
|
|
220
|
+
fields.push({
|
|
221
|
+
name: propName,
|
|
222
|
+
type: jsonSchemaPropertyToType(propSchema),
|
|
223
|
+
optional: !required.has(propName),
|
|
224
|
+
constraints: extractConstraints(propSchema),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { kind: "object", fields };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Handle type arrays (union of types)
|
|
232
|
+
if (Array.isArray(schema.type)) {
|
|
233
|
+
return {
|
|
234
|
+
kind: "union",
|
|
235
|
+
variants: schema.type.map((t) => ({
|
|
236
|
+
kind: "scalar" as const,
|
|
237
|
+
name: jsonTypeToScalar(t),
|
|
238
|
+
})),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Handle primitive types
|
|
243
|
+
if (typeof schema.type === "string") {
|
|
244
|
+
return { kind: "scalar", name: jsonTypeToScalar(schema.type) };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return { kind: "scalar", name: "unknown" };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function jsonTypeToScalar(type: string): ScalarType {
|
|
251
|
+
switch (type) {
|
|
252
|
+
case "string":
|
|
253
|
+
return "string";
|
|
254
|
+
case "number":
|
|
255
|
+
return "float64";
|
|
256
|
+
case "integer":
|
|
257
|
+
return "int64";
|
|
258
|
+
case "boolean":
|
|
259
|
+
return "boolean";
|
|
260
|
+
case "null":
|
|
261
|
+
return "unknown"; // TypeSpec doesn't have null, handle via optional
|
|
262
|
+
default:
|
|
263
|
+
return "unknown";
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function extractConstraints(schema: JSONSchema): FieldConstraints | undefined {
|
|
268
|
+
const constraints: FieldConstraints = {};
|
|
269
|
+
let hasConstraints = false;
|
|
270
|
+
|
|
271
|
+
if (schema.minLength !== undefined) {
|
|
272
|
+
constraints.minLength = schema.minLength;
|
|
273
|
+
hasConstraints = true;
|
|
274
|
+
}
|
|
275
|
+
if (schema.maxLength !== undefined) {
|
|
276
|
+
constraints.maxLength = schema.maxLength;
|
|
277
|
+
hasConstraints = true;
|
|
278
|
+
}
|
|
279
|
+
if (schema.minimum !== undefined) {
|
|
280
|
+
constraints.minValue = schema.minimum;
|
|
281
|
+
hasConstraints = true;
|
|
282
|
+
}
|
|
283
|
+
if (schema.maximum !== undefined) {
|
|
284
|
+
constraints.maxValue = schema.maximum;
|
|
285
|
+
hasConstraints = true;
|
|
286
|
+
}
|
|
287
|
+
if (schema.exclusiveMinimum !== undefined) {
|
|
288
|
+
constraints.minValue = schema.exclusiveMinimum;
|
|
289
|
+
hasConstraints = true;
|
|
290
|
+
}
|
|
291
|
+
if (schema.exclusiveMaximum !== undefined) {
|
|
292
|
+
constraints.maxValue = schema.exclusiveMaximum;
|
|
293
|
+
hasConstraints = true;
|
|
294
|
+
}
|
|
295
|
+
if (schema.pattern !== undefined) {
|
|
296
|
+
constraints.pattern = schema.pattern;
|
|
297
|
+
hasConstraints = true;
|
|
298
|
+
}
|
|
299
|
+
if (schema.format !== undefined) {
|
|
300
|
+
constraints.format = schema.format;
|
|
301
|
+
hasConstraints = true;
|
|
302
|
+
}
|
|
303
|
+
if (schema.multipleOf !== undefined) {
|
|
304
|
+
constraints.multipleOf = schema.multipleOf;
|
|
305
|
+
hasConstraints = true;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return hasConstraints ? constraints : undefined;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Check for constraints that cannot be serialized to TypeSpec.
|
|
313
|
+
*/
|
|
314
|
+
function checkSerializability(
|
|
315
|
+
schema: unknown,
|
|
316
|
+
typeName: string,
|
|
317
|
+
warnings: string[],
|
|
318
|
+
): void {
|
|
319
|
+
// Zod-specific checks
|
|
320
|
+
if (isZodSchemaWithDef(schema)) {
|
|
321
|
+
const def = schema._def;
|
|
322
|
+
|
|
323
|
+
// Check for transforms
|
|
324
|
+
if (def.typeName === "ZodEffects" && def.effect?.type === "transform") {
|
|
325
|
+
warnings.push(
|
|
326
|
+
`${typeName}: .transform() is not serializable - will be ignored in emission`,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Check for refines
|
|
331
|
+
if (def.typeName === "ZodEffects" && def.effect?.type === "refinement") {
|
|
332
|
+
warnings.push(
|
|
333
|
+
`${typeName}: .refine()/.superRefine() is not serializable - will be ignored in emission`,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Check for preprocessors
|
|
338
|
+
if (def.typeName === "ZodEffects" && def.effect?.type === "preprocess") {
|
|
339
|
+
warnings.push(
|
|
340
|
+
`${typeName}: .preprocess() is not serializable - will be ignored in emission`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function isZodSchemaWithDef(
|
|
347
|
+
schema: unknown,
|
|
348
|
+
): schema is { _def: { typeName?: string; effect?: { type: string } } } {
|
|
349
|
+
return (
|
|
350
|
+
schema !== null &&
|
|
351
|
+
typeof schema === "object" &&
|
|
352
|
+
"_def" in schema &&
|
|
353
|
+
typeof (schema as Record<string, unknown>)._def === "object"
|
|
354
|
+
);
|
|
355
|
+
}
|