progressive-zod 1.3.0 → 1.4.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.
- package/dist/cli/index.js +71 -41
- package/dist/client.cjs +74 -42
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +74 -42
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +74 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +74 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -29,9 +29,11 @@ var init_amplitude = __esm({
|
|
|
29
29
|
AmplitudeStorage = class {
|
|
30
30
|
client;
|
|
31
31
|
deviceId;
|
|
32
|
+
eventName;
|
|
32
33
|
constructor(client, config) {
|
|
33
34
|
this.client = client;
|
|
34
|
-
this.deviceId = `
|
|
35
|
+
this.deviceId = `progressive-zod:${config.keyPrefix ?? "default"}`;
|
|
36
|
+
this.eventName = config.amplitudeEventName ?? "progressive-zod: results";
|
|
35
37
|
}
|
|
36
38
|
addName(_name) {
|
|
37
39
|
}
|
|
@@ -41,15 +43,15 @@ var init_amplitude = __esm({
|
|
|
41
43
|
}
|
|
42
44
|
incrConform(name, _sample) {
|
|
43
45
|
this.client.track(
|
|
44
|
-
|
|
45
|
-
{ type_name: name, result: "
|
|
46
|
+
this.eventName,
|
|
47
|
+
{ type_name: name, result: "conforms" },
|
|
46
48
|
{ device_id: this.deviceId }
|
|
47
49
|
);
|
|
48
50
|
}
|
|
49
51
|
incrViolate(name, sample, errors) {
|
|
50
52
|
const properties = {
|
|
51
53
|
type_name: name,
|
|
52
|
-
result: "
|
|
54
|
+
result: "violation"
|
|
53
55
|
};
|
|
54
56
|
if (sample) {
|
|
55
57
|
try {
|
|
@@ -68,7 +70,7 @@ var init_amplitude = __esm({
|
|
|
68
70
|
properties.validation_errors = errors.slice(0, 1024);
|
|
69
71
|
}
|
|
70
72
|
this.client.track(
|
|
71
|
-
|
|
73
|
+
this.eventName,
|
|
72
74
|
properties,
|
|
73
75
|
{ device_id: this.deviceId }
|
|
74
76
|
);
|
|
@@ -195,6 +197,9 @@ var init_redis = __esm({
|
|
|
195
197
|
// src/cli/index.ts
|
|
196
198
|
import { Command } from "commander";
|
|
197
199
|
|
|
200
|
+
// src/storage/resolve.ts
|
|
201
|
+
import { z } from "zod";
|
|
202
|
+
|
|
198
203
|
// src/storage/memory.ts
|
|
199
204
|
var MemoryStorage = class {
|
|
200
205
|
names = /* @__PURE__ */ new Set();
|
|
@@ -254,6 +259,31 @@ var MemoryStorage = class {
|
|
|
254
259
|
};
|
|
255
260
|
|
|
256
261
|
// src/storage/resolve.ts
|
|
262
|
+
var progressiveConfigSchema = z.object({
|
|
263
|
+
storage: z.enum(["memory", "redis", "amplitude"]).optional(),
|
|
264
|
+
redisUrl: z.string().optional(),
|
|
265
|
+
keyPrefix: z.string().optional(),
|
|
266
|
+
maxViolations: z.number().optional(),
|
|
267
|
+
maxSamples: z.number().optional(),
|
|
268
|
+
dataDir: z.string().optional(),
|
|
269
|
+
amplitudeClient: z.any().optional(),
|
|
270
|
+
amplitudeEventName: z.string().optional()
|
|
271
|
+
}).superRefine((data, ctx) => {
|
|
272
|
+
if (data.amplitudeEventName && data.storage !== "amplitude") {
|
|
273
|
+
ctx.addIssue({
|
|
274
|
+
code: z.ZodIssueCode.custom,
|
|
275
|
+
message: 'amplitudeEventName can only be used when storage is "amplitude"',
|
|
276
|
+
path: ["amplitudeEventName"]
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (data.amplitudeClient && data.storage !== "amplitude") {
|
|
280
|
+
ctx.addIssue({
|
|
281
|
+
code: z.ZodIssueCode.custom,
|
|
282
|
+
message: 'amplitudeClient can only be used when storage is "amplitude"',
|
|
283
|
+
path: ["amplitudeClient"]
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
});
|
|
257
287
|
var currentConfig = {};
|
|
258
288
|
var currentStorage = null;
|
|
259
289
|
var storageFactory = async (config) => new MemoryStorage(config);
|
|
@@ -445,7 +475,7 @@ async function violationsCommand(name, opts) {
|
|
|
445
475
|
}
|
|
446
476
|
|
|
447
477
|
// src/infer-schema.ts
|
|
448
|
-
import { z } from "zod";
|
|
478
|
+
import { z as z2 } from "zod";
|
|
449
479
|
function inferSchema(samples) {
|
|
450
480
|
const objects = samples.filter(
|
|
451
481
|
(s) => typeof s === "object" && s !== null && !Array.isArray(s)
|
|
@@ -459,14 +489,14 @@ function inferSchema(samples) {
|
|
|
459
489
|
}
|
|
460
490
|
const discriminator = findDiscriminator(groups);
|
|
461
491
|
if (discriminator) {
|
|
462
|
-
return
|
|
492
|
+
return z2.discriminatedUnion(
|
|
463
493
|
discriminator,
|
|
464
494
|
groups.map(
|
|
465
495
|
(group) => buildObjectSchema(group)
|
|
466
496
|
)
|
|
467
497
|
);
|
|
468
498
|
}
|
|
469
|
-
return
|
|
499
|
+
return z2.union(
|
|
470
500
|
groups.map((group) => buildObjectSchema(group))
|
|
471
501
|
);
|
|
472
502
|
}
|
|
@@ -525,21 +555,21 @@ function buildObjectSchema(samples) {
|
|
|
525
555
|
const values = samples.map((s) => s[key]);
|
|
526
556
|
const unique = new Set(values);
|
|
527
557
|
if (unique.size === 1 && typeof values[0] === "string") {
|
|
528
|
-
shape[key] =
|
|
558
|
+
shape[key] = z2.literal(values[0]);
|
|
529
559
|
} else {
|
|
530
560
|
shape[key] = inferFieldType(values);
|
|
531
561
|
}
|
|
532
562
|
}
|
|
533
|
-
return
|
|
563
|
+
return z2.object(shape).strict();
|
|
534
564
|
}
|
|
535
565
|
function inferFieldType(values) {
|
|
536
566
|
const types = new Set(values.map(classifyValue));
|
|
537
567
|
if (types.size === 1) {
|
|
538
568
|
const type = [...types][0];
|
|
539
|
-
if (type === "string") return
|
|
540
|
-
if (type === "number") return
|
|
541
|
-
if (type === "boolean") return
|
|
542
|
-
if (type === "null") return
|
|
569
|
+
if (type === "string") return z2.string();
|
|
570
|
+
if (type === "number") return z2.number();
|
|
571
|
+
if (type === "boolean") return z2.boolean();
|
|
572
|
+
if (type === "null") return z2.null();
|
|
543
573
|
if (type === "object") {
|
|
544
574
|
return inferSchema(values);
|
|
545
575
|
}
|
|
@@ -547,21 +577,21 @@ function inferFieldType(values) {
|
|
|
547
577
|
const allItems = values.flatMap(
|
|
548
578
|
(v) => Array.isArray(v) ? v : []
|
|
549
579
|
);
|
|
550
|
-
if (allItems.length === 0) return
|
|
551
|
-
return
|
|
580
|
+
if (allItems.length === 0) return z2.array(z2.unknown());
|
|
581
|
+
return z2.array(inferFieldType(allItems));
|
|
552
582
|
}
|
|
553
583
|
}
|
|
554
584
|
if (types.size === 1 && types.has("string")) {
|
|
555
585
|
const unique = new Set(values);
|
|
556
586
|
if (unique.size === 1) {
|
|
557
|
-
return
|
|
587
|
+
return z2.literal(values[0]);
|
|
558
588
|
}
|
|
559
589
|
}
|
|
560
590
|
const members = [];
|
|
561
|
-
if (types.has("string")) members.push(
|
|
562
|
-
if (types.has("number")) members.push(
|
|
563
|
-
if (types.has("boolean")) members.push(
|
|
564
|
-
if (types.has("null")) members.push(
|
|
591
|
+
if (types.has("string")) members.push(z2.string());
|
|
592
|
+
if (types.has("number")) members.push(z2.number());
|
|
593
|
+
if (types.has("boolean")) members.push(z2.boolean());
|
|
594
|
+
if (types.has("null")) members.push(z2.null());
|
|
565
595
|
if (types.has("object")) {
|
|
566
596
|
const objs = values.filter(
|
|
567
597
|
(v) => typeof v === "object" && v !== null && !Array.isArray(v)
|
|
@@ -572,12 +602,12 @@ function inferFieldType(values) {
|
|
|
572
602
|
const arrs = values.filter(Array.isArray);
|
|
573
603
|
const allItems = arrs.flat();
|
|
574
604
|
members.push(
|
|
575
|
-
|
|
605
|
+
z2.array(allItems.length > 0 ? inferFieldType(allItems) : z2.unknown())
|
|
576
606
|
);
|
|
577
607
|
}
|
|
578
608
|
if (members.length === 1) return members[0];
|
|
579
|
-
if (members.length === 2) return
|
|
580
|
-
return
|
|
609
|
+
if (members.length === 2) return z2.union([members[0], members[1]]);
|
|
610
|
+
return z2.union(members);
|
|
581
611
|
}
|
|
582
612
|
function classifyValue(v) {
|
|
583
613
|
if (v === null) return "null";
|
|
@@ -587,33 +617,33 @@ function classifyValue(v) {
|
|
|
587
617
|
function inferPrimitiveUnion(samples) {
|
|
588
618
|
const types = new Set(samples.map(classifyValue));
|
|
589
619
|
const members = [];
|
|
590
|
-
if (types.has("string")) members.push(
|
|
591
|
-
if (types.has("number")) members.push(
|
|
592
|
-
if (types.has("boolean")) members.push(
|
|
593
|
-
if (types.has("null")) members.push(
|
|
594
|
-
if (members.length === 0) return
|
|
620
|
+
if (types.has("string")) members.push(z2.string());
|
|
621
|
+
if (types.has("number")) members.push(z2.number());
|
|
622
|
+
if (types.has("boolean")) members.push(z2.boolean());
|
|
623
|
+
if (types.has("null")) members.push(z2.null());
|
|
624
|
+
if (members.length === 0) return z2.unknown();
|
|
595
625
|
if (members.length === 1) return members[0];
|
|
596
|
-
return
|
|
626
|
+
return z2.union(members);
|
|
597
627
|
}
|
|
598
628
|
|
|
599
629
|
// src/schema-to-code.ts
|
|
600
|
-
import { z as
|
|
630
|
+
import { z as z3 } from "zod";
|
|
601
631
|
function schemaToCode(schema, indent = 0) {
|
|
602
632
|
const pad = " ".repeat(indent);
|
|
603
|
-
if (schema instanceof
|
|
604
|
-
if (schema instanceof
|
|
605
|
-
if (schema instanceof
|
|
606
|
-
if (schema instanceof
|
|
607
|
-
if (schema instanceof
|
|
608
|
-
if (schema instanceof
|
|
633
|
+
if (schema instanceof z3.ZodString) return "z.string()";
|
|
634
|
+
if (schema instanceof z3.ZodNumber) return "z.number()";
|
|
635
|
+
if (schema instanceof z3.ZodBoolean) return "z.boolean()";
|
|
636
|
+
if (schema instanceof z3.ZodNull) return "z.null()";
|
|
637
|
+
if (schema instanceof z3.ZodUnknown) return "z.unknown()";
|
|
638
|
+
if (schema instanceof z3.ZodLiteral) {
|
|
609
639
|
const val = schema.value;
|
|
610
640
|
return typeof val === "string" ? `z.literal("${val}")` : `z.literal(${val})`;
|
|
611
641
|
}
|
|
612
|
-
if (schema instanceof
|
|
642
|
+
if (schema instanceof z3.ZodArray) {
|
|
613
643
|
const inner = schemaToCode(schema.element, indent);
|
|
614
644
|
return `z.array(${inner})`;
|
|
615
645
|
}
|
|
616
|
-
if (schema instanceof
|
|
646
|
+
if (schema instanceof z3.ZodObject) {
|
|
617
647
|
const shape = schema.shape;
|
|
618
648
|
const keys = Object.keys(shape);
|
|
619
649
|
if (keys.length === 0) return "z.object({})";
|
|
@@ -626,7 +656,7 @@ function schemaToCode(schema, indent = 0) {
|
|
|
626
656
|
${fields.join("\n")}
|
|
627
657
|
${pad}})${strict}`;
|
|
628
658
|
}
|
|
629
|
-
if (schema instanceof
|
|
659
|
+
if (schema instanceof z3.ZodUnion) {
|
|
630
660
|
const opts = schema.options;
|
|
631
661
|
const members = opts.map((o) => schemaToCode(o, indent));
|
|
632
662
|
if (members.every((m) => !m.includes("\n"))) {
|
|
@@ -637,7 +667,7 @@ ${pad}})${strict}`;
|
|
|
637
667
|
${members.map((m) => `${inner}${m},`).join("\n")}
|
|
638
668
|
${pad}])`;
|
|
639
669
|
}
|
|
640
|
-
if (schema instanceof
|
|
670
|
+
if (schema instanceof z3.ZodDiscriminatedUnion) {
|
|
641
671
|
const disc = schema._def.discriminator;
|
|
642
672
|
const opts = schema._def.options;
|
|
643
673
|
const members = opts.map((o) => schemaToCode(o, indent + 1));
|
package/dist/client.cjs
CHANGED
|
@@ -39,9 +39,11 @@ var init_amplitude = __esm({
|
|
|
39
39
|
AmplitudeStorage = class {
|
|
40
40
|
client;
|
|
41
41
|
deviceId;
|
|
42
|
+
eventName;
|
|
42
43
|
constructor(client, config) {
|
|
43
44
|
this.client = client;
|
|
44
|
-
this.deviceId = `
|
|
45
|
+
this.deviceId = `progressive-zod:${config.keyPrefix ?? "default"}`;
|
|
46
|
+
this.eventName = config.amplitudeEventName ?? "progressive-zod: results";
|
|
45
47
|
}
|
|
46
48
|
addName(_name) {
|
|
47
49
|
}
|
|
@@ -51,15 +53,15 @@ var init_amplitude = __esm({
|
|
|
51
53
|
}
|
|
52
54
|
incrConform(name, _sample) {
|
|
53
55
|
this.client.track(
|
|
54
|
-
|
|
55
|
-
{ type_name: name, result: "
|
|
56
|
+
this.eventName,
|
|
57
|
+
{ type_name: name, result: "conforms" },
|
|
56
58
|
{ device_id: this.deviceId }
|
|
57
59
|
);
|
|
58
60
|
}
|
|
59
61
|
incrViolate(name, sample, errors) {
|
|
60
62
|
const properties = {
|
|
61
63
|
type_name: name,
|
|
62
|
-
result: "
|
|
64
|
+
result: "violation"
|
|
63
65
|
};
|
|
64
66
|
if (sample) {
|
|
65
67
|
try {
|
|
@@ -78,7 +80,7 @@ var init_amplitude = __esm({
|
|
|
78
80
|
properties.validation_errors = errors.slice(0, 1024);
|
|
79
81
|
}
|
|
80
82
|
this.client.track(
|
|
81
|
-
|
|
83
|
+
this.eventName,
|
|
82
84
|
properties,
|
|
83
85
|
{ device_id: this.deviceId }
|
|
84
86
|
);
|
|
@@ -115,6 +117,9 @@ __export(client_exports, {
|
|
|
115
117
|
});
|
|
116
118
|
module.exports = __toCommonJS(client_exports);
|
|
117
119
|
|
|
120
|
+
// src/storage/resolve.ts
|
|
121
|
+
var import_zod = require("zod");
|
|
122
|
+
|
|
118
123
|
// src/storage/memory.ts
|
|
119
124
|
var MemoryStorage = class {
|
|
120
125
|
names = /* @__PURE__ */ new Set();
|
|
@@ -174,6 +179,31 @@ var MemoryStorage = class {
|
|
|
174
179
|
};
|
|
175
180
|
|
|
176
181
|
// src/storage/resolve.ts
|
|
182
|
+
var progressiveConfigSchema = import_zod.z.object({
|
|
183
|
+
storage: import_zod.z.enum(["memory", "redis", "amplitude"]).optional(),
|
|
184
|
+
redisUrl: import_zod.z.string().optional(),
|
|
185
|
+
keyPrefix: import_zod.z.string().optional(),
|
|
186
|
+
maxViolations: import_zod.z.number().optional(),
|
|
187
|
+
maxSamples: import_zod.z.number().optional(),
|
|
188
|
+
dataDir: import_zod.z.string().optional(),
|
|
189
|
+
amplitudeClient: import_zod.z.any().optional(),
|
|
190
|
+
amplitudeEventName: import_zod.z.string().optional()
|
|
191
|
+
}).superRefine((data, ctx) => {
|
|
192
|
+
if (data.amplitudeEventName && data.storage !== "amplitude") {
|
|
193
|
+
ctx.addIssue({
|
|
194
|
+
code: import_zod.z.ZodIssueCode.custom,
|
|
195
|
+
message: 'amplitudeEventName can only be used when storage is "amplitude"',
|
|
196
|
+
path: ["amplitudeEventName"]
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (data.amplitudeClient && data.storage !== "amplitude") {
|
|
200
|
+
ctx.addIssue({
|
|
201
|
+
code: import_zod.z.ZodIssueCode.custom,
|
|
202
|
+
message: 'amplitudeClient can only be used when storage is "amplitude"',
|
|
203
|
+
path: ["amplitudeClient"]
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
});
|
|
177
207
|
var currentConfig = {};
|
|
178
208
|
var currentStorage = null;
|
|
179
209
|
var storageFactory = async (config) => new MemoryStorage(config);
|
|
@@ -181,7 +211,9 @@ function _setStorageFactory(factory) {
|
|
|
181
211
|
storageFactory = factory;
|
|
182
212
|
}
|
|
183
213
|
function configure(config) {
|
|
184
|
-
|
|
214
|
+
const merged = { ...currentConfig, ...config };
|
|
215
|
+
progressiveConfigSchema.parse(merged);
|
|
216
|
+
currentConfig = merged;
|
|
185
217
|
if (currentStorage) {
|
|
186
218
|
currentStorage.disconnect();
|
|
187
219
|
currentStorage = null;
|
|
@@ -392,7 +424,7 @@ _setStorageFactory(async (config, userConfig) => {
|
|
|
392
424
|
});
|
|
393
425
|
|
|
394
426
|
// src/infer-schema.ts
|
|
395
|
-
var
|
|
427
|
+
var import_zod2 = require("zod");
|
|
396
428
|
function inferSchema(samples) {
|
|
397
429
|
const objects = samples.filter(
|
|
398
430
|
(s) => typeof s === "object" && s !== null && !Array.isArray(s)
|
|
@@ -406,14 +438,14 @@ function inferSchema(samples) {
|
|
|
406
438
|
}
|
|
407
439
|
const discriminator = findDiscriminator(groups);
|
|
408
440
|
if (discriminator) {
|
|
409
|
-
return
|
|
441
|
+
return import_zod2.z.discriminatedUnion(
|
|
410
442
|
discriminator,
|
|
411
443
|
groups.map(
|
|
412
444
|
(group) => buildObjectSchema(group)
|
|
413
445
|
)
|
|
414
446
|
);
|
|
415
447
|
}
|
|
416
|
-
return
|
|
448
|
+
return import_zod2.z.union(
|
|
417
449
|
groups.map((group) => buildObjectSchema(group))
|
|
418
450
|
);
|
|
419
451
|
}
|
|
@@ -472,21 +504,21 @@ function buildObjectSchema(samples) {
|
|
|
472
504
|
const values = samples.map((s) => s[key]);
|
|
473
505
|
const unique = new Set(values);
|
|
474
506
|
if (unique.size === 1 && typeof values[0] === "string") {
|
|
475
|
-
shape[key] =
|
|
507
|
+
shape[key] = import_zod2.z.literal(values[0]);
|
|
476
508
|
} else {
|
|
477
509
|
shape[key] = inferFieldType(values);
|
|
478
510
|
}
|
|
479
511
|
}
|
|
480
|
-
return
|
|
512
|
+
return import_zod2.z.object(shape).strict();
|
|
481
513
|
}
|
|
482
514
|
function inferFieldType(values) {
|
|
483
515
|
const types = new Set(values.map(classifyValue));
|
|
484
516
|
if (types.size === 1) {
|
|
485
517
|
const type = [...types][0];
|
|
486
|
-
if (type === "string") return
|
|
487
|
-
if (type === "number") return
|
|
488
|
-
if (type === "boolean") return
|
|
489
|
-
if (type === "null") return
|
|
518
|
+
if (type === "string") return import_zod2.z.string();
|
|
519
|
+
if (type === "number") return import_zod2.z.number();
|
|
520
|
+
if (type === "boolean") return import_zod2.z.boolean();
|
|
521
|
+
if (type === "null") return import_zod2.z.null();
|
|
490
522
|
if (type === "object") {
|
|
491
523
|
return inferSchema(values);
|
|
492
524
|
}
|
|
@@ -494,21 +526,21 @@ function inferFieldType(values) {
|
|
|
494
526
|
const allItems = values.flatMap(
|
|
495
527
|
(v) => Array.isArray(v) ? v : []
|
|
496
528
|
);
|
|
497
|
-
if (allItems.length === 0) return
|
|
498
|
-
return
|
|
529
|
+
if (allItems.length === 0) return import_zod2.z.array(import_zod2.z.unknown());
|
|
530
|
+
return import_zod2.z.array(inferFieldType(allItems));
|
|
499
531
|
}
|
|
500
532
|
}
|
|
501
533
|
if (types.size === 1 && types.has("string")) {
|
|
502
534
|
const unique = new Set(values);
|
|
503
535
|
if (unique.size === 1) {
|
|
504
|
-
return
|
|
536
|
+
return import_zod2.z.literal(values[0]);
|
|
505
537
|
}
|
|
506
538
|
}
|
|
507
539
|
const members = [];
|
|
508
|
-
if (types.has("string")) members.push(
|
|
509
|
-
if (types.has("number")) members.push(
|
|
510
|
-
if (types.has("boolean")) members.push(
|
|
511
|
-
if (types.has("null")) members.push(
|
|
540
|
+
if (types.has("string")) members.push(import_zod2.z.string());
|
|
541
|
+
if (types.has("number")) members.push(import_zod2.z.number());
|
|
542
|
+
if (types.has("boolean")) members.push(import_zod2.z.boolean());
|
|
543
|
+
if (types.has("null")) members.push(import_zod2.z.null());
|
|
512
544
|
if (types.has("object")) {
|
|
513
545
|
const objs = values.filter(
|
|
514
546
|
(v) => typeof v === "object" && v !== null && !Array.isArray(v)
|
|
@@ -519,12 +551,12 @@ function inferFieldType(values) {
|
|
|
519
551
|
const arrs = values.filter(Array.isArray);
|
|
520
552
|
const allItems = arrs.flat();
|
|
521
553
|
members.push(
|
|
522
|
-
|
|
554
|
+
import_zod2.z.array(allItems.length > 0 ? inferFieldType(allItems) : import_zod2.z.unknown())
|
|
523
555
|
);
|
|
524
556
|
}
|
|
525
557
|
if (members.length === 1) return members[0];
|
|
526
|
-
if (members.length === 2) return
|
|
527
|
-
return
|
|
558
|
+
if (members.length === 2) return import_zod2.z.union([members[0], members[1]]);
|
|
559
|
+
return import_zod2.z.union(members);
|
|
528
560
|
}
|
|
529
561
|
function classifyValue(v) {
|
|
530
562
|
if (v === null) return "null";
|
|
@@ -534,33 +566,33 @@ function classifyValue(v) {
|
|
|
534
566
|
function inferPrimitiveUnion(samples) {
|
|
535
567
|
const types = new Set(samples.map(classifyValue));
|
|
536
568
|
const members = [];
|
|
537
|
-
if (types.has("string")) members.push(
|
|
538
|
-
if (types.has("number")) members.push(
|
|
539
|
-
if (types.has("boolean")) members.push(
|
|
540
|
-
if (types.has("null")) members.push(
|
|
541
|
-
if (members.length === 0) return
|
|
569
|
+
if (types.has("string")) members.push(import_zod2.z.string());
|
|
570
|
+
if (types.has("number")) members.push(import_zod2.z.number());
|
|
571
|
+
if (types.has("boolean")) members.push(import_zod2.z.boolean());
|
|
572
|
+
if (types.has("null")) members.push(import_zod2.z.null());
|
|
573
|
+
if (members.length === 0) return import_zod2.z.unknown();
|
|
542
574
|
if (members.length === 1) return members[0];
|
|
543
|
-
return
|
|
575
|
+
return import_zod2.z.union(members);
|
|
544
576
|
}
|
|
545
577
|
|
|
546
578
|
// src/schema-to-code.ts
|
|
547
|
-
var
|
|
579
|
+
var import_zod3 = require("zod");
|
|
548
580
|
function schemaToCode(schema, indent = 0) {
|
|
549
581
|
const pad = " ".repeat(indent);
|
|
550
|
-
if (schema instanceof
|
|
551
|
-
if (schema instanceof
|
|
552
|
-
if (schema instanceof
|
|
553
|
-
if (schema instanceof
|
|
554
|
-
if (schema instanceof
|
|
555
|
-
if (schema instanceof
|
|
582
|
+
if (schema instanceof import_zod3.z.ZodString) return "z.string()";
|
|
583
|
+
if (schema instanceof import_zod3.z.ZodNumber) return "z.number()";
|
|
584
|
+
if (schema instanceof import_zod3.z.ZodBoolean) return "z.boolean()";
|
|
585
|
+
if (schema instanceof import_zod3.z.ZodNull) return "z.null()";
|
|
586
|
+
if (schema instanceof import_zod3.z.ZodUnknown) return "z.unknown()";
|
|
587
|
+
if (schema instanceof import_zod3.z.ZodLiteral) {
|
|
556
588
|
const val = schema.value;
|
|
557
589
|
return typeof val === "string" ? `z.literal("${val}")` : `z.literal(${val})`;
|
|
558
590
|
}
|
|
559
|
-
if (schema instanceof
|
|
591
|
+
if (schema instanceof import_zod3.z.ZodArray) {
|
|
560
592
|
const inner = schemaToCode(schema.element, indent);
|
|
561
593
|
return `z.array(${inner})`;
|
|
562
594
|
}
|
|
563
|
-
if (schema instanceof
|
|
595
|
+
if (schema instanceof import_zod3.z.ZodObject) {
|
|
564
596
|
const shape = schema.shape;
|
|
565
597
|
const keys = Object.keys(shape);
|
|
566
598
|
if (keys.length === 0) return "z.object({})";
|
|
@@ -573,7 +605,7 @@ function schemaToCode(schema, indent = 0) {
|
|
|
573
605
|
${fields.join("\n")}
|
|
574
606
|
${pad}})${strict}`;
|
|
575
607
|
}
|
|
576
|
-
if (schema instanceof
|
|
608
|
+
if (schema instanceof import_zod3.z.ZodUnion) {
|
|
577
609
|
const opts = schema.options;
|
|
578
610
|
const members = opts.map((o) => schemaToCode(o, indent));
|
|
579
611
|
if (members.every((m) => !m.includes("\n"))) {
|
|
@@ -584,7 +616,7 @@ ${pad}})${strict}`;
|
|
|
584
616
|
${members.map((m) => `${inner}${m},`).join("\n")}
|
|
585
617
|
${pad}])`;
|
|
586
618
|
}
|
|
587
|
-
if (schema instanceof
|
|
619
|
+
if (schema instanceof import_zod3.z.ZodDiscriminatedUnion) {
|
|
588
620
|
const disc = schema._def.discriminator;
|
|
589
621
|
const opts = schema._def.options;
|
|
590
622
|
const members = opts.map((o) => schemaToCode(o, indent + 1));
|