@kaito-http/core 3.0.5 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,728 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/schema/schema.ts
21
- var schema_exports = {};
22
- __export(schema_exports, {
23
- BaseSchema: () => BaseSchema,
24
- KArray: () => KArray,
25
- KBoolean: () => KBoolean,
26
- KNull: () => KNull,
27
- KNumber: () => KNumber,
28
- KObject: () => KObject,
29
- KRef: () => KRef,
30
- KScalar: () => KScalar,
31
- KString: () => KString,
32
- KUnion: () => KUnion,
33
- ParseContext: () => ParseContext,
34
- STRING_FORMAT_REGEXES: () => STRING_FORMAT_REGEXES,
35
- SchemaError: () => SchemaError,
36
- k: () => k
37
- });
38
- module.exports = __toCommonJS(schema_exports);
39
- var SchemaError = class extends Error {
40
- constructor(issues) {
41
- const first = issues.values().next().value;
42
- if (first === void 0) {
43
- throw new Error("SchemaError expects at least one issue to be provided");
44
- }
45
- super(first.message);
46
- this.issues = issues;
47
- }
48
- };
49
- var ParseContext = class _ParseContext {
50
- static ISSUE = Symbol("ISSUE");
51
- ISSUE = _ParseContext.ISSUE;
52
- #issues = /* @__PURE__ */ new Set();
53
- addIssue(message, path) {
54
- this.#issues.add({ message, path });
55
- return _ParseContext.ISSUE;
56
- }
57
- addIssues(issues, path) {
58
- for (const issue of issues) {
59
- this.#issues.add({ ...issue, path: [...path, ...issue.path] });
60
- }
61
- return _ParseContext.ISSUE;
62
- }
63
- get issues() {
64
- return this.#issues;
65
- }
66
- static result(fn) {
67
- const result = _ParseContext.with(fn);
68
- if (result.type === "FATAL") {
69
- return {
70
- success: false,
71
- issues: result.issues
72
- };
73
- }
74
- return {
75
- success: true,
76
- result: result.result
77
- };
78
- }
79
- static with(fn) {
80
- const ctx = new _ParseContext();
81
- const result = fn(ctx);
82
- if (result === _ParseContext.ISSUE) {
83
- return {
84
- type: "FATAL",
85
- issues: ctx.issues
86
- };
87
- }
88
- return {
89
- type: "PARSED",
90
- issues: ctx.issues,
91
- result
92
- };
93
- }
94
- };
95
- var BaseSchema = class {
96
- /** @internal */
97
- _input;
98
- /** @internal */
99
- _output;
100
- def;
101
- clone(def) {
102
- return new this.constructor({
103
- ...this.def,
104
- ...def
105
- });
106
- }
107
- constructor(def) {
108
- this.def = def;
109
- }
110
- example(example) {
111
- if (example === void 0) {
112
- return this.def.example;
113
- }
114
- return this.clone({ example });
115
- }
116
- description(description) {
117
- if (description === void 0) {
118
- return this.def.description;
119
- }
120
- return this.clone({ description });
121
- }
122
- };
123
- var STRING_FORMAT_REGEXES = {
124
- 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,
125
- email: /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i,
126
- 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])$/,
127
- 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]))$/,
128
- date: /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/,
129
- uri: /^(([^:/?#]+):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/,
130
- 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])$/
131
- };
132
- var KString = class _KString extends BaseSchema {
133
- static create = () => new _KString({});
134
- serialize(value) {
135
- return value;
136
- }
137
- setCheck(check) {
138
- return this.clone({ [check.type]: check });
139
- }
140
- toOpenAPI() {
141
- const schema = {
142
- type: "string"
143
- };
144
- if (this.def.regex) {
145
- schema.pattern = this.def.regex.regex.source;
146
- }
147
- if (this.def.format) {
148
- schema.format = this.def.format.format;
149
- }
150
- if (this.def.min !== void 0) {
151
- schema.minLength = this.def.min.val;
152
- }
153
- if (this.def.max !== void 0) {
154
- schema.maxLength = this.def.max.val;
155
- }
156
- if (this.def.description) {
157
- schema.description = this.def.description;
158
- }
159
- return schema;
160
- }
161
- /**
162
- * Sets the minimum length of the string
163
- *
164
- * @param min The minimum length of the string
165
- * @returns A clone of the schema with the minimum length set
166
- */
167
- max(min, message) {
168
- return this.setCheck({ type: "min", val: min, message });
169
- }
170
- /**
171
- * Sets the maximum length of the string
172
- *
173
- * @param max The maximum length of the string
174
- * @returns A clone of the schema with the maximum length set
175
- */
176
- min(max, message) {
177
- return this.setCheck({ type: "max", val: max, message });
178
- }
179
- regex(regex, message) {
180
- return this.setCheck({ type: "regex", regex, message });
181
- }
182
- format(format, message) {
183
- return this.setCheck({ type: "format", format, message });
184
- }
185
- uri(message) {
186
- return this.format("uri", message);
187
- }
188
- /**
189
- * Deprecated because OpenAPI uses the term "uri"
190
- * but this method exists for making migration from
191
- * Zod easier.
192
- *
193
- * @deprecated Use {@link uri} instead
194
- */
195
- url(message) {
196
- return this.uri(message);
197
- }
198
- email(message) {
199
- return this.format("email", message);
200
- }
201
- uuid(message) {
202
- return this.format("uuid", message);
203
- }
204
- ipv4(message) {
205
- return this.format("ipv4", message);
206
- }
207
- ipv6(message) {
208
- return this.format("ipv6", message);
209
- }
210
- date(message) {
211
- return this.format("date", message);
212
- }
213
- dateTime(message) {
214
- return this.format("date-time", message);
215
- }
216
- password(message) {
217
- return this.format("password", message);
218
- }
219
- byte(message) {
220
- return this.format("byte", message);
221
- }
222
- binary(message) {
223
- return this.format("binary", message);
224
- }
225
- hostname(message) {
226
- return this.format("hostname", message);
227
- }
228
- parseSafe(json) {
229
- return ParseContext.result((ctx) => {
230
- if (typeof json !== "string") {
231
- return ctx.addIssue("Expected string", []);
232
- }
233
- if (this.def.min !== void 0 && json.length < this.def.min.val) {
234
- ctx.addIssue(this.def.min.message ?? `String must be at least ${this.def.min.val} characters long`, []);
235
- }
236
- if (this.def.max !== void 0 && json.length > this.def.max.val) {
237
- ctx.addIssue(this.def.max.message ?? `String must be at most ${this.def.max.val} characters long`, []);
238
- }
239
- if (this.def.regex !== void 0 && !this.def.regex.regex.test(json)) {
240
- ctx.addIssue(this.def.regex.message ?? `String must match ${this.def.regex.regex.source}`, []);
241
- }
242
- if (this.def.format !== void 0) {
243
- switch (this.def.format.format) {
244
- case "uuid":
245
- if (!STRING_FORMAT_REGEXES.uuid.test(json)) {
246
- ctx.addIssue(this.def.format.message ?? "Invalid UUID format", []);
247
- }
248
- break;
249
- case "email":
250
- if (!STRING_FORMAT_REGEXES.email.test(json)) {
251
- ctx.addIssue(this.def.format.message ?? "Invalid email format", []);
252
- }
253
- break;
254
- case "ipv4":
255
- if (!STRING_FORMAT_REGEXES.ipv4.test(json)) {
256
- ctx.addIssue(this.def.format.message ?? "Invalid IPv4 address", []);
257
- }
258
- break;
259
- case "ipv6":
260
- if (!STRING_FORMAT_REGEXES.ipv6.test(json)) {
261
- ctx.addIssue(this.def.format.message ?? "Invalid IPv6 address", []);
262
- }
263
- break;
264
- case "date":
265
- if (!STRING_FORMAT_REGEXES.date.test(json)) {
266
- ctx.addIssue(this.def.format.message ?? "Invalid date format", []);
267
- }
268
- break;
269
- case "date-time":
270
- if (Number.isNaN(new Date(json).getTime())) {
271
- ctx.addIssue(this.def.format.message ?? "Invalid date-time format", []);
272
- }
273
- break;
274
- case "byte":
275
- if (!/^[A-Za-z0-9+/]*={0,2}$/.test(json) || json.length % 4 !== 0) {
276
- ctx.addIssue(this.def.format.message ?? "Invalid base64 format", []);
277
- }
278
- break;
279
- case "uri":
280
- if (!STRING_FORMAT_REGEXES.uri.test(json)) {
281
- ctx.addIssue(this.def.format.message ?? "Invalid URI format", []);
282
- }
283
- break;
284
- case "hostname":
285
- if (!STRING_FORMAT_REGEXES.hostname.test(json)) {
286
- ctx.addIssue(this.def.format.message ?? "Invalid hostname format", []);
287
- }
288
- break;
289
- case "binary":
290
- break;
291
- case "password":
292
- break;
293
- default:
294
- this.def.format.format;
295
- break;
296
- }
297
- }
298
- return json;
299
- });
300
- }
301
- parse(json) {
302
- const result = this.parseSafe(json);
303
- if (!result.success) {
304
- throw new SchemaError(result.issues);
305
- }
306
- return result.result;
307
- }
308
- };
309
- var KNumber = class _KNumber extends BaseSchema {
310
- static create = () => new _KNumber({});
311
- serialize(value) {
312
- return value;
313
- }
314
- setCheck(check) {
315
- return this.clone({ [check.type]: check });
316
- }
317
- toOpenAPI() {
318
- const schema = { type: "number" };
319
- if (this.def.min !== void 0) {
320
- schema.minimum = this.def.min.val;
321
- }
322
- if (this.def.max !== void 0) {
323
- schema.maximum = this.def.max.val;
324
- }
325
- if (this.def.multipleOf !== void 0) {
326
- schema.multipleOf = this.def.multipleOf.val;
327
- }
328
- if (this.def.integer) {
329
- schema.type = "integer";
330
- }
331
- if (this.def.format) {
332
- schema.format = this.def.format.format;
333
- }
334
- if (this.def.description) {
335
- schema.description = this.def.description;
336
- }
337
- return schema;
338
- }
339
- min(min) {
340
- return this.setCheck({ type: "min", val: min });
341
- }
342
- max(max) {
343
- return this.setCheck({ type: "max", val: max });
344
- }
345
- integer() {
346
- return this.setCheck({ type: "integer" });
347
- }
348
- multipleOf(multipleOf) {
349
- return this.setCheck({ type: "multipleOf", val: multipleOf });
350
- }
351
- float() {
352
- return this.setCheck({ type: "format", format: "float" });
353
- }
354
- double() {
355
- return this.setCheck({ type: "format", format: "double" });
356
- }
357
- int32() {
358
- return this.setCheck({ type: "format", format: "int32" });
359
- }
360
- int64() {
361
- return this.setCheck({ type: "format", format: "int64" });
362
- }
363
- parseSafe(json) {
364
- return ParseContext.result((ctx) => {
365
- if (typeof json !== "number") {
366
- return ctx.addIssue("Expected number", []);
367
- }
368
- if (this.def.integer && !Number.isInteger(json)) {
369
- return ctx.addIssue(this.def.integer.message ?? "Expected integer", []);
370
- }
371
- if (this.def.min !== void 0 && json < this.def.min.val) {
372
- return ctx.addIssue(this.def.min.message ?? `Number must be greater than ${this.def.min.val}`, []);
373
- }
374
- if (this.def.max !== void 0 && json > this.def.max.val) {
375
- return ctx.addIssue(this.def.max.message ?? `Number must be less than ${this.def.max.val}`, []);
376
- }
377
- if (this.def.multipleOf !== void 0 && json % this.def.multipleOf.val !== 0) {
378
- return ctx.addIssue(
379
- this.def.multipleOf.message ?? `Number must be a multiple of ${this.def.multipleOf.val}`,
380
- []
381
- );
382
- }
383
- return json;
384
- });
385
- }
386
- parse(json) {
387
- const result = this.parseSafe(json);
388
- if (!result.success) {
389
- throw new SchemaError(result.issues);
390
- }
391
- return result.result;
392
- }
393
- };
394
- var KBoolean = class _KBoolean extends BaseSchema {
395
- static create = () => new _KBoolean({});
396
- serialize(value) {
397
- return value;
398
- }
399
- toOpenAPI() {
400
- return {
401
- type: "boolean",
402
- ...this.def.description ? { description: this.def.description } : {}
403
- };
404
- }
405
- parseSafe(json) {
406
- return ParseContext.result((ctx) => {
407
- if (typeof json !== "boolean") {
408
- return ctx.addIssue("Expected boolean", []);
409
- }
410
- return json;
411
- });
412
- }
413
- parse(json) {
414
- const result = this.parseSafe(json);
415
- if (!result.success) {
416
- throw new SchemaError(result.issues);
417
- }
418
- return result.result;
419
- }
420
- };
421
- var KArray = class _KArray extends BaseSchema {
422
- static create = (items) => new _KArray({ items });
423
- serialize(value) {
424
- return value.map((item) => this.def.items.serialize(item));
425
- }
426
- setCheck(check) {
427
- return this.clone({ [check.type]: check });
428
- }
429
- toOpenAPI() {
430
- return {
431
- type: "array",
432
- items: this.def.items.toOpenAPI(),
433
- ...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
434
- ...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
435
- ...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
436
- };
437
- }
438
- min(minItems) {
439
- return this.setCheck({ type: "minItems", val: minItems });
440
- }
441
- max(maxItems) {
442
- return this.setCheck({ type: "maxItems", val: maxItems });
443
- }
444
- unique() {
445
- return this.setCheck({ type: "uniqueItems", val: true });
446
- }
447
- notUnique() {
448
- return this.setCheck({ type: "uniqueItems", val: false });
449
- }
450
- parseSafe(json) {
451
- return ParseContext.result((ctx) => {
452
- if (!Array.isArray(json)) {
453
- return ctx.addIssue("Expected array", []);
454
- }
455
- if (this.def.minItems !== void 0 && json.length < this.def.minItems.val) {
456
- return ctx.addIssue(this.def.minItems.message ?? `Array must have at least ${this.def.minItems.val} items`, []);
457
- }
458
- if (this.def.maxItems !== void 0 && json.length > this.def.maxItems.val) {
459
- return ctx.addIssue(this.def.maxItems.message ?? `Array must have at most ${this.def.maxItems.val} items`, []);
460
- }
461
- if (this.def.uniqueItems !== void 0 && new Set(json).size !== json.length) {
462
- return ctx.addIssue(this.def.uniqueItems.message ?? "Array must have unique items", []);
463
- }
464
- const items = [];
465
- for (let i = 0; i < json.length; i++) {
466
- const item = json[i];
467
- const result = this.def.items.parseSafe(item);
468
- if (!result.success) {
469
- return ctx.addIssues(result.issues, [i.toString()]);
470
- }
471
- items.push(result.result);
472
- }
473
- return items;
474
- });
475
- }
476
- parse(json) {
477
- const result = this.parseSafe(json);
478
- if (!result.success) {
479
- throw new SchemaError(result.issues);
480
- }
481
- return result.result;
482
- }
483
- };
484
- var KNull = class _KNull extends BaseSchema {
485
- static create = () => new _KNull({});
486
- serialize(value) {
487
- return value;
488
- }
489
- toOpenAPI() {
490
- return {
491
- type: "null",
492
- ...this.def.description ? { description: this.def.description } : {}
493
- };
494
- }
495
- parseSafe(json) {
496
- return ParseContext.result((ctx) => {
497
- if (json !== null) {
498
- return ctx.addIssue("Expected null", []);
499
- }
500
- return null;
501
- });
502
- }
503
- parse(json) {
504
- const result = this.parseSafe(json);
505
- if (!result.success) {
506
- throw new SchemaError(result.issues);
507
- }
508
- return result.result;
509
- }
510
- };
511
- var KObject = class _KObject extends BaseSchema {
512
- static create = (shape) => new _KObject({ shape });
513
- serialize(value) {
514
- const result = {};
515
- for (const key in this.def.shape) {
516
- if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
517
- const fieldValue = value[key];
518
- if (fieldValue === void 0) {
519
- throw new Error(`Missing required property: ${key}`);
520
- }
521
- result[key] = this.def.shape[key].serialize(fieldValue);
522
- }
523
- }
524
- return result;
525
- }
526
- toOpenAPI() {
527
- return {
528
- type: "object",
529
- properties: Object.fromEntries(
530
- Object.entries(this.def.shape).map((entry) => {
531
- const [key, value] = entry;
532
- return [key, value.toOpenAPI()];
533
- })
534
- ),
535
- required: Object.keys(this.def.shape),
536
- ...this.def.description ? { description: this.def.description } : {}
537
- };
538
- }
539
- parseSafe(json) {
540
- return ParseContext.result((ctx) => {
541
- if (typeof json !== "object" || json === null || Array.isArray(json)) {
542
- return ctx.addIssue(`Expected object, got ${typeof json}`, []);
543
- }
544
- const result = {};
545
- for (const key in this.def.shape) {
546
- if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
547
- const value = json[key];
548
- if (value === void 0) {
549
- return ctx.addIssue(`Missing required property: ${key}`, [key]);
550
- }
551
- const parseResult = this.def.shape[key].parseSafe(value);
552
- if (!parseResult.success) {
553
- return ctx.addIssues(parseResult.issues, [key]);
554
- }
555
- result[key] = parseResult.result;
556
- }
557
- }
558
- return result;
559
- });
560
- }
561
- parse(json) {
562
- const result = this.parseSafe(json);
563
- if (!result.success) {
564
- throw new SchemaError(result.issues);
565
- }
566
- return result.result;
567
- }
568
- get shape() {
569
- return this.def.shape;
570
- }
571
- };
572
- var KRef = class _KRef extends BaseSchema {
573
- static create = (name, shape) => new _KRef({ name, shape });
574
- serialize(value) {
575
- const result = {};
576
- for (const key in this.def.shape) {
577
- if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
578
- const fieldValue = value[key];
579
- if (fieldValue === void 0) {
580
- throw new Error(`Missing required property: ${key}`);
581
- }
582
- result[key] = this.def.shape[key].serialize(fieldValue);
583
- }
584
- }
585
- return result;
586
- }
587
- toOpenAPI() {
588
- return {
589
- $ref: `#/components/schemas/${this.def.name}`,
590
- ...this.def.description ? { description: this.def.description } : {},
591
- ...this.def.summary ? { summary: this.def.summary } : {}
592
- };
593
- }
594
- parseSafe(json) {
595
- return ParseContext.result((ctx) => {
596
- if (typeof json !== "object" || json === null || Array.isArray(json)) {
597
- return ctx.addIssue(`Expected object, got ${typeof json}`, []);
598
- }
599
- const result = {};
600
- for (const key in this.def.shape) {
601
- if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
602
- const value = json[key];
603
- if (value === void 0) {
604
- return ctx.addIssue(`Missing required property: ${key}`, [key]);
605
- }
606
- const parseResult = this.def.shape[key].parseSafe(value);
607
- if (!parseResult.success) {
608
- return ctx.addIssues(parseResult.issues, [key]);
609
- }
610
- result[key] = parseResult.result;
611
- }
612
- }
613
- return result;
614
- });
615
- }
616
- parse(json) {
617
- const result = this.parseSafe(json);
618
- if (!result.success) {
619
- throw new SchemaError(result.issues);
620
- }
621
- return result.result;
622
- }
623
- summary(summary) {
624
- if (summary === void 0) {
625
- return this.def.summary;
626
- }
627
- return this.clone({ summary });
628
- }
629
- get shape() {
630
- return this.def.shape;
631
- }
632
- get name() {
633
- return this.def.name;
634
- }
635
- };
636
- var KScalar = class _KScalar extends BaseSchema {
637
- static create = (options) => new _KScalar(options);
638
- constructor(def) {
639
- super(def);
640
- }
641
- serialize(value) {
642
- return this.def.toClient(value);
643
- }
644
- toOpenAPI() {
645
- return this.def.schema.toOpenAPI();
646
- }
647
- parseSafe(json) {
648
- return ParseContext.result((ctx) => {
649
- const jsonValue = this.def.schema.parseSafe(json);
650
- if (!jsonValue.success) {
651
- return ctx.addIssues(jsonValue.issues, []);
652
- }
653
- return this.def.toServer(jsonValue.result);
654
- });
655
- }
656
- parse(json) {
657
- const result = this.parseSafe(json);
658
- if (!result.success) {
659
- throw new SchemaError(result.issues);
660
- }
661
- return result.result;
662
- }
663
- };
664
- var KUnion = class _KUnion extends BaseSchema {
665
- static create = (items) => new _KUnion({ items });
666
- serialize(value) {
667
- for (const option of this.def.items) {
668
- try {
669
- return option.serialize(value);
670
- } catch {
671
- }
672
- }
673
- throw new Error("Value does not match any union option for serialization");
674
- }
675
- toOpenAPI() {
676
- return {
677
- oneOf: this.def.items.map((option) => option.toOpenAPI()),
678
- ...this.def.description ? { description: this.def.description } : {}
679
- };
680
- }
681
- parseSafe(json) {
682
- let lastIssues;
683
- for (const option of this.def.items) {
684
- const result = option.parseSafe(json);
685
- if (result.success) {
686
- return { success: true, result: result.result };
687
- } else {
688
- lastIssues = result.issues;
689
- }
690
- }
691
- return { success: false, issues: lastIssues ?? /* @__PURE__ */ new Set([{ message: "No union option matched", path: [] }]) };
692
- }
693
- parse(json) {
694
- const result = this.parseSafe(json);
695
- if (!result.success) {
696
- throw new SchemaError(result.issues);
697
- }
698
- return result.result;
699
- }
700
- };
701
- var k = {
702
- string: KString.create,
703
- number: KNumber.create,
704
- boolean: KBoolean.create,
705
- array: KArray.create,
706
- null: KNull.create,
707
- ref: KRef.create,
708
- object: KObject.create,
709
- scalar: KScalar.create,
710
- union: KUnion.create
711
- };
712
- // Annotate the CommonJS export names for ESM import in node:
713
- 0 && (module.exports = {
714
- BaseSchema,
715
- KArray,
716
- KBoolean,
717
- KNull,
718
- KNumber,
719
- KObject,
720
- KRef,
721
- KScalar,
722
- KString,
723
- KUnion,
724
- ParseContext,
725
- STRING_FORMAT_REGEXES,
726
- SchemaError,
727
- k
728
- });