progressive-zod 1.2.1 → 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 +79 -42
- package/dist/client.cjs +82 -43
- 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 +82 -43
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +82 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +82 -43
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -15,6 +15,13 @@ var amplitude_exports = {};
|
|
|
15
15
|
__export(amplitude_exports, {
|
|
16
16
|
AmplitudeStorage: () => AmplitudeStorage
|
|
17
17
|
});
|
|
18
|
+
function flattenToString(value) {
|
|
19
|
+
if (value === null || value === void 0) return String(value);
|
|
20
|
+
if (typeof value !== "object") return String(value);
|
|
21
|
+
if (Array.isArray(value)) return value.map(String).join(", ");
|
|
22
|
+
const entries = Object.entries(value);
|
|
23
|
+
return entries.map(([k, v]) => `${k}=${JSON.stringify(v)}`).join("; ");
|
|
24
|
+
}
|
|
18
25
|
var AmplitudeStorage;
|
|
19
26
|
var init_amplitude = __esm({
|
|
20
27
|
"src/storage/amplitude.ts"() {
|
|
@@ -22,9 +29,11 @@ var init_amplitude = __esm({
|
|
|
22
29
|
AmplitudeStorage = class {
|
|
23
30
|
client;
|
|
24
31
|
deviceId;
|
|
32
|
+
eventName;
|
|
25
33
|
constructor(client, config) {
|
|
26
34
|
this.client = client;
|
|
27
|
-
this.deviceId = `
|
|
35
|
+
this.deviceId = `progressive-zod:${config.keyPrefix ?? "default"}`;
|
|
36
|
+
this.eventName = config.amplitudeEventName ?? "progressive-zod: results";
|
|
28
37
|
}
|
|
29
38
|
addName(_name) {
|
|
30
39
|
}
|
|
@@ -34,15 +43,15 @@ var init_amplitude = __esm({
|
|
|
34
43
|
}
|
|
35
44
|
incrConform(name, _sample) {
|
|
36
45
|
this.client.track(
|
|
37
|
-
|
|
38
|
-
{ type_name: name, result: "
|
|
46
|
+
this.eventName,
|
|
47
|
+
{ type_name: name, result: "conforms" },
|
|
39
48
|
{ device_id: this.deviceId }
|
|
40
49
|
);
|
|
41
50
|
}
|
|
42
51
|
incrViolate(name, sample, errors) {
|
|
43
52
|
const properties = {
|
|
44
53
|
type_name: name,
|
|
45
|
-
result: "
|
|
54
|
+
result: "violation"
|
|
46
55
|
};
|
|
47
56
|
if (sample) {
|
|
48
57
|
try {
|
|
@@ -51,7 +60,7 @@ var init_amplitude = __esm({
|
|
|
51
60
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
52
61
|
properties.field_count = Object.keys(parsed).length;
|
|
53
62
|
}
|
|
54
|
-
properties.sample_preview =
|
|
63
|
+
properties.sample_preview = flattenToString(parsed).slice(0, 256);
|
|
55
64
|
} catch {
|
|
56
65
|
properties.sample_type = "unknown";
|
|
57
66
|
properties.sample_preview = sample.slice(0, 256);
|
|
@@ -61,7 +70,7 @@ var init_amplitude = __esm({
|
|
|
61
70
|
properties.validation_errors = errors.slice(0, 1024);
|
|
62
71
|
}
|
|
63
72
|
this.client.track(
|
|
64
|
-
|
|
73
|
+
this.eventName,
|
|
65
74
|
properties,
|
|
66
75
|
{ device_id: this.deviceId }
|
|
67
76
|
);
|
|
@@ -188,6 +197,9 @@ var init_redis = __esm({
|
|
|
188
197
|
// src/cli/index.ts
|
|
189
198
|
import { Command } from "commander";
|
|
190
199
|
|
|
200
|
+
// src/storage/resolve.ts
|
|
201
|
+
import { z } from "zod";
|
|
202
|
+
|
|
191
203
|
// src/storage/memory.ts
|
|
192
204
|
var MemoryStorage = class {
|
|
193
205
|
names = /* @__PURE__ */ new Set();
|
|
@@ -247,6 +259,31 @@ var MemoryStorage = class {
|
|
|
247
259
|
};
|
|
248
260
|
|
|
249
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
|
+
});
|
|
250
287
|
var currentConfig = {};
|
|
251
288
|
var currentStorage = null;
|
|
252
289
|
var storageFactory = async (config) => new MemoryStorage(config);
|
|
@@ -438,7 +475,7 @@ async function violationsCommand(name, opts) {
|
|
|
438
475
|
}
|
|
439
476
|
|
|
440
477
|
// src/infer-schema.ts
|
|
441
|
-
import { z } from "zod";
|
|
478
|
+
import { z as z2 } from "zod";
|
|
442
479
|
function inferSchema(samples) {
|
|
443
480
|
const objects = samples.filter(
|
|
444
481
|
(s) => typeof s === "object" && s !== null && !Array.isArray(s)
|
|
@@ -452,14 +489,14 @@ function inferSchema(samples) {
|
|
|
452
489
|
}
|
|
453
490
|
const discriminator = findDiscriminator(groups);
|
|
454
491
|
if (discriminator) {
|
|
455
|
-
return
|
|
492
|
+
return z2.discriminatedUnion(
|
|
456
493
|
discriminator,
|
|
457
494
|
groups.map(
|
|
458
495
|
(group) => buildObjectSchema(group)
|
|
459
496
|
)
|
|
460
497
|
);
|
|
461
498
|
}
|
|
462
|
-
return
|
|
499
|
+
return z2.union(
|
|
463
500
|
groups.map((group) => buildObjectSchema(group))
|
|
464
501
|
);
|
|
465
502
|
}
|
|
@@ -518,21 +555,21 @@ function buildObjectSchema(samples) {
|
|
|
518
555
|
const values = samples.map((s) => s[key]);
|
|
519
556
|
const unique = new Set(values);
|
|
520
557
|
if (unique.size === 1 && typeof values[0] === "string") {
|
|
521
|
-
shape[key] =
|
|
558
|
+
shape[key] = z2.literal(values[0]);
|
|
522
559
|
} else {
|
|
523
560
|
shape[key] = inferFieldType(values);
|
|
524
561
|
}
|
|
525
562
|
}
|
|
526
|
-
return
|
|
563
|
+
return z2.object(shape).strict();
|
|
527
564
|
}
|
|
528
565
|
function inferFieldType(values) {
|
|
529
566
|
const types = new Set(values.map(classifyValue));
|
|
530
567
|
if (types.size === 1) {
|
|
531
568
|
const type = [...types][0];
|
|
532
|
-
if (type === "string") return
|
|
533
|
-
if (type === "number") return
|
|
534
|
-
if (type === "boolean") return
|
|
535
|
-
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();
|
|
536
573
|
if (type === "object") {
|
|
537
574
|
return inferSchema(values);
|
|
538
575
|
}
|
|
@@ -540,21 +577,21 @@ function inferFieldType(values) {
|
|
|
540
577
|
const allItems = values.flatMap(
|
|
541
578
|
(v) => Array.isArray(v) ? v : []
|
|
542
579
|
);
|
|
543
|
-
if (allItems.length === 0) return
|
|
544
|
-
return
|
|
580
|
+
if (allItems.length === 0) return z2.array(z2.unknown());
|
|
581
|
+
return z2.array(inferFieldType(allItems));
|
|
545
582
|
}
|
|
546
583
|
}
|
|
547
584
|
if (types.size === 1 && types.has("string")) {
|
|
548
585
|
const unique = new Set(values);
|
|
549
586
|
if (unique.size === 1) {
|
|
550
|
-
return
|
|
587
|
+
return z2.literal(values[0]);
|
|
551
588
|
}
|
|
552
589
|
}
|
|
553
590
|
const members = [];
|
|
554
|
-
if (types.has("string")) members.push(
|
|
555
|
-
if (types.has("number")) members.push(
|
|
556
|
-
if (types.has("boolean")) members.push(
|
|
557
|
-
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());
|
|
558
595
|
if (types.has("object")) {
|
|
559
596
|
const objs = values.filter(
|
|
560
597
|
(v) => typeof v === "object" && v !== null && !Array.isArray(v)
|
|
@@ -565,12 +602,12 @@ function inferFieldType(values) {
|
|
|
565
602
|
const arrs = values.filter(Array.isArray);
|
|
566
603
|
const allItems = arrs.flat();
|
|
567
604
|
members.push(
|
|
568
|
-
|
|
605
|
+
z2.array(allItems.length > 0 ? inferFieldType(allItems) : z2.unknown())
|
|
569
606
|
);
|
|
570
607
|
}
|
|
571
608
|
if (members.length === 1) return members[0];
|
|
572
|
-
if (members.length === 2) return
|
|
573
|
-
return
|
|
609
|
+
if (members.length === 2) return z2.union([members[0], members[1]]);
|
|
610
|
+
return z2.union(members);
|
|
574
611
|
}
|
|
575
612
|
function classifyValue(v) {
|
|
576
613
|
if (v === null) return "null";
|
|
@@ -580,33 +617,33 @@ function classifyValue(v) {
|
|
|
580
617
|
function inferPrimitiveUnion(samples) {
|
|
581
618
|
const types = new Set(samples.map(classifyValue));
|
|
582
619
|
const members = [];
|
|
583
|
-
if (types.has("string")) members.push(
|
|
584
|
-
if (types.has("number")) members.push(
|
|
585
|
-
if (types.has("boolean")) members.push(
|
|
586
|
-
if (types.has("null")) members.push(
|
|
587
|
-
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();
|
|
588
625
|
if (members.length === 1) return members[0];
|
|
589
|
-
return
|
|
626
|
+
return z2.union(members);
|
|
590
627
|
}
|
|
591
628
|
|
|
592
629
|
// src/schema-to-code.ts
|
|
593
|
-
import { z as
|
|
630
|
+
import { z as z3 } from "zod";
|
|
594
631
|
function schemaToCode(schema, indent = 0) {
|
|
595
632
|
const pad = " ".repeat(indent);
|
|
596
|
-
if (schema instanceof
|
|
597
|
-
if (schema instanceof
|
|
598
|
-
if (schema instanceof
|
|
599
|
-
if (schema instanceof
|
|
600
|
-
if (schema instanceof
|
|
601
|
-
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) {
|
|
602
639
|
const val = schema.value;
|
|
603
640
|
return typeof val === "string" ? `z.literal("${val}")` : `z.literal(${val})`;
|
|
604
641
|
}
|
|
605
|
-
if (schema instanceof
|
|
642
|
+
if (schema instanceof z3.ZodArray) {
|
|
606
643
|
const inner = schemaToCode(schema.element, indent);
|
|
607
644
|
return `z.array(${inner})`;
|
|
608
645
|
}
|
|
609
|
-
if (schema instanceof
|
|
646
|
+
if (schema instanceof z3.ZodObject) {
|
|
610
647
|
const shape = schema.shape;
|
|
611
648
|
const keys = Object.keys(shape);
|
|
612
649
|
if (keys.length === 0) return "z.object({})";
|
|
@@ -619,7 +656,7 @@ function schemaToCode(schema, indent = 0) {
|
|
|
619
656
|
${fields.join("\n")}
|
|
620
657
|
${pad}})${strict}`;
|
|
621
658
|
}
|
|
622
|
-
if (schema instanceof
|
|
659
|
+
if (schema instanceof z3.ZodUnion) {
|
|
623
660
|
const opts = schema.options;
|
|
624
661
|
const members = opts.map((o) => schemaToCode(o, indent));
|
|
625
662
|
if (members.every((m) => !m.includes("\n"))) {
|
|
@@ -630,7 +667,7 @@ ${pad}})${strict}`;
|
|
|
630
667
|
${members.map((m) => `${inner}${m},`).join("\n")}
|
|
631
668
|
${pad}])`;
|
|
632
669
|
}
|
|
633
|
-
if (schema instanceof
|
|
670
|
+
if (schema instanceof z3.ZodDiscriminatedUnion) {
|
|
634
671
|
const disc = schema._def.discriminator;
|
|
635
672
|
const opts = schema._def.options;
|
|
636
673
|
const members = opts.map((o) => schemaToCode(o, indent + 1));
|
package/dist/client.cjs
CHANGED
|
@@ -25,6 +25,13 @@ var amplitude_exports = {};
|
|
|
25
25
|
__export(amplitude_exports, {
|
|
26
26
|
AmplitudeStorage: () => AmplitudeStorage
|
|
27
27
|
});
|
|
28
|
+
function flattenToString(value) {
|
|
29
|
+
if (value === null || value === void 0) return String(value);
|
|
30
|
+
if (typeof value !== "object") return String(value);
|
|
31
|
+
if (Array.isArray(value)) return value.map(String).join(", ");
|
|
32
|
+
const entries = Object.entries(value);
|
|
33
|
+
return entries.map(([k, v]) => `${k}=${JSON.stringify(v)}`).join("; ");
|
|
34
|
+
}
|
|
28
35
|
var AmplitudeStorage;
|
|
29
36
|
var init_amplitude = __esm({
|
|
30
37
|
"src/storage/amplitude.ts"() {
|
|
@@ -32,9 +39,11 @@ var init_amplitude = __esm({
|
|
|
32
39
|
AmplitudeStorage = class {
|
|
33
40
|
client;
|
|
34
41
|
deviceId;
|
|
42
|
+
eventName;
|
|
35
43
|
constructor(client, config) {
|
|
36
44
|
this.client = client;
|
|
37
|
-
this.deviceId = `
|
|
45
|
+
this.deviceId = `progressive-zod:${config.keyPrefix ?? "default"}`;
|
|
46
|
+
this.eventName = config.amplitudeEventName ?? "progressive-zod: results";
|
|
38
47
|
}
|
|
39
48
|
addName(_name) {
|
|
40
49
|
}
|
|
@@ -44,15 +53,15 @@ var init_amplitude = __esm({
|
|
|
44
53
|
}
|
|
45
54
|
incrConform(name, _sample) {
|
|
46
55
|
this.client.track(
|
|
47
|
-
|
|
48
|
-
{ type_name: name, result: "
|
|
56
|
+
this.eventName,
|
|
57
|
+
{ type_name: name, result: "conforms" },
|
|
49
58
|
{ device_id: this.deviceId }
|
|
50
59
|
);
|
|
51
60
|
}
|
|
52
61
|
incrViolate(name, sample, errors) {
|
|
53
62
|
const properties = {
|
|
54
63
|
type_name: name,
|
|
55
|
-
result: "
|
|
64
|
+
result: "violation"
|
|
56
65
|
};
|
|
57
66
|
if (sample) {
|
|
58
67
|
try {
|
|
@@ -61,7 +70,7 @@ var init_amplitude = __esm({
|
|
|
61
70
|
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
62
71
|
properties.field_count = Object.keys(parsed).length;
|
|
63
72
|
}
|
|
64
|
-
properties.sample_preview =
|
|
73
|
+
properties.sample_preview = flattenToString(parsed).slice(0, 256);
|
|
65
74
|
} catch {
|
|
66
75
|
properties.sample_type = "unknown";
|
|
67
76
|
properties.sample_preview = sample.slice(0, 256);
|
|
@@ -71,7 +80,7 @@ var init_amplitude = __esm({
|
|
|
71
80
|
properties.validation_errors = errors.slice(0, 1024);
|
|
72
81
|
}
|
|
73
82
|
this.client.track(
|
|
74
|
-
|
|
83
|
+
this.eventName,
|
|
75
84
|
properties,
|
|
76
85
|
{ device_id: this.deviceId }
|
|
77
86
|
);
|
|
@@ -108,6 +117,9 @@ __export(client_exports, {
|
|
|
108
117
|
});
|
|
109
118
|
module.exports = __toCommonJS(client_exports);
|
|
110
119
|
|
|
120
|
+
// src/storage/resolve.ts
|
|
121
|
+
var import_zod = require("zod");
|
|
122
|
+
|
|
111
123
|
// src/storage/memory.ts
|
|
112
124
|
var MemoryStorage = class {
|
|
113
125
|
names = /* @__PURE__ */ new Set();
|
|
@@ -167,6 +179,31 @@ var MemoryStorage = class {
|
|
|
167
179
|
};
|
|
168
180
|
|
|
169
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
|
+
});
|
|
170
207
|
var currentConfig = {};
|
|
171
208
|
var currentStorage = null;
|
|
172
209
|
var storageFactory = async (config) => new MemoryStorage(config);
|
|
@@ -174,7 +211,9 @@ function _setStorageFactory(factory) {
|
|
|
174
211
|
storageFactory = factory;
|
|
175
212
|
}
|
|
176
213
|
function configure(config) {
|
|
177
|
-
|
|
214
|
+
const merged = { ...currentConfig, ...config };
|
|
215
|
+
progressiveConfigSchema.parse(merged);
|
|
216
|
+
currentConfig = merged;
|
|
178
217
|
if (currentStorage) {
|
|
179
218
|
currentStorage.disconnect();
|
|
180
219
|
currentStorage = null;
|
|
@@ -385,7 +424,7 @@ _setStorageFactory(async (config, userConfig) => {
|
|
|
385
424
|
});
|
|
386
425
|
|
|
387
426
|
// src/infer-schema.ts
|
|
388
|
-
var
|
|
427
|
+
var import_zod2 = require("zod");
|
|
389
428
|
function inferSchema(samples) {
|
|
390
429
|
const objects = samples.filter(
|
|
391
430
|
(s) => typeof s === "object" && s !== null && !Array.isArray(s)
|
|
@@ -399,14 +438,14 @@ function inferSchema(samples) {
|
|
|
399
438
|
}
|
|
400
439
|
const discriminator = findDiscriminator(groups);
|
|
401
440
|
if (discriminator) {
|
|
402
|
-
return
|
|
441
|
+
return import_zod2.z.discriminatedUnion(
|
|
403
442
|
discriminator,
|
|
404
443
|
groups.map(
|
|
405
444
|
(group) => buildObjectSchema(group)
|
|
406
445
|
)
|
|
407
446
|
);
|
|
408
447
|
}
|
|
409
|
-
return
|
|
448
|
+
return import_zod2.z.union(
|
|
410
449
|
groups.map((group) => buildObjectSchema(group))
|
|
411
450
|
);
|
|
412
451
|
}
|
|
@@ -465,21 +504,21 @@ function buildObjectSchema(samples) {
|
|
|
465
504
|
const values = samples.map((s) => s[key]);
|
|
466
505
|
const unique = new Set(values);
|
|
467
506
|
if (unique.size === 1 && typeof values[0] === "string") {
|
|
468
|
-
shape[key] =
|
|
507
|
+
shape[key] = import_zod2.z.literal(values[0]);
|
|
469
508
|
} else {
|
|
470
509
|
shape[key] = inferFieldType(values);
|
|
471
510
|
}
|
|
472
511
|
}
|
|
473
|
-
return
|
|
512
|
+
return import_zod2.z.object(shape).strict();
|
|
474
513
|
}
|
|
475
514
|
function inferFieldType(values) {
|
|
476
515
|
const types = new Set(values.map(classifyValue));
|
|
477
516
|
if (types.size === 1) {
|
|
478
517
|
const type = [...types][0];
|
|
479
|
-
if (type === "string") return
|
|
480
|
-
if (type === "number") return
|
|
481
|
-
if (type === "boolean") return
|
|
482
|
-
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();
|
|
483
522
|
if (type === "object") {
|
|
484
523
|
return inferSchema(values);
|
|
485
524
|
}
|
|
@@ -487,21 +526,21 @@ function inferFieldType(values) {
|
|
|
487
526
|
const allItems = values.flatMap(
|
|
488
527
|
(v) => Array.isArray(v) ? v : []
|
|
489
528
|
);
|
|
490
|
-
if (allItems.length === 0) return
|
|
491
|
-
return
|
|
529
|
+
if (allItems.length === 0) return import_zod2.z.array(import_zod2.z.unknown());
|
|
530
|
+
return import_zod2.z.array(inferFieldType(allItems));
|
|
492
531
|
}
|
|
493
532
|
}
|
|
494
533
|
if (types.size === 1 && types.has("string")) {
|
|
495
534
|
const unique = new Set(values);
|
|
496
535
|
if (unique.size === 1) {
|
|
497
|
-
return
|
|
536
|
+
return import_zod2.z.literal(values[0]);
|
|
498
537
|
}
|
|
499
538
|
}
|
|
500
539
|
const members = [];
|
|
501
|
-
if (types.has("string")) members.push(
|
|
502
|
-
if (types.has("number")) members.push(
|
|
503
|
-
if (types.has("boolean")) members.push(
|
|
504
|
-
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());
|
|
505
544
|
if (types.has("object")) {
|
|
506
545
|
const objs = values.filter(
|
|
507
546
|
(v) => typeof v === "object" && v !== null && !Array.isArray(v)
|
|
@@ -512,12 +551,12 @@ function inferFieldType(values) {
|
|
|
512
551
|
const arrs = values.filter(Array.isArray);
|
|
513
552
|
const allItems = arrs.flat();
|
|
514
553
|
members.push(
|
|
515
|
-
|
|
554
|
+
import_zod2.z.array(allItems.length > 0 ? inferFieldType(allItems) : import_zod2.z.unknown())
|
|
516
555
|
);
|
|
517
556
|
}
|
|
518
557
|
if (members.length === 1) return members[0];
|
|
519
|
-
if (members.length === 2) return
|
|
520
|
-
return
|
|
558
|
+
if (members.length === 2) return import_zod2.z.union([members[0], members[1]]);
|
|
559
|
+
return import_zod2.z.union(members);
|
|
521
560
|
}
|
|
522
561
|
function classifyValue(v) {
|
|
523
562
|
if (v === null) return "null";
|
|
@@ -527,33 +566,33 @@ function classifyValue(v) {
|
|
|
527
566
|
function inferPrimitiveUnion(samples) {
|
|
528
567
|
const types = new Set(samples.map(classifyValue));
|
|
529
568
|
const members = [];
|
|
530
|
-
if (types.has("string")) members.push(
|
|
531
|
-
if (types.has("number")) members.push(
|
|
532
|
-
if (types.has("boolean")) members.push(
|
|
533
|
-
if (types.has("null")) members.push(
|
|
534
|
-
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();
|
|
535
574
|
if (members.length === 1) return members[0];
|
|
536
|
-
return
|
|
575
|
+
return import_zod2.z.union(members);
|
|
537
576
|
}
|
|
538
577
|
|
|
539
578
|
// src/schema-to-code.ts
|
|
540
|
-
var
|
|
579
|
+
var import_zod3 = require("zod");
|
|
541
580
|
function schemaToCode(schema, indent = 0) {
|
|
542
581
|
const pad = " ".repeat(indent);
|
|
543
|
-
if (schema instanceof
|
|
544
|
-
if (schema instanceof
|
|
545
|
-
if (schema instanceof
|
|
546
|
-
if (schema instanceof
|
|
547
|
-
if (schema instanceof
|
|
548
|
-
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) {
|
|
549
588
|
const val = schema.value;
|
|
550
589
|
return typeof val === "string" ? `z.literal("${val}")` : `z.literal(${val})`;
|
|
551
590
|
}
|
|
552
|
-
if (schema instanceof
|
|
591
|
+
if (schema instanceof import_zod3.z.ZodArray) {
|
|
553
592
|
const inner = schemaToCode(schema.element, indent);
|
|
554
593
|
return `z.array(${inner})`;
|
|
555
594
|
}
|
|
556
|
-
if (schema instanceof
|
|
595
|
+
if (schema instanceof import_zod3.z.ZodObject) {
|
|
557
596
|
const shape = schema.shape;
|
|
558
597
|
const keys = Object.keys(shape);
|
|
559
598
|
if (keys.length === 0) return "z.object({})";
|
|
@@ -566,7 +605,7 @@ function schemaToCode(schema, indent = 0) {
|
|
|
566
605
|
${fields.join("\n")}
|
|
567
606
|
${pad}})${strict}`;
|
|
568
607
|
}
|
|
569
|
-
if (schema instanceof
|
|
608
|
+
if (schema instanceof import_zod3.z.ZodUnion) {
|
|
570
609
|
const opts = schema.options;
|
|
571
610
|
const members = opts.map((o) => schemaToCode(o, indent));
|
|
572
611
|
if (members.every((m) => !m.includes("\n"))) {
|
|
@@ -577,7 +616,7 @@ ${pad}})${strict}`;
|
|
|
577
616
|
${members.map((m) => `${inner}${m},`).join("\n")}
|
|
578
617
|
${pad}])`;
|
|
579
618
|
}
|
|
580
|
-
if (schema instanceof
|
|
619
|
+
if (schema instanceof import_zod3.z.ZodDiscriminatedUnion) {
|
|
581
620
|
const disc = schema._def.discriminator;
|
|
582
621
|
const opts = schema._def.options;
|
|
583
622
|
const members = opts.map((o) => schemaToCode(o, indent + 1));
|