shapecraft 1.0.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 (112) hide show
  1. package/CLAUDE.md +227 -0
  2. package/README.md +22 -0
  3. package/apps/cli/node_modules/.bin/prettier +21 -0
  4. package/apps/cli/node_modules/.bin/tsc +21 -0
  5. package/apps/cli/node_modules/.bin/tsserver +21 -0
  6. package/apps/cli/node_modules/.bin/tsx +21 -0
  7. package/apps/cli/node_modules/.bin/vitest +21 -0
  8. package/apps/cli/package.json +47 -0
  9. package/apps/cli/src/index.ts +98 -0
  10. package/apps/cli/tsconfig.cjs.json +10 -0
  11. package/apps/cli/tsconfig.esm.json +10 -0
  12. package/apps/cli/tsconfig.json +22 -0
  13. package/package.json +16 -0
  14. package/packages/core/node_modules/.bin/prettier +21 -0
  15. package/packages/core/node_modules/.bin/tsc +21 -0
  16. package/packages/core/node_modules/.bin/tsserver +21 -0
  17. package/packages/core/node_modules/.bin/tsx +21 -0
  18. package/packages/core/node_modules/.bin/vitest +21 -0
  19. package/packages/core/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  20. package/packages/core/package.json +44 -0
  21. package/packages/core/src/common/array.test.ts +19 -0
  22. package/packages/core/src/common/array.ts +15 -0
  23. package/packages/core/src/common/index.ts +5 -0
  24. package/packages/core/src/common/is.ts +23 -0
  25. package/packages/core/src/common/object.ts +35 -0
  26. package/packages/core/src/common/phantom.ts +1 -0
  27. package/packages/core/src/common/result.ts +43 -0
  28. package/packages/core/src/common/string.ts +28 -0
  29. package/packages/core/src/common/types.ts +34 -0
  30. package/packages/core/src/index.ts +1 -0
  31. package/packages/core/src/shape/annotate.ts +139 -0
  32. package/packages/core/src/shape/annotation.ts +47 -0
  33. package/packages/core/src/shape/base.ts +71 -0
  34. package/packages/core/src/shape/builder.test.ts +728 -0
  35. package/packages/core/src/shape/builder.ts +475 -0
  36. package/packages/core/src/shape/error.ts +4 -0
  37. package/packages/core/src/shape/index.ts +3 -0
  38. package/packages/core/src/shape/number.ts +118 -0
  39. package/packages/core/src/shape/shape.test.ts +792 -0
  40. package/packages/core/src/shape/shape.ts +377 -0
  41. package/packages/core/src/shape/tags.ts +14 -0
  42. package/packages/core/src/shape/transforms/index.ts +3 -0
  43. package/packages/core/src/shape/transforms/json-schema/index.ts +2 -0
  44. package/packages/core/src/shape/transforms/json-schema/transform.test.ts +850 -0
  45. package/packages/core/src/shape/transforms/json-schema/transform.ts +882 -0
  46. package/packages/core/src/shape/transforms/json-schema/types.ts +132 -0
  47. package/packages/core/src/shape/transforms/sql/dialects/dialect.ts +89 -0
  48. package/packages/core/src/shape/transforms/sql/dialects/index.ts +14 -0
  49. package/packages/core/src/shape/transforms/sql/dialects/postgres.ts +392 -0
  50. package/packages/core/src/shape/transforms/sql/dialects/sqlite.ts +333 -0
  51. package/packages/core/src/shape/transforms/sql/from-sql.test.ts +704 -0
  52. package/packages/core/src/shape/transforms/sql/from-sql.ts +210 -0
  53. package/packages/core/src/shape/transforms/sql/index.ts +3 -0
  54. package/packages/core/src/shape/transforms/sql/options.ts +6 -0
  55. package/packages/core/src/shape/transforms/sql/parser/check-decoder.ts +457 -0
  56. package/packages/core/src/shape/transforms/sql/parser/create-domain.ts +105 -0
  57. package/packages/core/src/shape/transforms/sql/parser/create-table.ts +809 -0
  58. package/packages/core/src/shape/transforms/sql/parser/create-type.ts +91 -0
  59. package/packages/core/src/shape/transforms/sql/parser/cursor.ts +179 -0
  60. package/packages/core/src/shape/transforms/sql/parser/default-decoder.ts +129 -0
  61. package/packages/core/src/shape/transforms/sql/parser/lexer.ts +289 -0
  62. package/packages/core/src/shape/transforms/sql/parser/pg-types.ts +247 -0
  63. package/packages/core/src/shape/transforms/sql/parser/sqlite-types.ts +103 -0
  64. package/packages/core/src/shape/transforms/sql/parser/statements.ts +127 -0
  65. package/packages/core/src/shape/transforms/sql/parser/type-spec.ts +159 -0
  66. package/packages/core/src/shape/transforms/sql/transform.sqlite.test.ts +448 -0
  67. package/packages/core/src/shape/transforms/sql/transform.test.ts +880 -0
  68. package/packages/core/src/shape/transforms/sql/transform.ts +295 -0
  69. package/packages/core/src/shape/transforms/typescript/index.ts +1 -0
  70. package/packages/core/src/shape/transforms/typescript/transform.ts +211 -0
  71. package/packages/core/src/shape/tuple.test.ts +171 -0
  72. package/packages/core/src/shape/validate.ts +413 -0
  73. package/packages/core/tsconfig.cjs.json +11 -0
  74. package/packages/core/tsconfig.esm.json +10 -0
  75. package/packages/core/tsconfig.json +23 -0
  76. package/packages/samples/node_modules/.bin/prettier +21 -0
  77. package/packages/samples/node_modules/.bin/tsc +21 -0
  78. package/packages/samples/node_modules/.bin/tsserver +21 -0
  79. package/packages/samples/node_modules/.bin/tsx +21 -0
  80. package/packages/samples/node_modules/.bin/vitest +21 -0
  81. package/packages/samples/package.json +47 -0
  82. package/packages/samples/src/blog.ts +49 -0
  83. package/packages/samples/src/config.ts +50 -0
  84. package/packages/samples/src/ecommerce.ts +65 -0
  85. package/packages/samples/src/embeddings.ts +43 -0
  86. package/packages/samples/src/events.ts +52 -0
  87. package/packages/samples/src/geometry.ts +62 -0
  88. package/packages/samples/src/index.ts +9 -0
  89. package/packages/samples/src/relational.ts +17 -0
  90. package/packages/samples/src/tuples.ts +67 -0
  91. package/packages/samples/src/user.ts +9 -0
  92. package/packages/samples/tsconfig.cjs.json +11 -0
  93. package/packages/samples/tsconfig.esm.json +10 -0
  94. package/packages/samples/tsconfig.json +23 -0
  95. package/pnpm-workspace.yaml +3 -0
  96. package/test-data/json-schema/address.json +35 -0
  97. package/test-data/json-schema/array-of-things.json +36 -0
  98. package/test-data/json-schema/basic.json +21 -0
  99. package/test-data/json-schema/blog-post.json +29 -0
  100. package/test-data/json-schema/calendar.json +48 -0
  101. package/test-data/json-schema/complex-object-with-nested-properties.json +41 -0
  102. package/test-data/json-schema/ecommerce-complex.json +344 -0
  103. package/test-data/json-schema/ecommerce-system.json +27 -0
  104. package/test-data/json-schema/enumerated-values.json +11 -0
  105. package/test-data/json-schema/fstab-entry.json +92 -0
  106. package/test-data/json-schema/geographical-location.json +20 -0
  107. package/test-data/json-schema/health-record.json +41 -0
  108. package/test-data/json-schema/job-posting.json +33 -0
  109. package/test-data/json-schema/movie.json +35 -0
  110. package/test-data/json-schema/regular-expression-pattern.json +12 -0
  111. package/test-data/json-schema/user-profile.json +33 -0
  112. package/test-data/sql/ecommerce.sql +641 -0
@@ -0,0 +1,850 @@
1
+ import { describe, expect } from "vitest";
2
+ import { annotate, shapes } from "../../shape";
3
+ import { fromJSONSchema, toJSONSchema } from "./transform";
4
+
5
+ describe("toJSONSchema — scalars", (it) => {
6
+ it("string → { type: 'string' }", () => {
7
+ expect(toJSONSchema(shapes.string())).toEqual({ type: "string" });
8
+ });
9
+
10
+ it("string with min/max → minLength/maxLength", () => {
11
+ const s = annotate.as(shapes.string(), { min: 2, max: 8 });
12
+ expect(toJSONSchema(s)).toEqual({
13
+ type: "string",
14
+ minLength: 2,
15
+ maxLength: 8,
16
+ });
17
+ });
18
+
19
+ it("string with RegExp pattern → pattern uses .source", () => {
20
+ const s = annotate.pattern(shapes.string(), /^[a-z]+$/);
21
+ expect(toJSONSchema(s)).toEqual({ type: "string", pattern: "^[a-z]+$" });
22
+ });
23
+
24
+ it("string with string-source pattern → pattern stored as-is", () => {
25
+ const s = annotate.pattern(shapes.string(), "^[A-Z]{3}$");
26
+ expect(toJSONSchema(s)).toEqual({ type: "string", pattern: "^[A-Z]{3}$" });
27
+ });
28
+
29
+ it("boolean → { type: 'boolean' }", () => {
30
+ expect(toJSONSchema(shapes.boolean())).toEqual({ type: "boolean" });
31
+ });
32
+
33
+ it("nil → { type: 'null' }", () => {
34
+ expect(toJSONSchema(shapes.nil())).toEqual({ type: "null" });
35
+ });
36
+
37
+ it("notDefined → {} with x-shapecraft.kind 'undefined'", () => {
38
+ expect(toJSONSchema(shapes.notDefined())).toEqual({
39
+ "x-shapecraft": { kind: "undefined" },
40
+ });
41
+ });
42
+
43
+ it("unknown → {}", () => {
44
+ expect(toJSONSchema(shapes.unknown())).toEqual({});
45
+ });
46
+
47
+ it("date → string + date-time format + x-shapecraft.kind 'date'", () => {
48
+ expect(toJSONSchema(shapes.date())).toEqual({
49
+ type: "string",
50
+ format: "date-time",
51
+ "x-shapecraft": { kind: "date" },
52
+ });
53
+ });
54
+
55
+ it("binary → string + contentEncoding base64 + x-shapecraft.kind 'binary'", () => {
56
+ expect(toJSONSchema(shapes.binary())).toEqual({
57
+ type: "string",
58
+ contentEncoding: "base64",
59
+ "x-shapecraft": { kind: "binary" },
60
+ });
61
+ });
62
+ });
63
+
64
+ describe("toJSONSchema — numbers", (it) => {
65
+ it("number() → type 'number' + tag preserved", () => {
66
+ expect(toJSONSchema(shapes.number())).toEqual({
67
+ type: "number",
68
+ "x-shapecraft": { numberTag: "number" },
69
+ });
70
+ });
71
+
72
+ it("int() → type 'integer' + tag preserved, no bounds", () => {
73
+ expect(toJSONSchema(shapes.int())).toEqual({
74
+ type: "integer",
75
+ "x-shapecraft": { numberTag: "int" },
76
+ });
77
+ });
78
+
79
+ it("int8 → integer with [-128, 127]", () => {
80
+ expect(toJSONSchema(shapes.int8())).toEqual({
81
+ type: "integer",
82
+ minimum: -128,
83
+ maximum: 127,
84
+ "x-shapecraft": { numberTag: "int8" },
85
+ });
86
+ });
87
+
88
+ it("uint8 → integer with [0, 255]", () => {
89
+ expect(toJSONSchema(shapes.uint8())).toEqual({
90
+ type: "integer",
91
+ minimum: 0,
92
+ maximum: 255,
93
+ "x-shapecraft": { numberTag: "uint8" },
94
+ });
95
+ });
96
+
97
+ it("int16 → integer with [-32768, 32767]", () => {
98
+ expect(toJSONSchema(shapes.int16())).toEqual({
99
+ type: "integer",
100
+ minimum: -32768,
101
+ maximum: 32767,
102
+ "x-shapecraft": { numberTag: "int16" },
103
+ });
104
+ });
105
+
106
+ it("uint32 → integer with [0, 4294967295]", () => {
107
+ expect(toJSONSchema(shapes.uint32())).toEqual({
108
+ type: "integer",
109
+ minimum: 0,
110
+ maximum: 4_294_967_295,
111
+ "x-shapecraft": { numberTag: "uint32" },
112
+ });
113
+ });
114
+
115
+ it("float32 → number with float32 bounds", () => {
116
+ const out = toJSONSchema(shapes.float32());
117
+ expect(out.type).toBe("number");
118
+ expect(out.minimum).toBeLessThan(0);
119
+ expect(out.maximum).toBeGreaterThan(0);
120
+ expect(out["x-shapecraft"]).toEqual({ numberTag: "float32" });
121
+ });
122
+
123
+ it("integer with narrower anno.min/max clamps tighter than tag range", () => {
124
+ const s = annotate.as(shapes.int16(), { min: 0, max: 100 });
125
+ expect(toJSONSchema(s)).toEqual({
126
+ type: "integer",
127
+ minimum: 0,
128
+ maximum: 100,
129
+ "x-shapecraft": { numberTag: "int16" },
130
+ });
131
+ });
132
+
133
+ it("integer with wider anno.min/max keeps the tag bound (intersection)", () => {
134
+ const s = annotate.as(shapes.uint8(), { min: -1000, max: 1000 });
135
+ expect(toJSONSchema(s)).toEqual({
136
+ type: "integer",
137
+ minimum: 0,
138
+ maximum: 255,
139
+ "x-shapecraft": { numberTag: "uint8" },
140
+ });
141
+ });
142
+
143
+ it("number literal → { const }, no type", () => {
144
+ expect(toJSONSchema(shapes.literal(7))).toEqual({ const: 7 });
145
+ });
146
+ });
147
+
148
+ describe("toJSONSchema — literals & unions", (it) => {
149
+ it("string literal → { const: 'a' }", () => {
150
+ expect(toJSONSchema(shapes.literal("a"))).toEqual({ const: "a" });
151
+ });
152
+
153
+ it("boolean literal → { const: true }", () => {
154
+ expect(toJSONSchema(shapes.literal(true))).toEqual({ const: true });
155
+ });
156
+
157
+ it("union of all string literals → enum", () => {
158
+ const u = shapes.union(
159
+ shapes.literal("a"),
160
+ shapes.literal("b"),
161
+ shapes.literal("c"),
162
+ );
163
+ expect(toJSONSchema(u)).toEqual({ enum: ["a", "b", "c"] });
164
+ });
165
+
166
+ it("union of mixed literal scalars → enum", () => {
167
+ const u = shapes.union(
168
+ shapes.literal("a"),
169
+ shapes.literal(1),
170
+ shapes.literal(true),
171
+ );
172
+ expect(toJSONSchema(u)).toEqual({ enum: ["a", 1, true] });
173
+ });
174
+
175
+ it("union with at least one non-literal → anyOf recursed", () => {
176
+ const u = shapes.union(shapes.literal("a"), shapes.number());
177
+ expect(toJSONSchema(u)).toEqual({
178
+ anyOf: [
179
+ { const: "a" },
180
+ { type: "number", "x-shapecraft": { numberTag: "number" } },
181
+ ],
182
+ });
183
+ });
184
+ });
185
+
186
+ describe("toJSONSchema — collections", (it) => {
187
+ it("array of strings with min/max → items, minItems, maxItems", () => {
188
+ const s = annotate.as(shapes.array(shapes.string()), { min: 1, max: 3 });
189
+ expect(toJSONSchema(s)).toEqual({
190
+ type: "array",
191
+ items: { type: "string" },
192
+ minItems: 1,
193
+ maxItems: 3,
194
+ });
195
+ });
196
+
197
+ it("array with anno.uniqueItems → uniqueItems: true", () => {
198
+ const s = annotate.uniqueItems(shapes.array(shapes.string()));
199
+ expect(toJSONSchema(s)).toEqual({
200
+ type: "array",
201
+ items: { type: "string" },
202
+ uniqueItems: true,
203
+ });
204
+ });
205
+
206
+ it("array with anno.unique → x-shapecraft.unique (column-level)", () => {
207
+ const s = annotate.unique(shapes.array(shapes.string()));
208
+ expect(toJSONSchema(s)).toEqual({
209
+ type: "array",
210
+ items: { type: "string" },
211
+ "x-shapecraft": { unique: true },
212
+ });
213
+ });
214
+
215
+ it("mapping with required + optional → properties present, required skips optional, additionalProperties false", () => {
216
+ const m = shapes.mapping({
217
+ a: shapes.string(),
218
+ b: annotate.optional(shapes.number()),
219
+ });
220
+ expect(toJSONSchema(m)).toEqual({
221
+ type: "object",
222
+ properties: {
223
+ a: { type: "string" },
224
+ b: { type: "number", "x-shapecraft": { numberTag: "number" } },
225
+ },
226
+ required: ["a"],
227
+ additionalProperties: false,
228
+ });
229
+ });
230
+
231
+ it("mapping with no required keys omits the required array", () => {
232
+ const m = shapes.mapping({
233
+ a: annotate.optional(shapes.string()),
234
+ });
235
+ expect(toJSONSchema(m)).toEqual({
236
+ type: "object",
237
+ properties: { a: { type: "string" } },
238
+ additionalProperties: false,
239
+ });
240
+ });
241
+
242
+ it("nested mapping recurses", () => {
243
+ const m = shapes.mapping({
244
+ outer: shapes.mapping({ inner: shapes.number() }),
245
+ });
246
+ expect(toJSONSchema(m)).toEqual({
247
+ type: "object",
248
+ properties: {
249
+ outer: {
250
+ type: "object",
251
+ properties: {
252
+ inner: { type: "number", "x-shapecraft": { numberTag: "number" } },
253
+ },
254
+ required: ["inner"],
255
+ additionalProperties: false,
256
+ },
257
+ },
258
+ required: ["outer"],
259
+ additionalProperties: false,
260
+ });
261
+ });
262
+
263
+ it("record with patterned string key → additionalProperties + propertyNames", () => {
264
+ const r = shapes.record(
265
+ annotate.pattern(shapes.string(), /^[a-z]+$/),
266
+ shapes.number(),
267
+ );
268
+ expect(toJSONSchema(r)).toEqual({
269
+ type: "object",
270
+ additionalProperties: {
271
+ type: "number",
272
+ "x-shapecraft": { numberTag: "number" },
273
+ },
274
+ propertyNames: { type: "string", pattern: "^[a-z]+$" },
275
+ });
276
+ });
277
+
278
+ it("record with number key → numeric-pattern propertyNames hint", () => {
279
+ const r = shapes.record(shapes.number(), shapes.string());
280
+ expect(toJSONSchema(r)).toEqual({
281
+ type: "object",
282
+ additionalProperties: { type: "string" },
283
+ propertyNames: { type: "string", pattern: "^-?\\d+(\\.\\d+)?$" },
284
+ });
285
+ });
286
+
287
+ it("vector(3, float32) → fixed-length array + x-shapecraft.kind 'vector'", () => {
288
+ const v = shapes.vector(3, shapes.float32());
289
+ const out = toJSONSchema(v);
290
+ expect(out.type).toBe("array");
291
+ expect(out.minItems).toBe(3);
292
+ expect(out.maxItems).toBe(3);
293
+ expect(out["x-shapecraft"]?.kind).toBe("vector");
294
+ expect(out["x-shapecraft"]?.vectorDims).toBe(3);
295
+ const items = out.items as {
296
+ type?: string;
297
+ "x-shapecraft"?: { numberTag?: string };
298
+ };
299
+ expect(items.type).toBe("number");
300
+ expect(items["x-shapecraft"]?.numberTag).toBe("float32");
301
+ });
302
+ });
303
+
304
+ describe("toJSONSchema — shapecraft-only annotations", (it) => {
305
+ it("scalar anno.unique → x-shapecraft.unique (not uniqueItems)", () => {
306
+ const s = annotate.unique(shapes.string());
307
+ expect(toJSONSchema(s)).toEqual({
308
+ type: "string",
309
+ "x-shapecraft": { unique: true },
310
+ });
311
+ });
312
+
313
+ it("anno.primary → x-shapecraft.primary", () => {
314
+ const s = annotate.primary(shapes.string());
315
+ expect(toJSONSchema(s)).toEqual({
316
+ type: "string",
317
+ "x-shapecraft": { primary: true },
318
+ });
319
+ });
320
+
321
+ it("anno.foreign → x-shapecraft.foreign round-trips", () => {
322
+ const s = annotate.foreign(shapes.string(), "users", "id");
323
+ expect(toJSONSchema(s)).toEqual({
324
+ type: "string",
325
+ "x-shapecraft": { foreign: { table: "users", column: "id" } },
326
+ });
327
+ });
328
+
329
+ it("top-level optional → x-shapecraft.optional: true", () => {
330
+ const s = annotate.optional(shapes.string());
331
+ expect(toJSONSchema(s)).toEqual({
332
+ type: "string",
333
+ "x-shapecraft": { optional: true },
334
+ });
335
+ });
336
+
337
+ it("optional as mapping value is consumed by parent (not surfaced as x-shapecraft.optional)", () => {
338
+ const m = shapes.mapping({ a: annotate.optional(shapes.string()) });
339
+ const out = toJSONSchema(m);
340
+ expect(out.required).toBeUndefined();
341
+ expect(out.properties?.a).toEqual({ type: "string" });
342
+ });
343
+ });
344
+
345
+ describe("fromJSONSchema — direct construction (lenient inputs)", (it) => {
346
+ it("{} → unknown", () => {
347
+ expect(toJSONSchema(fromJSONSchema({}))).toEqual({});
348
+ });
349
+
350
+ it("{ type: 'string' } → string", () => {
351
+ expect(toJSONSchema(fromJSONSchema({ type: "string" }))).toEqual({
352
+ type: "string",
353
+ });
354
+ });
355
+
356
+ it("{ type: 'boolean' } → boolean", () => {
357
+ expect(toJSONSchema(fromJSONSchema({ type: "boolean" }))).toEqual({
358
+ type: "boolean",
359
+ });
360
+ });
361
+
362
+ it("{ type: 'null' } → nil", () => {
363
+ expect(toJSONSchema(fromJSONSchema({ type: "null" }))).toEqual({
364
+ type: "null",
365
+ });
366
+ });
367
+
368
+ it("{ type: 'integer' } without numberTag → int", () => {
369
+ expect(toJSONSchema(fromJSONSchema({ type: "integer" }))).toEqual({
370
+ type: "integer",
371
+ "x-shapecraft": { numberTag: "int" },
372
+ });
373
+ });
374
+
375
+ it("{ type: 'number' } without numberTag → number", () => {
376
+ expect(toJSONSchema(fromJSONSchema({ type: "number" }))).toEqual({
377
+ type: "number",
378
+ "x-shapecraft": { numberTag: "number" },
379
+ });
380
+ });
381
+
382
+ it("oneOf is treated like anyOf", () => {
383
+ const out = fromJSONSchema({
384
+ oneOf: [{ type: "string" }, { type: "number" }],
385
+ });
386
+ expect(out.type).toBe("union");
387
+ expect(toJSONSchema(out)).toEqual({
388
+ anyOf: [
389
+ { type: "string" },
390
+ { type: "number", "x-shapecraft": { numberTag: "number" } },
391
+ ],
392
+ });
393
+ });
394
+
395
+ it("{ type: 'object', additionalProperties: { type: 'string' } } → record(string, string)", () => {
396
+ const out = fromJSONSchema({
397
+ type: "object",
398
+ additionalProperties: { type: "string" },
399
+ });
400
+ expect(out.type).toBe("record");
401
+ expect(toJSONSchema(out)).toEqual({
402
+ type: "object",
403
+ additionalProperties: { type: "string" },
404
+ });
405
+ });
406
+
407
+ it("{ x-shapecraft: { kind: 'undefined' } } → notDefined", () => {
408
+ const out = fromJSONSchema({ "x-shapecraft": { kind: "undefined" } });
409
+ expect(out.type).toBe("undefined");
410
+ });
411
+
412
+ it("string with format 'date-time' but no x-shapecraft → date (lenient)", () => {
413
+ const out = fromJSONSchema({ type: "string", format: "date-time" });
414
+ expect(out.type).toBe("date");
415
+ });
416
+
417
+ it("string with contentEncoding 'base64' but no x-shapecraft → binary (lenient)", () => {
418
+ const out = fromJSONSchema({ type: "string", contentEncoding: "base64" });
419
+ expect(out.type).toBe("binary");
420
+ });
421
+
422
+ it("const literal → shapes.literal", () => {
423
+ expect(toJSONSchema(fromJSONSchema({ const: "hi" }))).toEqual({
424
+ const: "hi",
425
+ });
426
+ expect(toJSONSchema(fromJSONSchema({ const: 42 }))).toEqual({ const: 42 });
427
+ expect(toJSONSchema(fromJSONSchema({ const: true }))).toEqual({
428
+ const: true,
429
+ });
430
+ });
431
+
432
+ it("enum of literals → union of literals", () => {
433
+ expect(toJSONSchema(fromJSONSchema({ enum: ["a", "b", "c"] }))).toEqual({
434
+ enum: ["a", "b", "c"],
435
+ });
436
+ });
437
+
438
+ it("array without x-shapecraft.kind → array (not vector) even if minItems=maxItems", () => {
439
+ const out = fromJSONSchema({
440
+ type: "array",
441
+ items: { type: "string" },
442
+ minItems: 3,
443
+ maxItems: 3,
444
+ });
445
+ expect(out.type).toBe("array");
446
+ expect(toJSONSchema(out)).toEqual({
447
+ type: "array",
448
+ items: { type: "string" },
449
+ minItems: 3,
450
+ maxItems: 3,
451
+ });
452
+ });
453
+ });
454
+
455
+ describe("fromJSONSchema — round-trip via toJSONSchema", (it) => {
456
+ const roundTrip = (s: shapes.Shape): void => {
457
+ const json = toJSONSchema(s);
458
+ const reconstructed = fromJSONSchema(json);
459
+ expect(toJSONSchema(reconstructed)).toEqual(json);
460
+ };
461
+
462
+ it("scalars", () => {
463
+ roundTrip(shapes.string());
464
+ roundTrip(shapes.boolean());
465
+ roundTrip(shapes.nil());
466
+ roundTrip(shapes.notDefined());
467
+ roundTrip(shapes.unknown());
468
+ roundTrip(shapes.date());
469
+ roundTrip(shapes.binary());
470
+ });
471
+
472
+ it("strings with min/max and patterns", () => {
473
+ roundTrip(annotate.as(shapes.string(), { min: 2, max: 8 }));
474
+ roundTrip(annotate.pattern(shapes.string(), /^[a-z]+$/));
475
+ roundTrip(annotate.pattern(shapes.string(), "^[A-Z]{3}$"));
476
+ });
477
+
478
+ it("every number tag", () => {
479
+ roundTrip(shapes.number());
480
+ roundTrip(shapes.int());
481
+ roundTrip(shapes.int8());
482
+ roundTrip(shapes.uint8());
483
+ roundTrip(shapes.int16());
484
+ roundTrip(shapes.uint16());
485
+ roundTrip(shapes.int32());
486
+ roundTrip(shapes.uint32());
487
+ roundTrip(shapes.int64());
488
+ roundTrip(shapes.uint64());
489
+ roundTrip(shapes.float());
490
+ roundTrip(shapes.float32());
491
+ roundTrip(shapes.float64());
492
+ });
493
+
494
+ it("integer with narrower anno.min/max", () => {
495
+ roundTrip(annotate.as(shapes.int16(), { min: 0, max: 100 }));
496
+ });
497
+
498
+ it("integer with wider anno.min/max (intersection)", () => {
499
+ roundTrip(annotate.as(shapes.uint8(), { min: -1000, max: 1000 }));
500
+ });
501
+
502
+ it("literal scalars", () => {
503
+ roundTrip(shapes.literal("a"));
504
+ roundTrip(shapes.literal(7));
505
+ roundTrip(shapes.literal(true));
506
+ });
507
+
508
+ it("union of literals (enum)", () => {
509
+ roundTrip(
510
+ shapes.union(
511
+ shapes.literal("a"),
512
+ shapes.literal("b"),
513
+ shapes.literal("c"),
514
+ ),
515
+ );
516
+ roundTrip(
517
+ shapes.union(
518
+ shapes.literal("a"),
519
+ shapes.literal(1),
520
+ shapes.literal(true),
521
+ ),
522
+ );
523
+ });
524
+
525
+ it("union with non-literal variant (anyOf)", () => {
526
+ roundTrip(shapes.union(shapes.literal("a"), shapes.number()));
527
+ });
528
+
529
+ it("arrays with min/max, uniqueItems, and column-level unique", () => {
530
+ roundTrip(annotate.as(shapes.array(shapes.string()), { min: 1, max: 3 }));
531
+ roundTrip(annotate.uniqueItems(shapes.array(shapes.string())));
532
+ roundTrip(annotate.unique(shapes.array(shapes.string())));
533
+ });
534
+
535
+ it("mappings with required, optional, nested", () => {
536
+ roundTrip(
537
+ shapes.mapping({
538
+ a: shapes.string(),
539
+ b: annotate.optional(shapes.number()),
540
+ }),
541
+ );
542
+ roundTrip(shapes.mapping({ a: annotate.optional(shapes.string()) }));
543
+ roundTrip(
544
+ shapes.mapping({
545
+ outer: shapes.mapping({ inner: shapes.number() }),
546
+ }),
547
+ );
548
+ });
549
+
550
+ it("records with patterned and numeric keys", () => {
551
+ roundTrip(
552
+ shapes.record(
553
+ annotate.pattern(shapes.string(), /^[a-z]+$/),
554
+ shapes.number(),
555
+ ),
556
+ );
557
+ roundTrip(shapes.record(shapes.number(), shapes.string()));
558
+ roundTrip(shapes.record(shapes.string(), shapes.string()));
559
+ });
560
+
561
+ it("vector(3, float32)", () => {
562
+ roundTrip(shapes.vector(3, shapes.float32()));
563
+ });
564
+
565
+ it("shapecraft-only annotations at top level", () => {
566
+ roundTrip(annotate.optional(shapes.string()));
567
+ roundTrip(annotate.primary(shapes.string()));
568
+ roundTrip(annotate.unique(shapes.string()));
569
+ roundTrip(annotate.foreign(shapes.string(), "users", "id"));
570
+ });
571
+ });
572
+
573
+ describe("fromJSONSchema — $defs / $ref / $anchor", (it) => {
574
+ it("$defs becomes a ShapeModule with one table per def", () => {
575
+ const out = fromJSONSchema({
576
+ $defs: {
577
+ product: {
578
+ type: "object",
579
+ properties: { name: { type: "string" } },
580
+ required: ["name"],
581
+ },
582
+ },
583
+ });
584
+ expect(out.type).toBe("module");
585
+ const mod = out as shapes.ShapeModule;
586
+ expect(Object.keys(mod.input)).toEqual(["product"]);
587
+ expect(mod.input.product?.type).toBe("mapping");
588
+ });
589
+
590
+ it("def with no PK-like field auto-injects id bigint identity PK", () => {
591
+ const out = fromJSONSchema({
592
+ $defs: {
593
+ product: { type: "object", properties: { name: { type: "string" } } },
594
+ },
595
+ }) as shapes.ShapeModule;
596
+ const product = mod_(out, "product");
597
+ const id = product.input.id as shapes.ShapeNumber;
598
+ expect(id?.type).toBe("number");
599
+ expect(id?.tag).toBe("int64");
600
+ expect(id?.anno.primary).toBe(true);
601
+ expect(id?.anno.autoIncrement).toBe(true);
602
+ });
603
+
604
+ it("def with a required 'id' uses it as PK without auto-inject", () => {
605
+ const out = fromJSONSchema({
606
+ $defs: {
607
+ user: {
608
+ type: "object",
609
+ properties: {
610
+ id: { type: "integer" },
611
+ name: { type: "string" },
612
+ },
613
+ required: ["id", "name"],
614
+ },
615
+ },
616
+ }) as shapes.ShapeModule;
617
+ const user = mod_(out, "user");
618
+ const id = user.input.id as shapes.ShapeNumber;
619
+ expect(id?.anno.primary).toBe(true);
620
+ expect(id?.anno.autoIncrement).toBeUndefined();
621
+ expect(Object.keys(user.input)).toEqual(["id", "name"]);
622
+ });
623
+
624
+ it("def with required '${name}Id' uses that as PK", () => {
625
+ const out = fromJSONSchema({
626
+ $defs: {
627
+ order: {
628
+ type: "object",
629
+ properties: { orderId: { type: "string" } },
630
+ required: ["orderId"],
631
+ },
632
+ },
633
+ }) as shapes.ShapeModule;
634
+ const order = mod_(out, "order");
635
+ const pk = order.input.orderId;
636
+ expect(pk?.anno.primary).toBe(true);
637
+ });
638
+
639
+ it("non-required 'id' field → error", () => {
640
+ expect(() =>
641
+ fromJSONSchema({
642
+ $defs: {
643
+ user: {
644
+ type: "object",
645
+ properties: { id: { type: "integer" } },
646
+ },
647
+ },
648
+ }),
649
+ ).toThrow(/has an 'id' field that doesn't qualify as PK/);
650
+ });
651
+
652
+ it("$ref to another def becomes FK column", () => {
653
+ const out = fromJSONSchema({
654
+ $defs: {
655
+ user: {
656
+ type: "object",
657
+ properties: { id: { type: "integer" }, name: { type: "string" } },
658
+ required: ["id"],
659
+ },
660
+ post: {
661
+ type: "object",
662
+ properties: {
663
+ id: { type: "integer" },
664
+ author: { $ref: "#/$defs/user" },
665
+ },
666
+ required: ["id"],
667
+ },
668
+ },
669
+ }) as shapes.ShapeModule;
670
+ const post = mod_(out, "post");
671
+ const author = post.input.author;
672
+ expect(author?.anno.foreign).toEqual({ table: "user", column: "id" });
673
+ expect(author?.type).toBe("number");
674
+ });
675
+
676
+ it("$anchor allows #anchor refs", () => {
677
+ const out = fromJSONSchema({
678
+ $defs: {
679
+ product: {
680
+ $anchor: "ProductSchema",
681
+ type: "object",
682
+ properties: { id: { type: "integer" } },
683
+ required: ["id"],
684
+ },
685
+ order: {
686
+ type: "object",
687
+ properties: {
688
+ id: { type: "integer" },
689
+ product: { $ref: "#ProductSchema" },
690
+ },
691
+ required: ["id"],
692
+ },
693
+ },
694
+ }) as shapes.ShapeModule;
695
+ const order = mod_(out, "order");
696
+ expect(order.input.product?.anno.foreign).toEqual({
697
+ table: "product",
698
+ column: "id",
699
+ });
700
+ });
701
+
702
+ it("array<$ref> generates a junction table; original property dropped", () => {
703
+ const out = fromJSONSchema({
704
+ $defs: {
705
+ product: { type: "object", properties: {} },
706
+ order: {
707
+ type: "object",
708
+ properties: {
709
+ items: { type: "array", items: { $ref: "#/$defs/product" } },
710
+ },
711
+ },
712
+ },
713
+ }) as shapes.ShapeModule;
714
+ expect(Object.keys(out.input).sort()).toEqual([
715
+ "order",
716
+ "order_items",
717
+ "product",
718
+ ]);
719
+ const junction = mod_(out, "order_items");
720
+ expect(Object.keys(junction.input)).toEqual(["order_id", "items_id"]);
721
+ expect(junction.input.order_id?.anno.foreign).toEqual({
722
+ table: "order",
723
+ column: "id",
724
+ });
725
+ expect(junction.input.order_id?.anno.primary).toBe(true);
726
+ expect(junction.input.items_id?.anno.foreign).toEqual({
727
+ table: "product",
728
+ column: "id",
729
+ });
730
+ expect(junction.input.items_id?.anno.primary).toBe(true);
731
+ });
732
+
733
+ it("external $ref → unknown column", () => {
734
+ const out = fromJSONSchema({
735
+ $defs: {
736
+ user: {
737
+ type: "object",
738
+ properties: {
739
+ id: { type: "integer" },
740
+ avatar: { $ref: "https://example.com/avatar.json" },
741
+ },
742
+ required: ["id"],
743
+ },
744
+ },
745
+ }) as shapes.ShapeModule;
746
+ const user = mod_(out, "user");
747
+ expect(user.input.avatar?.type).toBe("unknown");
748
+ });
749
+
750
+ it("top-level object with $defs becomes one more table in the module", () => {
751
+ const out = fromJSONSchema({
752
+ title: "blog",
753
+ type: "object",
754
+ properties: { content: { type: "string" } },
755
+ required: ["content"],
756
+ $defs: {
757
+ comment: { type: "object", properties: {} },
758
+ },
759
+ }) as shapes.ShapeModule;
760
+ expect(Object.keys(out.input).sort()).toEqual(["blog", "comment"]);
761
+ });
762
+
763
+ it("legacy 'definitions' keyword is treated like '$defs'", () => {
764
+ const out = fromJSONSchema({
765
+ definitions: {
766
+ user: {
767
+ type: "object",
768
+ properties: { id: { type: "integer" } },
769
+ required: ["id"],
770
+ },
771
+ },
772
+ });
773
+ expect(out.type).toBe("module");
774
+ expect(Object.keys((out as shapes.ShapeModule).input)).toEqual(["user"]);
775
+ });
776
+
777
+ it("$ref to a non-object def is inlined (typedef-style)", () => {
778
+ const out = fromJSONSchema({
779
+ definitions: {
780
+ Currency: { type: "string", pattern: "^[A-Z]{3}$" },
781
+ Money: {
782
+ type: "object",
783
+ properties: {
784
+ amount: { type: "number" },
785
+ currency: { $ref: "#/definitions/Currency" },
786
+ },
787
+ required: ["amount", "currency"],
788
+ },
789
+ },
790
+ }) as shapes.ShapeModule;
791
+ expect(Object.keys(out.input).sort()).toEqual(["Money"]);
792
+ const money = mod_(out, "Money");
793
+ const currency = money.input.currency;
794
+ expect(currency?.type).toBe("string");
795
+ expect(currency?.anno.foreign).toBeUndefined();
796
+ expect(currency?.anno.pattern).toBe("^[A-Z]{3}$");
797
+ });
798
+
799
+ it("$ref nested inside oneOf is resolved by ref index", () => {
800
+ const out = fromJSONSchema({
801
+ definitions: {
802
+ A: {
803
+ type: "object",
804
+ properties: { id: { type: "integer" } },
805
+ required: ["id"],
806
+ },
807
+ B: {
808
+ type: "object",
809
+ properties: { id: { type: "integer" } },
810
+ required: ["id"],
811
+ },
812
+ Either: {
813
+ type: "object",
814
+ properties: {
815
+ id: { type: "integer" },
816
+ details: {
817
+ oneOf: [{ $ref: "#/definitions/A" }, { $ref: "#/definitions/B" }],
818
+ },
819
+ },
820
+ required: ["id"],
821
+ },
822
+ },
823
+ }) as shapes.ShapeModule;
824
+ const either = mod_(out, "Either");
825
+ const details = either.input.details;
826
+ expect(details?.type).toBe("union");
827
+ });
828
+
829
+ it("module round-trips through toJSONSchema", () => {
830
+ const original = fromJSONSchema({
831
+ $defs: {
832
+ product: {
833
+ $anchor: "ProductSchema",
834
+ type: "object",
835
+ properties: { id: { type: "integer" } },
836
+ required: ["id"],
837
+ },
838
+ },
839
+ });
840
+ const schema = toJSONSchema(original);
841
+ expect(schema.$defs).toBeDefined();
842
+ expect(schema.$defs?.product).toBeDefined();
843
+ });
844
+ });
845
+
846
+ const mod_ = (m: shapes.ShapeModule, k: string): shapes.ShapeMapping => {
847
+ const t = m.input[k];
848
+ if (t === undefined) throw new Error(`missing table ${k}`);
849
+ return t;
850
+ };