@kaito-http/core 4.0.0-beta.3 → 4.0.0-beta.30

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.
@@ -0,0 +1,1003 @@
1
+ // src/schema/schema.ts
2
+ function isPrimitiveJSONValue(value) {
3
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
4
+ }
5
+ var SchemaError = class extends Error {
6
+ constructor(issues) {
7
+ const first = issues.values().next().value;
8
+ if (first === void 0) {
9
+ throw new Error("SchemaError expects at least one issue to be provided");
10
+ }
11
+ super(first.message);
12
+ this.issues = issues;
13
+ }
14
+ };
15
+ var ParseContext = class _ParseContext {
16
+ static ISSUE = Symbol("ISSUE");
17
+ ISSUE = _ParseContext.ISSUE;
18
+ #issues = /* @__PURE__ */ new Set();
19
+ addIssue(message, path) {
20
+ this.#issues.add({ message, path });
21
+ return _ParseContext.ISSUE;
22
+ }
23
+ addIssues(issues, path) {
24
+ for (const issue of issues) {
25
+ this.#issues.add({ ...issue, path: [...path, ...issue.path] });
26
+ }
27
+ return _ParseContext.ISSUE;
28
+ }
29
+ get issues() {
30
+ return this.#issues;
31
+ }
32
+ static result(fn) {
33
+ const result = _ParseContext.with(fn);
34
+ if (result.type === "FATAL" || result.issues.size > 0) {
35
+ return {
36
+ success: false,
37
+ issues: result.issues
38
+ };
39
+ }
40
+ return {
41
+ success: true,
42
+ result: result.data
43
+ };
44
+ }
45
+ static with(fn) {
46
+ const ctx = new _ParseContext();
47
+ const data = fn(ctx);
48
+ if (data === _ParseContext.ISSUE) {
49
+ return {
50
+ type: "FATAL",
51
+ issues: ctx.issues
52
+ };
53
+ }
54
+ return {
55
+ type: "PARSED",
56
+ issues: ctx.issues,
57
+ data
58
+ };
59
+ }
60
+ };
61
+ var BaseSchema = class {
62
+ /** @internal */
63
+ _input;
64
+ /** @internal */
65
+ _output;
66
+ def;
67
+ getSchemaObject() {
68
+ const schema = {};
69
+ if (this.def.description !== void 0) {
70
+ schema.description = this.def.description;
71
+ }
72
+ if (this.def.example !== void 0) {
73
+ schema.example = this.def.example;
74
+ }
75
+ return schema;
76
+ }
77
+ clone(def) {
78
+ return new this.constructor({
79
+ ...this.def,
80
+ ...def
81
+ });
82
+ }
83
+ constructor(def) {
84
+ this.def = def;
85
+ }
86
+ or(other) {
87
+ return k.union([this, other]);
88
+ }
89
+ example(example) {
90
+ if (example === void 0) {
91
+ return this.def.example;
92
+ }
93
+ return this.clone({ example });
94
+ }
95
+ description(description) {
96
+ if (description === void 0) {
97
+ return this.def.description;
98
+ }
99
+ return this.clone({ description });
100
+ }
101
+ };
102
+ var STRING_FORMAT_REGEXES = {
103
+ uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i,
104
+ email: /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i,
105
+ ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
106
+ ipv6: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/,
107
+ date: /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/,
108
+ uri: /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/.+|^[a-zA-Z][a-zA-Z0-9+.-]*:[^\/].+/,
109
+ hostname: /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
110
+ };
111
+ var KString = class _KString extends BaseSchema {
112
+ static create = () => new _KString({});
113
+ serialize(value) {
114
+ return value;
115
+ }
116
+ setCheck(check) {
117
+ return this.clone({ [check.type]: check });
118
+ }
119
+ toOpenAPI() {
120
+ const baseSchema = this.getSchemaObject();
121
+ const schema = {
122
+ ...baseSchema,
123
+ type: "string"
124
+ };
125
+ if (this.def.regex) {
126
+ schema.pattern = this.def.regex.regex.source;
127
+ }
128
+ if (this.def.format) {
129
+ schema.format = this.def.format.format;
130
+ }
131
+ if (this.def.min !== void 0) {
132
+ schema.minLength = this.def.min.val;
133
+ }
134
+ if (this.def.max !== void 0) {
135
+ schema.maxLength = this.def.max.val;
136
+ }
137
+ return schema;
138
+ }
139
+ /**
140
+ * Sets the minimum length of the string
141
+ *
142
+ * @param min The minimum length of the string
143
+ * @returns A clone of the schema with the minimum length set
144
+ */
145
+ min(min, message) {
146
+ return this.setCheck({ type: "min", val: min, message });
147
+ }
148
+ /**
149
+ * Sets the maximum length of the string
150
+ *
151
+ * @param max The maximum length of the string
152
+ * @returns A clone of the schema with the maximum length set
153
+ */
154
+ max(max, message) {
155
+ return this.setCheck({ type: "max", val: max, message });
156
+ }
157
+ regex(regex, message) {
158
+ return this.setCheck({ type: "regex", regex, message });
159
+ }
160
+ startsWith(prefix, message) {
161
+ return this.setCheck({ type: "startsWith", prefix, message });
162
+ }
163
+ endsWith(suffix, message) {
164
+ return this.setCheck({ type: "endsWith", suffix, message });
165
+ }
166
+ format(format, message) {
167
+ return this.setCheck({ type: "format", format, message });
168
+ }
169
+ uri(message) {
170
+ return this.format("uri", message);
171
+ }
172
+ /**
173
+ * Deprecated because OpenAPI uses the term "uri"
174
+ * but this method exists for making migration from
175
+ * Zod easier.
176
+ *
177
+ * @deprecated Use {@link uri} instead
178
+ */
179
+ url(message) {
180
+ return this.uri(message);
181
+ }
182
+ email(message) {
183
+ return this.format("email", message);
184
+ }
185
+ uuid(message) {
186
+ return this.format("uuid", message);
187
+ }
188
+ ipv4(message) {
189
+ return this.format("ipv4", message);
190
+ }
191
+ ipv6(message) {
192
+ return this.format("ipv6", message);
193
+ }
194
+ date(message) {
195
+ return this.format("date", message);
196
+ }
197
+ dateTime(message) {
198
+ return this.format("date-time", message);
199
+ }
200
+ password(message) {
201
+ return this.format("password", message);
202
+ }
203
+ byte(message) {
204
+ return this.format("byte", message);
205
+ }
206
+ binary(message) {
207
+ return this.format("binary", message);
208
+ }
209
+ hostname(message) {
210
+ return this.format("hostname", message);
211
+ }
212
+ parseSafe(json) {
213
+ return ParseContext.result((ctx) => {
214
+ if (typeof json !== "string") {
215
+ return ctx.addIssue("Expected string", []);
216
+ }
217
+ if (this.def.min !== void 0 && json.length < this.def.min.val) {
218
+ ctx.addIssue(this.def.min.message ?? `String must be at least ${this.def.min.val} characters`, []);
219
+ }
220
+ if (this.def.max !== void 0 && json.length > this.def.max.val) {
221
+ ctx.addIssue(this.def.max.message ?? `String must be at most ${this.def.max.val} characters`, []);
222
+ }
223
+ if (this.def.regex !== void 0 && !this.def.regex.regex.test(json)) {
224
+ ctx.addIssue(this.def.regex.message ?? `String must match ${this.def.regex.regex.source}`, []);
225
+ }
226
+ if (this.def.startsWith !== void 0 && !json.startsWith(this.def.startsWith.prefix)) {
227
+ ctx.addIssue(this.def.startsWith.message ?? `String must start with "${this.def.startsWith.prefix}"`, []);
228
+ }
229
+ if (this.def.endsWith !== void 0 && !json.endsWith(this.def.endsWith.suffix)) {
230
+ ctx.addIssue(this.def.endsWith.message ?? `String must end with "${this.def.endsWith.suffix}"`, []);
231
+ }
232
+ if (this.def.format !== void 0) {
233
+ switch (this.def.format.format) {
234
+ case "uuid":
235
+ if (!STRING_FORMAT_REGEXES.uuid.test(json)) {
236
+ ctx.addIssue(this.def.format.message ?? "Invalid UUID format", []);
237
+ }
238
+ break;
239
+ case "email":
240
+ if (!STRING_FORMAT_REGEXES.email.test(json)) {
241
+ ctx.addIssue(this.def.format.message ?? "Invalid email format", []);
242
+ }
243
+ break;
244
+ case "ipv4":
245
+ if (!STRING_FORMAT_REGEXES.ipv4.test(json)) {
246
+ ctx.addIssue(this.def.format.message ?? "Invalid IPv4 address", []);
247
+ }
248
+ break;
249
+ case "ipv6":
250
+ if (!STRING_FORMAT_REGEXES.ipv6.test(json)) {
251
+ ctx.addIssue(this.def.format.message ?? "Invalid IPv6 address", []);
252
+ }
253
+ break;
254
+ case "date":
255
+ if (!STRING_FORMAT_REGEXES.date.test(json)) {
256
+ ctx.addIssue(this.def.format.message ?? "Invalid date format", []);
257
+ }
258
+ break;
259
+ case "date-time":
260
+ if (Number.isNaN(new Date(json).getTime())) {
261
+ ctx.addIssue(this.def.format.message ?? "Invalid date-time format", []);
262
+ }
263
+ break;
264
+ case "byte":
265
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(json) || json.length % 4 !== 0) {
266
+ ctx.addIssue(this.def.format.message ?? "Invalid base64 format", []);
267
+ }
268
+ break;
269
+ case "uri":
270
+ if (!STRING_FORMAT_REGEXES.uri.test(json)) {
271
+ ctx.addIssue(this.def.format.message ?? "Invalid URI format", []);
272
+ }
273
+ break;
274
+ case "hostname":
275
+ if (!STRING_FORMAT_REGEXES.hostname.test(json)) {
276
+ ctx.addIssue(this.def.format.message ?? "Invalid hostname format", []);
277
+ }
278
+ break;
279
+ case "binary":
280
+ break;
281
+ case "password":
282
+ break;
283
+ default:
284
+ this.def.format.format;
285
+ break;
286
+ }
287
+ }
288
+ return json;
289
+ });
290
+ }
291
+ parse(json) {
292
+ const result = this.parseSafe(json);
293
+ if (!result.success) {
294
+ throw new SchemaError(result.issues);
295
+ }
296
+ return result.result;
297
+ }
298
+ visit() {
299
+ }
300
+ };
301
+ var KNumber = class _KNumber extends BaseSchema {
302
+ static create = () => new _KNumber({});
303
+ serialize(value) {
304
+ return value;
305
+ }
306
+ setCheck(check) {
307
+ return this.clone({ [check.type]: check });
308
+ }
309
+ toOpenAPI() {
310
+ const baseSchema = this.getSchemaObject();
311
+ const schema = {
312
+ ...baseSchema,
313
+ type: "number"
314
+ };
315
+ if (this.def.min !== void 0) {
316
+ schema.minimum = this.def.min.val;
317
+ }
318
+ if (this.def.max !== void 0) {
319
+ schema.maximum = this.def.max.val;
320
+ }
321
+ if (this.def.multipleOf !== void 0) {
322
+ schema.multipleOf = this.def.multipleOf.val;
323
+ }
324
+ if (this.def.integer) {
325
+ schema.type = "integer";
326
+ }
327
+ if (this.def.format) {
328
+ switch (this.def.format.format) {
329
+ case "float":
330
+ schema.format = "float";
331
+ schema.type = "number";
332
+ break;
333
+ case "double":
334
+ schema.format = "double";
335
+ schema.type = "number";
336
+ break;
337
+ case "int32":
338
+ schema.format = "int32";
339
+ schema.type = "integer";
340
+ break;
341
+ case "int64":
342
+ schema.format = "int64";
343
+ schema.type = "integer";
344
+ break;
345
+ }
346
+ }
347
+ if (this.def.format) {
348
+ schema.format = this.def.format.format;
349
+ }
350
+ return schema;
351
+ }
352
+ min(min) {
353
+ return this.setCheck({ type: "min", val: min });
354
+ }
355
+ max(max) {
356
+ return this.setCheck({ type: "max", val: max });
357
+ }
358
+ integer() {
359
+ return this.setCheck({ type: "integer" });
360
+ }
361
+ multipleOf(multipleOf) {
362
+ if (multipleOf <= 0) {
363
+ throw new Error("multipleOf must be a positive number");
364
+ }
365
+ return this.setCheck({ type: "multipleOf", val: multipleOf });
366
+ }
367
+ float() {
368
+ return this.setCheck({ type: "format", format: "float" });
369
+ }
370
+ double() {
371
+ return this.setCheck({ type: "format", format: "double" });
372
+ }
373
+ int32() {
374
+ return this.setCheck({ type: "format", format: "int32" }).integer();
375
+ }
376
+ int64() {
377
+ return this.setCheck({ type: "format", format: "int64" }).integer();
378
+ }
379
+ parseSafe(json) {
380
+ return ParseContext.result((ctx) => {
381
+ if (typeof json !== "number") {
382
+ return ctx.addIssue("Expected number", []);
383
+ }
384
+ if (this.def.integer && !Number.isInteger(json)) {
385
+ ctx.addIssue(this.def.integer.message ?? "Expected integer", []);
386
+ }
387
+ if (this.def.min !== void 0 && json < this.def.min.val) {
388
+ ctx.addIssue(this.def.min.message ?? `Number must be greater than or equal to ${this.def.min.val}`, []);
389
+ }
390
+ if (this.def.max !== void 0 && json > this.def.max.val) {
391
+ ctx.addIssue(this.def.max.message ?? `Number must be less than or equal to ${this.def.max.val}`, []);
392
+ }
393
+ if (this.def.multipleOf !== void 0 && json % this.def.multipleOf.val !== 0) {
394
+ ctx.addIssue(this.def.multipleOf.message ?? `Number must be multiple of ${this.def.multipleOf.val}`, []);
395
+ }
396
+ return json;
397
+ });
398
+ }
399
+ parse(json) {
400
+ const result = this.parseSafe(json);
401
+ if (!result.success) {
402
+ throw new SchemaError(result.issues);
403
+ }
404
+ return result.result;
405
+ }
406
+ visit() {
407
+ }
408
+ };
409
+ var KBoolean = class _KBoolean extends BaseSchema {
410
+ static create = () => new _KBoolean({});
411
+ serialize(value) {
412
+ return value;
413
+ }
414
+ toOpenAPI() {
415
+ const baseSchema = this.getSchemaObject();
416
+ return {
417
+ ...baseSchema,
418
+ type: "boolean"
419
+ };
420
+ }
421
+ parseSafe(json) {
422
+ return ParseContext.result((ctx) => {
423
+ if (typeof json !== "boolean") {
424
+ return ctx.addIssue("Expected boolean", []);
425
+ }
426
+ return json;
427
+ });
428
+ }
429
+ parse(json) {
430
+ const result = this.parseSafe(json);
431
+ if (!result.success) {
432
+ throw new SchemaError(result.issues);
433
+ }
434
+ return result.result;
435
+ }
436
+ visit() {
437
+ }
438
+ };
439
+ var KArray = class _KArray extends BaseSchema {
440
+ static create = (items) => new _KArray({ items });
441
+ serialize(value) {
442
+ return value.map((item) => this.def.items.serialize(item));
443
+ }
444
+ setCheck(check) {
445
+ return this.clone({ [check.type]: check });
446
+ }
447
+ toOpenAPI() {
448
+ const baseSchema = this.getSchemaObject();
449
+ return {
450
+ ...baseSchema,
451
+ type: "array",
452
+ items: this.def.items.toOpenAPI(),
453
+ ...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
454
+ ...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
455
+ ...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
456
+ };
457
+ }
458
+ min(minItems) {
459
+ return this.setCheck({ type: "minItems", val: minItems });
460
+ }
461
+ max(maxItems) {
462
+ return this.setCheck({ type: "maxItems", val: maxItems });
463
+ }
464
+ unique() {
465
+ return this.setCheck({ type: "uniqueItems", val: true });
466
+ }
467
+ notUnique() {
468
+ return this.setCheck({ type: "uniqueItems", val: false });
469
+ }
470
+ parseSafe(json) {
471
+ return ParseContext.result((ctx) => {
472
+ if (!Array.isArray(json)) {
473
+ return ctx.addIssue("Expected array", []);
474
+ }
475
+ if (this.def.minItems !== void 0 && json.length < this.def.minItems.val) {
476
+ ctx.addIssue(this.def.minItems.message ?? `Array must have at least ${this.def.minItems.val} items`, []);
477
+ }
478
+ if (this.def.maxItems !== void 0 && json.length > this.def.maxItems.val) {
479
+ ctx.addIssue(this.def.maxItems.message ?? `Array must have at most ${this.def.maxItems.val} items`, []);
480
+ }
481
+ if (this.def.uniqueItems?.val === true && new Set(json).size !== json.length) {
482
+ ctx.addIssue(this.def.uniqueItems.message ?? "Array items must be unique", []);
483
+ }
484
+ const items = [];
485
+ for (let i = 0; i < json.length; i++) {
486
+ const item = json[i];
487
+ const result = this.def.items.parseSafe(item);
488
+ if (!result.success) {
489
+ ctx.addIssues(result.issues, [i.toString()]);
490
+ } else {
491
+ items.push(result.result);
492
+ }
493
+ }
494
+ return items;
495
+ });
496
+ }
497
+ parse(json) {
498
+ const result = this.parseSafe(json);
499
+ if (!result.success) {
500
+ throw new SchemaError(result.issues);
501
+ }
502
+ return result.result;
503
+ }
504
+ visit(visitor) {
505
+ const child = this.def.items;
506
+ visitor(child);
507
+ child.visit(visitor);
508
+ }
509
+ };
510
+ var KNull = class _KNull extends BaseSchema {
511
+ static create = () => new _KNull({});
512
+ serialize(value) {
513
+ return value;
514
+ }
515
+ toOpenAPI() {
516
+ const baseSchema = this.getSchemaObject();
517
+ return {
518
+ ...baseSchema,
519
+ type: "null"
520
+ };
521
+ }
522
+ parseSafe(json) {
523
+ return ParseContext.result((ctx) => {
524
+ if (json !== null) {
525
+ return ctx.addIssue("Expected null", []);
526
+ }
527
+ return null;
528
+ });
529
+ }
530
+ parse(json) {
531
+ const result = this.parseSafe(json);
532
+ if (!result.success) {
533
+ throw new SchemaError(result.issues);
534
+ }
535
+ return result.result;
536
+ }
537
+ visit() {
538
+ }
539
+ };
540
+ var KObject = class _KObject extends BaseSchema {
541
+ static create = (shape) => new _KObject({ shape });
542
+ serialize(value) {
543
+ const result = {};
544
+ for (const key in this.def.shape) {
545
+ if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
546
+ const fieldValue = value[key];
547
+ if (fieldValue === void 0) {
548
+ throw new Error(`Missing required property: ${key}`);
549
+ }
550
+ result[key] = this.def.shape[key].serialize(fieldValue);
551
+ }
552
+ }
553
+ return result;
554
+ }
555
+ toOpenAPI() {
556
+ const baseSchema = this.getSchemaObject();
557
+ return {
558
+ ...baseSchema,
559
+ type: "object",
560
+ properties: Object.fromEntries(
561
+ Object.entries(this.def.shape).map((entry) => {
562
+ const [key, value] = entry;
563
+ return [key, value.toOpenAPI()];
564
+ })
565
+ ),
566
+ required: Object.keys(this.def.shape)
567
+ };
568
+ }
569
+ parseSafe(json) {
570
+ return ParseContext.result((ctx) => {
571
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
572
+ return ctx.addIssue(`Expected object, got ${typeof json}`, []);
573
+ }
574
+ const result = {};
575
+ for (const key in this.def.shape) {
576
+ if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
577
+ const value = json[key];
578
+ if (value === void 0) {
579
+ return ctx.addIssue(`Missing required property: ${key}`, [key]);
580
+ }
581
+ const parseResult = this.def.shape[key].parseSafe(value);
582
+ if (!parseResult.success) {
583
+ return ctx.addIssues(parseResult.issues, [key]);
584
+ }
585
+ result[key] = parseResult.result;
586
+ }
587
+ }
588
+ return result;
589
+ });
590
+ }
591
+ parse(json) {
592
+ const result = this.parseSafe(json);
593
+ if (!result.success) {
594
+ throw new SchemaError(result.issues);
595
+ }
596
+ return result.result;
597
+ }
598
+ get shape() {
599
+ return this.def.shape;
600
+ }
601
+ visit(visitor) {
602
+ for (const child of Object.values(this.def.shape)) {
603
+ visitor(child);
604
+ child.visit(visitor);
605
+ }
606
+ }
607
+ };
608
+ var KObjectFromURLSearchParams = class _KObjectFromURLSearchParams extends KObject {
609
+ static create = (shape) => new _KObjectFromURLSearchParams({ shape });
610
+ serialize(value) {
611
+ return super.serialize(value);
612
+ }
613
+ toOpenAPI() {
614
+ return super.toOpenAPI();
615
+ }
616
+ parseSafe(json) {
617
+ return ParseContext.result((ctx) => {
618
+ if (!(json instanceof URLSearchParams)) {
619
+ return ctx.addIssue(`Expected URLSearchParams, got ${typeof json}`, []);
620
+ }
621
+ const result = {};
622
+ for (const key in this.def.shape) {
623
+ if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
624
+ const value = json.get(key);
625
+ if (value === null) {
626
+ return ctx.addIssue(`Missing required property: ${key}`, [key]);
627
+ }
628
+ const parseResult = this.def.shape[key].parseSafe(value);
629
+ if (!parseResult.success) {
630
+ return ctx.addIssues(parseResult.issues, [key]);
631
+ }
632
+ result[key] = parseResult.result;
633
+ }
634
+ }
635
+ return result;
636
+ });
637
+ }
638
+ };
639
+ var KRef = class _KRef extends BaseSchema {
640
+ static create = (name, shape) => new _KRef({ name, shape });
641
+ serialize(value) {
642
+ const result = {};
643
+ for (const key in this.def.shape) {
644
+ if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
645
+ const fieldValue = value[key];
646
+ if (fieldValue === void 0) {
647
+ throw new Error(`Missing required property: ${key}`);
648
+ }
649
+ result[key] = this.def.shape[key].serialize(fieldValue);
650
+ }
651
+ }
652
+ return result;
653
+ }
654
+ visit(visitor) {
655
+ for (const child of Object.values(this.def.shape)) {
656
+ visitor(child);
657
+ child.visit(visitor);
658
+ }
659
+ }
660
+ example() {
661
+ throw new Error("Cannot set an example on a KRef");
662
+ }
663
+ toOpenAPI() {
664
+ return {
665
+ $ref: `#/components/schemas/${this.def.name}`,
666
+ ...this.def.description ? { description: this.def.description } : {},
667
+ ...this.def.summary ? { summary: this.def.summary } : {}
668
+ };
669
+ }
670
+ parseSafe(json) {
671
+ return ParseContext.result((ctx) => {
672
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
673
+ return ctx.addIssue(`Expected object, got ${typeof json}`, []);
674
+ }
675
+ const result = {};
676
+ for (const key in this.def.shape) {
677
+ if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
678
+ const value = json[key];
679
+ if (value === void 0) {
680
+ return ctx.addIssue(`Missing required property: ${key}`, [key]);
681
+ }
682
+ const parseResult = this.def.shape[key].parseSafe(value);
683
+ if (!parseResult.success) {
684
+ return ctx.addIssues(parseResult.issues, [key]);
685
+ }
686
+ result[key] = parseResult.result;
687
+ }
688
+ }
689
+ return result;
690
+ });
691
+ }
692
+ parse(json) {
693
+ const result = this.parseSafe(json);
694
+ if (!result.success) {
695
+ throw new SchemaError(result.issues);
696
+ }
697
+ return result.result;
698
+ }
699
+ summary(summary) {
700
+ if (summary === void 0) {
701
+ return this.def.summary;
702
+ }
703
+ return this.clone({ summary });
704
+ }
705
+ get shape() {
706
+ return this.def.shape;
707
+ }
708
+ get name() {
709
+ return this.def.name;
710
+ }
711
+ };
712
+ var KScalar = class _KScalar extends BaseSchema {
713
+ static create = (options) => new _KScalar(options);
714
+ constructor(def) {
715
+ super({
716
+ ...def,
717
+ example: def.schema.example(),
718
+ description: def.schema.description()
719
+ });
720
+ }
721
+ serialize(value) {
722
+ return this.def.toClient(value);
723
+ }
724
+ toOpenAPI() {
725
+ return {
726
+ ...this.def.schema.toOpenAPI(),
727
+ ...this.def.description ? { description: this.def.description } : {},
728
+ ...this.def.example ? { example: this.def.example } : {}
729
+ };
730
+ }
731
+ parseSafe(json) {
732
+ return ParseContext.result((ctx) => {
733
+ const jsonValue = this.def.schema.parseSafe(json);
734
+ if (!jsonValue.success) {
735
+ return ctx.addIssues(jsonValue.issues, []);
736
+ }
737
+ try {
738
+ return this.def.toServer(jsonValue.result);
739
+ } catch (error) {
740
+ return ctx.addIssue(error instanceof Error ? error.message : "Conversion failed", []);
741
+ }
742
+ });
743
+ }
744
+ parse(json) {
745
+ const result = this.parseSafe(json);
746
+ if (!result.success) {
747
+ throw new SchemaError(result.issues);
748
+ }
749
+ return result.result;
750
+ }
751
+ visit(visitor) {
752
+ const child = this.def.schema;
753
+ visitor(child);
754
+ child.visit(visitor);
755
+ }
756
+ };
757
+ var KUnion = class _KUnion extends BaseSchema {
758
+ static create = (items) => new _KUnion({ items });
759
+ static enum = (values) => k.union(values.map((v) => k.literal(v)));
760
+ serialize(value) {
761
+ for (const option of this.def.items) {
762
+ try {
763
+ return option.serialize(value);
764
+ } catch {
765
+ }
766
+ }
767
+ throw new Error("Value does not match any union option for serialization");
768
+ }
769
+ toOpenAPI() {
770
+ const baseSchema = this.getSchemaObject();
771
+ return {
772
+ ...baseSchema,
773
+ oneOf: this.def.items.map((option) => option.toOpenAPI())
774
+ };
775
+ }
776
+ parseSafe(json) {
777
+ let lastIssues;
778
+ for (const option of this.def.items) {
779
+ const result = option.parseSafe(json);
780
+ if (result.success) {
781
+ return { success: true, result: result.result };
782
+ } else {
783
+ lastIssues = result.issues;
784
+ }
785
+ }
786
+ return { success: false, issues: lastIssues ?? /* @__PURE__ */ new Set([{ message: "No union option matched", path: [] }]) };
787
+ }
788
+ parse(json) {
789
+ const result = this.parseSafe(json);
790
+ if (!result.success) {
791
+ throw new SchemaError(result.issues);
792
+ }
793
+ return result.result;
794
+ }
795
+ visit(visitor) {
796
+ for (const child of this.def.items) {
797
+ visitor(child);
798
+ child.visit(visitor);
799
+ }
800
+ }
801
+ };
802
+ var KLiteral = class _KLiteral extends BaseSchema {
803
+ parse(json) {
804
+ const result = this.parseSafe(json);
805
+ if (!result.success) {
806
+ throw new SchemaError(result.issues);
807
+ }
808
+ return result.result;
809
+ }
810
+ static create = (value) => new _KLiteral({ value });
811
+ serialize(value) {
812
+ return value;
813
+ }
814
+ toOpenAPI() {
815
+ const baseSchema = this.getSchemaObject();
816
+ const type = typeof this.def.value;
817
+ return {
818
+ ...baseSchema,
819
+ type,
820
+ enum: [this.def.value]
821
+ };
822
+ }
823
+ parseSafe(json) {
824
+ return ParseContext.result((ctx) => {
825
+ if (json !== this.def.value) {
826
+ return ctx.addIssue(`Expected ${this.def.value}`, []);
827
+ }
828
+ return this.def.value;
829
+ });
830
+ }
831
+ visit() {
832
+ }
833
+ };
834
+ var KNativeEnum = class _KNativeEnum extends BaseSchema {
835
+ static create = (enumObject) => new _KNativeEnum({ enum: enumObject });
836
+ serialize(value) {
837
+ return value;
838
+ }
839
+ toOpenAPI() {
840
+ const baseSchema = this.getSchemaObject();
841
+ const actualValues = Object.keys(this.def.enum).filter((key) => Number.isNaN(Number(key))).map((key) => this.def.enum[key]);
842
+ const uniqueValues = [...new Set(actualValues)];
843
+ const type = typeof uniqueValues[0] === "number" ? "number" : "string";
844
+ return {
845
+ ...baseSchema,
846
+ type,
847
+ enum: uniqueValues
848
+ };
849
+ }
850
+ parseSafe(json) {
851
+ return ParseContext.result((ctx) => {
852
+ const actualValues = Object.keys(this.def.enum).filter((key) => Number.isNaN(Number(key))).map((key) => this.def.enum[key]);
853
+ if (!actualValues.includes(json)) {
854
+ return ctx.addIssue(`Expected one of: ${actualValues.join(", ")}`, []);
855
+ }
856
+ return json;
857
+ });
858
+ }
859
+ parse(json) {
860
+ const result = this.parseSafe(json);
861
+ if (!result.success) {
862
+ throw new SchemaError(result.issues);
863
+ }
864
+ return result.result;
865
+ }
866
+ visit() {
867
+ }
868
+ };
869
+ var KRecord = class _KRecord extends BaseSchema {
870
+ static create = (keys, values) => new _KRecord({ keys, values });
871
+ serialize(value) {
872
+ const result = {};
873
+ for (const key in value) {
874
+ if (!Object.prototype.hasOwnProperty.call(value, key)) continue;
875
+ const keySerialize = this.def.keys.serialize(key);
876
+ const valueSerialize = this.def.values.serialize(value[key]);
877
+ result[keySerialize] = valueSerialize;
878
+ }
879
+ return result;
880
+ }
881
+ toOpenAPI() {
882
+ const baseSchema = this.getSchemaObject();
883
+ return {
884
+ ...baseSchema,
885
+ type: "object",
886
+ propertyNames: this.def.keys.toOpenAPI(),
887
+ additionalProperties: this.def.values.toOpenAPI()
888
+ };
889
+ }
890
+ parseSafe(json) {
891
+ return ParseContext.result((ctx) => {
892
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
893
+ return ctx.addIssue("Expected object", []);
894
+ }
895
+ const result = {};
896
+ for (const key in json) {
897
+ if (!Object.prototype.hasOwnProperty.call(json, key)) continue;
898
+ const keyParse = this.def.keys.parseSafe(key);
899
+ if (!keyParse.success) {
900
+ return ctx.addIssues(keyParse.issues, [key]);
901
+ }
902
+ const value = json[keyParse.result];
903
+ const valueParse = this.def.values.parseSafe(value);
904
+ if (!valueParse.success) {
905
+ return ctx.addIssues(valueParse.issues, [key]);
906
+ }
907
+ result[keyParse.result] = valueParse.result;
908
+ }
909
+ return result;
910
+ });
911
+ }
912
+ parse(json) {
913
+ const result = this.parseSafe(json);
914
+ if (!result.success) {
915
+ throw new SchemaError(result.issues);
916
+ }
917
+ return result.result;
918
+ }
919
+ visit(visitor) {
920
+ visitor(this.def.keys);
921
+ visitor(this.def.values);
922
+ }
923
+ };
924
+ var KLazy = class _KLazy extends BaseSchema {
925
+ schema;
926
+ static create = (getter) => new _KLazy({ getter });
927
+ getSchema() {
928
+ if (!this.schema) {
929
+ this.schema = this.def.getter();
930
+ }
931
+ return this.schema;
932
+ }
933
+ serialize(value) {
934
+ return this.getSchema().serialize(value);
935
+ }
936
+ toOpenAPI() {
937
+ return this.getSchema().toOpenAPI();
938
+ }
939
+ parseSafe(json) {
940
+ return this.getSchema().parseSafe(json);
941
+ }
942
+ parse(json) {
943
+ return this.getSchema().parse(json);
944
+ }
945
+ visit(visitor) {
946
+ const schema = this.getSchema();
947
+ visitor(schema);
948
+ schema.visit(visitor);
949
+ }
950
+ };
951
+ var k = {
952
+ string: KString.create,
953
+ number: KNumber.create,
954
+ boolean: KBoolean.create,
955
+ array: KArray.create,
956
+ null: KNull.create,
957
+ ref: KRef.create,
958
+ record: KRecord.create,
959
+ object: KObject.create,
960
+ scalar: KScalar.create,
961
+ literal: KLiteral.create,
962
+ enum: KUnion.enum,
963
+ nativeEnum: KNativeEnum.create,
964
+ union: KUnion.create,
965
+ lazy: KLazy.create,
966
+ /**
967
+ * Schema for any valid JSON value
968
+ */
969
+ json: () => {
970
+ const jsonSchema = k.lazy(
971
+ () => k.union([k.string(), k.number(), k.boolean(), k.null(), k.array(jsonSchema), k.record(k.string(), jsonSchema)])
972
+ );
973
+ return jsonSchema;
974
+ },
975
+ /**
976
+ * @internal
977
+ * @experimental
978
+ */
979
+ objectFromURLSearchParams: KObjectFromURLSearchParams.create
980
+ };
981
+
982
+ export {
983
+ isPrimitiveJSONValue,
984
+ SchemaError,
985
+ ParseContext,
986
+ BaseSchema,
987
+ STRING_FORMAT_REGEXES,
988
+ KString,
989
+ KNumber,
990
+ KBoolean,
991
+ KArray,
992
+ KNull,
993
+ KObject,
994
+ KObjectFromURLSearchParams,
995
+ KRef,
996
+ KScalar,
997
+ KUnion,
998
+ KLiteral,
999
+ KNativeEnum,
1000
+ KRecord,
1001
+ KLazy,
1002
+ k
1003
+ };