@vertz/schema 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +782 -0
  2. package/dist/index.js +2438 -0
  3. package/package.json +30 -0
package/dist/index.js ADDED
@@ -0,0 +1,2438 @@
1
+ // src/core/errors.ts
2
+ var ErrorCode;
3
+ ((ErrorCode2) => {
4
+ ErrorCode2["InvalidType"] = "invalid_type";
5
+ ErrorCode2["TooSmall"] = "too_small";
6
+ ErrorCode2["TooBig"] = "too_big";
7
+ ErrorCode2["InvalidString"] = "invalid_string";
8
+ ErrorCode2["InvalidEnumValue"] = "invalid_enum_value";
9
+ ErrorCode2["InvalidLiteral"] = "invalid_literal";
10
+ ErrorCode2["InvalidUnion"] = "invalid_union";
11
+ ErrorCode2["InvalidDate"] = "invalid_date";
12
+ ErrorCode2["MissingProperty"] = "missing_property";
13
+ ErrorCode2["UnrecognizedKeys"] = "unrecognized_keys";
14
+ ErrorCode2["Custom"] = "custom";
15
+ ErrorCode2["InvalidIntersection"] = "invalid_intersection";
16
+ ErrorCode2["NotMultipleOf"] = "not_multiple_of";
17
+ ErrorCode2["NotFinite"] = "not_finite";
18
+ })(ErrorCode ||= {});
19
+
20
+ class ParseError extends Error {
21
+ issues;
22
+ constructor(issues) {
23
+ const message = ParseError.formatMessage(issues);
24
+ super(message);
25
+ this.name = "ParseError";
26
+ this.issues = issues;
27
+ }
28
+ static formatMessage(issues) {
29
+ return issues.map((issue) => {
30
+ const path = issue.path.length > 0 ? ` at "${issue.path.join(".")}"` : "";
31
+ return `${issue.message}${path}`;
32
+ }).join("; ");
33
+ }
34
+ }
35
+ // src/core/parse-context.ts
36
+ class ParseContext {
37
+ issues = [];
38
+ _path = [];
39
+ addIssue(issue) {
40
+ this.issues.push({
41
+ ...issue,
42
+ path: issue.path ?? [...this._path]
43
+ });
44
+ }
45
+ hasIssues() {
46
+ return this.issues.length > 0;
47
+ }
48
+ pushPath(segment) {
49
+ this._path.push(segment);
50
+ }
51
+ popPath() {
52
+ this._path.pop();
53
+ }
54
+ get path() {
55
+ return [...this._path];
56
+ }
57
+ }
58
+ // src/core/registry.ts
59
+ class SchemaRegistry {
60
+ static _schemas = new Map;
61
+ static register(name, schema) {
62
+ SchemaRegistry._schemas.set(name, schema);
63
+ }
64
+ static get(name) {
65
+ return SchemaRegistry._schemas.get(name);
66
+ }
67
+ static getOrThrow(name) {
68
+ const schema = SchemaRegistry._schemas.get(name);
69
+ if (!schema) {
70
+ throw new Error(`Schema "${name}" not found in registry`);
71
+ }
72
+ return schema;
73
+ }
74
+ static has(name) {
75
+ return SchemaRegistry._schemas.has(name);
76
+ }
77
+ static getAll() {
78
+ return SchemaRegistry._schemas;
79
+ }
80
+ static clear() {
81
+ SchemaRegistry._schemas.clear();
82
+ }
83
+ }
84
+ // src/introspection/json-schema.ts
85
+ class RefTracker {
86
+ _seen = new Set;
87
+ _defs = {};
88
+ hasSeen(id) {
89
+ return this._seen.has(id);
90
+ }
91
+ markSeen(id) {
92
+ this._seen.add(id);
93
+ }
94
+ addDef(id, schema) {
95
+ this._defs[id] = schema;
96
+ }
97
+ getDefs() {
98
+ return { ...this._defs };
99
+ }
100
+ }
101
+ function toJSONSchema(schema) {
102
+ return schema.toJSONSchema();
103
+ }
104
+
105
+ // src/core/schema.ts
106
+ class Schema {
107
+ _id;
108
+ _description;
109
+ _meta;
110
+ _examples;
111
+ constructor() {
112
+ this._examples = [];
113
+ }
114
+ parse(value) {
115
+ const ctx = new ParseContext;
116
+ const result = this._runPipeline(value, ctx);
117
+ if (ctx.hasIssues()) {
118
+ throw new ParseError(ctx.issues);
119
+ }
120
+ return result;
121
+ }
122
+ safeParse(value) {
123
+ const ctx = new ParseContext;
124
+ try {
125
+ const data = this._runPipeline(value, ctx);
126
+ if (ctx.hasIssues()) {
127
+ return { success: false, error: new ParseError(ctx.issues) };
128
+ }
129
+ return { success: true, data };
130
+ } catch (e) {
131
+ if (e instanceof ParseError) {
132
+ return { success: false, error: e };
133
+ }
134
+ throw e;
135
+ }
136
+ }
137
+ id(name) {
138
+ const clone = this._clone();
139
+ clone._id = name;
140
+ SchemaRegistry.register(name, clone);
141
+ return clone;
142
+ }
143
+ describe(description) {
144
+ const clone = this._clone();
145
+ clone._description = description;
146
+ return clone;
147
+ }
148
+ meta(data) {
149
+ const clone = this._clone();
150
+ clone._meta = { ...this._meta ?? {}, ...data };
151
+ return clone;
152
+ }
153
+ example(value) {
154
+ const clone = this._clone();
155
+ clone._examples = [...this._examples, value];
156
+ return clone;
157
+ }
158
+ get metadata() {
159
+ return {
160
+ type: this._schemaType(),
161
+ id: this._id,
162
+ description: this._description,
163
+ meta: this._meta,
164
+ examples: this._examples
165
+ };
166
+ }
167
+ toJSONSchema() {
168
+ const tracker = new RefTracker;
169
+ const schema = this._toJSONSchemaWithRefs(tracker);
170
+ const defs = tracker.getDefs();
171
+ if (Object.keys(defs).length > 0) {
172
+ return { $defs: defs, ...schema };
173
+ }
174
+ return schema;
175
+ }
176
+ _toJSONSchemaWithRefs(tracker) {
177
+ if (this._id && tracker.hasSeen(this._id)) {
178
+ return { $ref: `#/$defs/${this._id}` };
179
+ }
180
+ if (this._id) {
181
+ tracker.markSeen(this._id);
182
+ const jsonSchema = this._applyMetadata(this._toJSONSchema(tracker));
183
+ tracker.addDef(this._id, jsonSchema);
184
+ return { $ref: `#/$defs/${this._id}` };
185
+ }
186
+ return this._applyMetadata(this._toJSONSchema(tracker));
187
+ }
188
+ _applyMetadata(schema) {
189
+ if (this._description)
190
+ schema.description = this._description;
191
+ if (this._examples.length > 0)
192
+ schema.examples = this._examples;
193
+ return schema;
194
+ }
195
+ _cloneBase(target) {
196
+ target._id = this._id;
197
+ target._description = this._description;
198
+ target._meta = this._meta ? { ...this._meta } : undefined;
199
+ target._examples = [...this._examples];
200
+ return target;
201
+ }
202
+ optional() {
203
+ return new OptionalSchema(this);
204
+ }
205
+ nullable() {
206
+ return new NullableSchema(this);
207
+ }
208
+ default(defaultValue) {
209
+ return new DefaultSchema(this, defaultValue);
210
+ }
211
+ refine(predicate, params) {
212
+ return new RefinedSchema(this, predicate, params);
213
+ }
214
+ superRefine(refinement) {
215
+ return new SuperRefinedSchema(this, refinement);
216
+ }
217
+ check(refinement) {
218
+ return new SuperRefinedSchema(this, refinement);
219
+ }
220
+ transform(fn) {
221
+ return new TransformSchema(this, fn);
222
+ }
223
+ pipe(schema) {
224
+ return new PipeSchema(this, schema);
225
+ }
226
+ catch(fallback) {
227
+ return new CatchSchema(this, fallback);
228
+ }
229
+ brand() {
230
+ return new BrandedSchema(this);
231
+ }
232
+ readonly() {
233
+ return new ReadonlySchema(this);
234
+ }
235
+ _runPipeline(value, ctx) {
236
+ return this._parse(value, ctx);
237
+ }
238
+ }
239
+
240
+ class OptionalSchema extends Schema {
241
+ _inner;
242
+ constructor(_inner) {
243
+ super();
244
+ this._inner = _inner;
245
+ }
246
+ _schemaType() {
247
+ return this._inner._schemaType();
248
+ }
249
+ _parse(value, ctx) {
250
+ if (value === undefined)
251
+ return;
252
+ return this._inner._runPipeline(value, ctx);
253
+ }
254
+ _toJSONSchema(tracker) {
255
+ return this._inner._toJSONSchemaWithRefs(tracker);
256
+ }
257
+ _clone() {
258
+ return this._cloneBase(new OptionalSchema(this._inner));
259
+ }
260
+ unwrap() {
261
+ return this._inner;
262
+ }
263
+ }
264
+
265
+ class NullableSchema extends Schema {
266
+ _inner;
267
+ constructor(_inner) {
268
+ super();
269
+ this._inner = _inner;
270
+ }
271
+ _schemaType() {
272
+ return this._inner._schemaType();
273
+ }
274
+ _parse(value, ctx) {
275
+ if (value === null)
276
+ return null;
277
+ return this._inner._runPipeline(value, ctx);
278
+ }
279
+ _toJSONSchema(tracker) {
280
+ const inner = this._inner._toJSONSchemaWithRefs(tracker);
281
+ if (typeof inner.type === "string") {
282
+ return { ...inner, type: [inner.type, "null"] };
283
+ }
284
+ return { anyOf: [inner, { type: "null" }] };
285
+ }
286
+ _clone() {
287
+ return this._cloneBase(new NullableSchema(this._inner));
288
+ }
289
+ unwrap() {
290
+ return this._inner;
291
+ }
292
+ }
293
+
294
+ class DefaultSchema extends Schema {
295
+ _inner;
296
+ _default;
297
+ constructor(_inner, _default) {
298
+ super();
299
+ this._inner = _inner;
300
+ this._default = _default;
301
+ }
302
+ _schemaType() {
303
+ return this._inner._schemaType();
304
+ }
305
+ _parse(value, ctx) {
306
+ if (value === undefined) {
307
+ return this._inner._runPipeline(this._resolveDefault(), ctx);
308
+ }
309
+ return this._inner._runPipeline(value, ctx);
310
+ }
311
+ _toJSONSchema(tracker) {
312
+ const inner = this._inner._toJSONSchemaWithRefs(tracker);
313
+ return { ...inner, default: this._resolveDefault() };
314
+ }
315
+ _resolveDefault() {
316
+ return typeof this._default === "function" ? this._default() : this._default;
317
+ }
318
+ _clone() {
319
+ return this._cloneBase(new DefaultSchema(this._inner, this._default));
320
+ }
321
+ unwrap() {
322
+ return this._inner;
323
+ }
324
+ }
325
+
326
+ class RefinedSchema extends Schema {
327
+ _inner;
328
+ _predicate;
329
+ _message;
330
+ _path;
331
+ constructor(inner, predicate, params) {
332
+ super();
333
+ this._inner = inner;
334
+ this._predicate = predicate;
335
+ const normalized = typeof params === "string" ? { message: params } : params;
336
+ this._message = normalized?.message ?? "Custom validation failed";
337
+ this._path = normalized?.path;
338
+ }
339
+ _parse(value, ctx) {
340
+ const result = this._inner._runPipeline(value, ctx);
341
+ if (ctx.hasIssues())
342
+ return result;
343
+ if (!this._predicate(result)) {
344
+ ctx.addIssue({
345
+ code: "custom" /* Custom */,
346
+ message: this._message,
347
+ path: this._path
348
+ });
349
+ }
350
+ return result;
351
+ }
352
+ _schemaType() {
353
+ return this._inner._schemaType();
354
+ }
355
+ _toJSONSchema(tracker) {
356
+ return this._inner._toJSONSchemaWithRefs(tracker);
357
+ }
358
+ _clone() {
359
+ return this._cloneBase(new RefinedSchema(this._inner, this._predicate, {
360
+ message: this._message,
361
+ path: this._path
362
+ }));
363
+ }
364
+ }
365
+
366
+ class SuperRefinedSchema extends Schema {
367
+ _inner;
368
+ _refinement;
369
+ constructor(inner, refinement) {
370
+ super();
371
+ this._inner = inner;
372
+ this._refinement = refinement;
373
+ }
374
+ _parse(value, ctx) {
375
+ const result = this._inner._runPipeline(value, ctx);
376
+ if (ctx.hasIssues())
377
+ return result;
378
+ this._refinement(result, ctx);
379
+ return result;
380
+ }
381
+ _schemaType() {
382
+ return this._inner._schemaType();
383
+ }
384
+ _toJSONSchema(tracker) {
385
+ return this._inner._toJSONSchemaWithRefs(tracker);
386
+ }
387
+ _clone() {
388
+ return this._cloneBase(new SuperRefinedSchema(this._inner, this._refinement));
389
+ }
390
+ }
391
+
392
+ class TransformSchema extends Schema {
393
+ _inner;
394
+ _transform;
395
+ constructor(inner, transform) {
396
+ super();
397
+ this._inner = inner;
398
+ this._transform = transform;
399
+ }
400
+ _parse(value, ctx) {
401
+ const result = this._inner._runPipeline(value, ctx);
402
+ if (ctx.hasIssues())
403
+ return result;
404
+ try {
405
+ return this._transform(result);
406
+ } catch (e) {
407
+ ctx.addIssue({
408
+ code: "custom" /* Custom */,
409
+ message: e instanceof Error ? e.message : "Transform failed"
410
+ });
411
+ return result;
412
+ }
413
+ }
414
+ _schemaType() {
415
+ return this._inner._schemaType();
416
+ }
417
+ _toJSONSchema(tracker) {
418
+ return this._inner._toJSONSchemaWithRefs(tracker);
419
+ }
420
+ _clone() {
421
+ return this._cloneBase(new TransformSchema(this._inner, this._transform));
422
+ }
423
+ }
424
+
425
+ class PipeSchema extends Schema {
426
+ _first;
427
+ _second;
428
+ constructor(first, second) {
429
+ super();
430
+ this._first = first;
431
+ this._second = second;
432
+ }
433
+ _parse(value, ctx) {
434
+ const intermediate = this._first._runPipeline(value, ctx);
435
+ if (ctx.hasIssues())
436
+ return intermediate;
437
+ return this._second._runPipeline(intermediate, ctx);
438
+ }
439
+ _schemaType() {
440
+ return this._second._schemaType();
441
+ }
442
+ _toJSONSchema(tracker) {
443
+ return this._first._toJSONSchemaWithRefs(tracker);
444
+ }
445
+ _clone() {
446
+ return this._cloneBase(new PipeSchema(this._first, this._second));
447
+ }
448
+ }
449
+
450
+ class CatchSchema extends Schema {
451
+ _inner;
452
+ _fallback;
453
+ constructor(inner, fallback) {
454
+ super();
455
+ this._inner = inner;
456
+ this._fallback = fallback;
457
+ }
458
+ _parse(value, _ctx) {
459
+ const innerCtx = new ParseContext;
460
+ const result = this._inner._runPipeline(value, innerCtx);
461
+ if (innerCtx.hasIssues()) {
462
+ return this._resolveFallback();
463
+ }
464
+ return result;
465
+ }
466
+ _schemaType() {
467
+ return this._inner._schemaType();
468
+ }
469
+ _toJSONSchema(tracker) {
470
+ return this._inner._toJSONSchemaWithRefs(tracker);
471
+ }
472
+ _resolveFallback() {
473
+ return typeof this._fallback === "function" ? this._fallback() : this._fallback;
474
+ }
475
+ _clone() {
476
+ return this._cloneBase(new CatchSchema(this._inner, this._fallback));
477
+ }
478
+ }
479
+
480
+ class BrandedSchema extends Schema {
481
+ _inner;
482
+ constructor(inner) {
483
+ super();
484
+ this._inner = inner;
485
+ }
486
+ _parse(value, ctx) {
487
+ return this._inner._runPipeline(value, ctx);
488
+ }
489
+ _schemaType() {
490
+ return this._inner._schemaType();
491
+ }
492
+ _toJSONSchema(tracker) {
493
+ return this._inner._toJSONSchemaWithRefs(tracker);
494
+ }
495
+ _clone() {
496
+ return this._cloneBase(new BrandedSchema(this._inner));
497
+ }
498
+ }
499
+
500
+ class ReadonlySchema extends Schema {
501
+ _inner;
502
+ constructor(inner) {
503
+ super();
504
+ this._inner = inner;
505
+ }
506
+ _parse(value, ctx) {
507
+ const result = this._inner._runPipeline(value, ctx);
508
+ if (ctx.hasIssues())
509
+ return result;
510
+ if (typeof result === "object" && result !== null) {
511
+ return Object.freeze(result);
512
+ }
513
+ return result;
514
+ }
515
+ _schemaType() {
516
+ return this._inner._schemaType();
517
+ }
518
+ _toJSONSchema(tracker) {
519
+ return this._inner._toJSONSchemaWithRefs(tracker);
520
+ }
521
+ _clone() {
522
+ return this._cloneBase(new ReadonlySchema(this._inner));
523
+ }
524
+ }
525
+ // src/core/types.ts
526
+ var SchemaType;
527
+ ((SchemaType2) => {
528
+ SchemaType2["String"] = "string";
529
+ SchemaType2["Number"] = "number";
530
+ SchemaType2["BigInt"] = "bigint";
531
+ SchemaType2["Boolean"] = "boolean";
532
+ SchemaType2["Date"] = "date";
533
+ SchemaType2["Symbol"] = "symbol";
534
+ SchemaType2["Undefined"] = "undefined";
535
+ SchemaType2["Null"] = "null";
536
+ SchemaType2["Void"] = "void";
537
+ SchemaType2["Any"] = "any";
538
+ SchemaType2["Unknown"] = "unknown";
539
+ SchemaType2["Never"] = "never";
540
+ SchemaType2["NaN"] = "nan";
541
+ SchemaType2["Object"] = "object";
542
+ SchemaType2["Array"] = "array";
543
+ SchemaType2["Tuple"] = "tuple";
544
+ SchemaType2["Enum"] = "enum";
545
+ SchemaType2["Union"] = "union";
546
+ SchemaType2["DiscriminatedUnion"] = "discriminatedUnion";
547
+ SchemaType2["Intersection"] = "intersection";
548
+ SchemaType2["Record"] = "record";
549
+ SchemaType2["Map"] = "map";
550
+ SchemaType2["Set"] = "set";
551
+ SchemaType2["Literal"] = "literal";
552
+ SchemaType2["Lazy"] = "lazy";
553
+ SchemaType2["Custom"] = "custom";
554
+ SchemaType2["InstanceOf"] = "instanceof";
555
+ SchemaType2["File"] = "file";
556
+ })(SchemaType ||= {});
557
+ // src/schemas/array.ts
558
+ class ArraySchema extends Schema {
559
+ _element;
560
+ _min;
561
+ _max;
562
+ _length;
563
+ constructor(element) {
564
+ super();
565
+ this._element = element;
566
+ }
567
+ _parse(value, ctx) {
568
+ if (!Array.isArray(value)) {
569
+ ctx.addIssue({
570
+ code: "invalid_type" /* InvalidType */,
571
+ message: `Expected array, received ${typeof value}`
572
+ });
573
+ return value;
574
+ }
575
+ if (this._min !== undefined && value.length < this._min) {
576
+ ctx.addIssue({
577
+ code: "too_small" /* TooSmall */,
578
+ message: `Array must contain at least ${this._min} element(s)`
579
+ });
580
+ }
581
+ if (this._max !== undefined && value.length > this._max) {
582
+ ctx.addIssue({
583
+ code: "too_big" /* TooBig */,
584
+ message: `Array must contain at most ${this._max} element(s)`
585
+ });
586
+ }
587
+ if (this._length !== undefined && value.length !== this._length) {
588
+ ctx.addIssue({
589
+ code: "invalid_type" /* InvalidType */,
590
+ message: `Array must contain exactly ${this._length} element(s)`
591
+ });
592
+ }
593
+ const result = [];
594
+ for (let i = 0;i < value.length; i++) {
595
+ ctx.pushPath(i);
596
+ result.push(this._element._runPipeline(value[i], ctx));
597
+ ctx.popPath();
598
+ }
599
+ return result;
600
+ }
601
+ min(n) {
602
+ const clone = this._clone();
603
+ clone._min = n;
604
+ return clone;
605
+ }
606
+ max(n) {
607
+ const clone = this._clone();
608
+ clone._max = n;
609
+ return clone;
610
+ }
611
+ length(n) {
612
+ const clone = this._clone();
613
+ clone._length = n;
614
+ return clone;
615
+ }
616
+ _schemaType() {
617
+ return "array" /* Array */;
618
+ }
619
+ _toJSONSchema(tracker) {
620
+ const schema = {
621
+ type: "array",
622
+ items: this._element._toJSONSchemaWithRefs(tracker)
623
+ };
624
+ if (this._min !== undefined)
625
+ schema.minItems = this._min;
626
+ if (this._max !== undefined)
627
+ schema.maxItems = this._max;
628
+ if (this._length !== undefined) {
629
+ schema.minItems = this._length;
630
+ schema.maxItems = this._length;
631
+ }
632
+ return schema;
633
+ }
634
+ _clone() {
635
+ const clone = this._cloneBase(new ArraySchema(this._element));
636
+ clone._min = this._min;
637
+ clone._max = this._max;
638
+ clone._length = this._length;
639
+ return clone;
640
+ }
641
+ }
642
+ // src/schemas/bigint.ts
643
+ class BigIntSchema extends Schema {
644
+ _parse(value, ctx) {
645
+ if (typeof value !== "bigint") {
646
+ ctx.addIssue({
647
+ code: "invalid_type" /* InvalidType */,
648
+ message: `Expected bigint, received ${typeof value}`
649
+ });
650
+ return value;
651
+ }
652
+ return value;
653
+ }
654
+ _schemaType() {
655
+ return "bigint" /* BigInt */;
656
+ }
657
+ _toJSONSchema(_tracker) {
658
+ return { type: "integer", format: "int64" };
659
+ }
660
+ _clone() {
661
+ return this._cloneBase(new BigIntSchema);
662
+ }
663
+ }
664
+ // src/schemas/boolean.ts
665
+ class BooleanSchema extends Schema {
666
+ _parse(value, ctx) {
667
+ if (typeof value !== "boolean") {
668
+ ctx.addIssue({
669
+ code: "invalid_type" /* InvalidType */,
670
+ message: `Expected boolean, received ${typeof value}`
671
+ });
672
+ return value;
673
+ }
674
+ return value;
675
+ }
676
+ _schemaType() {
677
+ return "boolean" /* Boolean */;
678
+ }
679
+ _toJSONSchema(_tracker) {
680
+ return { type: "boolean" };
681
+ }
682
+ _clone() {
683
+ return this._cloneBase(new BooleanSchema);
684
+ }
685
+ }
686
+ // src/schemas/date.ts
687
+ class DateSchema extends Schema {
688
+ _min;
689
+ _minMessage;
690
+ _max;
691
+ _maxMessage;
692
+ _parse(value, ctx) {
693
+ if (!(value instanceof Date)) {
694
+ ctx.addIssue({
695
+ code: "invalid_type" /* InvalidType */,
696
+ message: `Expected Date, received ${typeof value}`
697
+ });
698
+ return value;
699
+ }
700
+ if (Number.isNaN(value.getTime())) {
701
+ ctx.addIssue({ code: "invalid_date" /* InvalidDate */, message: "Invalid date" });
702
+ return value;
703
+ }
704
+ if (this._min !== undefined && value.getTime() < this._min.getTime()) {
705
+ ctx.addIssue({
706
+ code: "too_small" /* TooSmall */,
707
+ message: this._minMessage ?? `Date must be after ${this._min.toISOString()}`
708
+ });
709
+ }
710
+ if (this._max !== undefined && value.getTime() > this._max.getTime()) {
711
+ ctx.addIssue({
712
+ code: "too_big" /* TooBig */,
713
+ message: this._maxMessage ?? `Date must be before ${this._max.toISOString()}`
714
+ });
715
+ }
716
+ return value;
717
+ }
718
+ min(date, message) {
719
+ const clone = this._clone();
720
+ clone._min = date;
721
+ clone._minMessage = message;
722
+ return clone;
723
+ }
724
+ max(date, message) {
725
+ const clone = this._clone();
726
+ clone._max = date;
727
+ clone._maxMessage = message;
728
+ return clone;
729
+ }
730
+ _schemaType() {
731
+ return "date" /* Date */;
732
+ }
733
+ _toJSONSchema(_tracker) {
734
+ return { type: "string", format: "date-time" };
735
+ }
736
+ _clone() {
737
+ const clone = this._cloneBase(new DateSchema);
738
+ clone._min = this._min;
739
+ clone._minMessage = this._minMessage;
740
+ clone._max = this._max;
741
+ clone._maxMessage = this._maxMessage;
742
+ return clone;
743
+ }
744
+ }
745
+
746
+ // src/schemas/number.ts
747
+ class NumberSchema extends Schema {
748
+ _gte;
749
+ _gteMessage;
750
+ _gt;
751
+ _gtMessage;
752
+ _lte;
753
+ _lteMessage;
754
+ _lt;
755
+ _ltMessage;
756
+ _int = false;
757
+ _positive = false;
758
+ _negative = false;
759
+ _nonnegative = false;
760
+ _nonpositive = false;
761
+ _multipleOf;
762
+ _finite = false;
763
+ _parse(value, ctx) {
764
+ if (typeof value !== "number" || Number.isNaN(value)) {
765
+ ctx.addIssue({
766
+ code: "invalid_type" /* InvalidType */,
767
+ message: `Expected number, received ${typeof value}`
768
+ });
769
+ return value;
770
+ }
771
+ if (this._gte !== undefined && value < this._gte) {
772
+ ctx.addIssue({
773
+ code: "too_small" /* TooSmall */,
774
+ message: this._gteMessage ?? `Number must be greater than or equal to ${this._gte}`
775
+ });
776
+ }
777
+ if (this._gt !== undefined && value <= this._gt) {
778
+ ctx.addIssue({
779
+ code: "too_small" /* TooSmall */,
780
+ message: this._gtMessage ?? `Number must be greater than ${this._gt}`
781
+ });
782
+ }
783
+ if (this._lte !== undefined && value > this._lte) {
784
+ ctx.addIssue({
785
+ code: "too_big" /* TooBig */,
786
+ message: this._lteMessage ?? `Number must be less than or equal to ${this._lte}`
787
+ });
788
+ }
789
+ if (this._lt !== undefined && value >= this._lt) {
790
+ ctx.addIssue({
791
+ code: "too_big" /* TooBig */,
792
+ message: this._ltMessage ?? `Number must be less than ${this._lt}`
793
+ });
794
+ }
795
+ if (this._int && !Number.isInteger(value)) {
796
+ ctx.addIssue({ code: "invalid_type" /* InvalidType */, message: "Expected integer, received float" });
797
+ }
798
+ if (this._positive && value <= 0) {
799
+ ctx.addIssue({ code: "too_small" /* TooSmall */, message: "Number must be positive" });
800
+ }
801
+ if (this._negative && value >= 0) {
802
+ ctx.addIssue({ code: "too_big" /* TooBig */, message: "Number must be negative" });
803
+ }
804
+ if (this._nonnegative && value < 0) {
805
+ ctx.addIssue({ code: "too_small" /* TooSmall */, message: "Number must be nonnegative" });
806
+ }
807
+ if (this._nonpositive && value > 0) {
808
+ ctx.addIssue({ code: "too_big" /* TooBig */, message: "Number must be nonpositive" });
809
+ }
810
+ if (this._multipleOf !== undefined && value % this._multipleOf !== 0) {
811
+ ctx.addIssue({
812
+ code: "not_multiple_of" /* NotMultipleOf */,
813
+ message: `Number must be a multiple of ${this._multipleOf}`
814
+ });
815
+ }
816
+ if (this._finite && !Number.isFinite(value)) {
817
+ ctx.addIssue({ code: "not_finite" /* NotFinite */, message: "Number must be finite" });
818
+ }
819
+ return value;
820
+ }
821
+ gte(n, message) {
822
+ const clone = this._clone();
823
+ clone._gte = n;
824
+ clone._gteMessage = message;
825
+ return clone;
826
+ }
827
+ min(n, message) {
828
+ return this.gte(n, message);
829
+ }
830
+ gt(n, message) {
831
+ const clone = this._clone();
832
+ clone._gt = n;
833
+ clone._gtMessage = message;
834
+ return clone;
835
+ }
836
+ lte(n, message) {
837
+ const clone = this._clone();
838
+ clone._lte = n;
839
+ clone._lteMessage = message;
840
+ return clone;
841
+ }
842
+ max(n, message) {
843
+ return this.lte(n, message);
844
+ }
845
+ lt(n, message) {
846
+ const clone = this._clone();
847
+ clone._lt = n;
848
+ clone._ltMessage = message;
849
+ return clone;
850
+ }
851
+ int() {
852
+ const clone = this._clone();
853
+ clone._int = true;
854
+ return clone;
855
+ }
856
+ positive() {
857
+ const clone = this._clone();
858
+ clone._positive = true;
859
+ return clone;
860
+ }
861
+ negative() {
862
+ const clone = this._clone();
863
+ clone._negative = true;
864
+ return clone;
865
+ }
866
+ nonnegative() {
867
+ const clone = this._clone();
868
+ clone._nonnegative = true;
869
+ return clone;
870
+ }
871
+ nonpositive() {
872
+ const clone = this._clone();
873
+ clone._nonpositive = true;
874
+ return clone;
875
+ }
876
+ multipleOf(n) {
877
+ const clone = this._clone();
878
+ clone._multipleOf = n;
879
+ return clone;
880
+ }
881
+ step(n) {
882
+ return this.multipleOf(n);
883
+ }
884
+ finite() {
885
+ const clone = this._clone();
886
+ clone._finite = true;
887
+ return clone;
888
+ }
889
+ _schemaType() {
890
+ return "number" /* Number */;
891
+ }
892
+ _toJSONSchema(_tracker) {
893
+ const schema = { type: this._int ? "integer" : "number" };
894
+ if (this._gte !== undefined)
895
+ schema.minimum = this._gte;
896
+ if (this._gt !== undefined)
897
+ schema.exclusiveMinimum = this._gt;
898
+ if (this._lte !== undefined)
899
+ schema.maximum = this._lte;
900
+ if (this._lt !== undefined)
901
+ schema.exclusiveMaximum = this._lt;
902
+ if (this._multipleOf !== undefined)
903
+ schema.multipleOf = this._multipleOf;
904
+ return schema;
905
+ }
906
+ _clone() {
907
+ const clone = this._cloneBase(new NumberSchema);
908
+ clone._gte = this._gte;
909
+ clone._gteMessage = this._gteMessage;
910
+ clone._gt = this._gt;
911
+ clone._gtMessage = this._gtMessage;
912
+ clone._lte = this._lte;
913
+ clone._lteMessage = this._lteMessage;
914
+ clone._lt = this._lt;
915
+ clone._ltMessage = this._ltMessage;
916
+ clone._int = this._int;
917
+ clone._positive = this._positive;
918
+ clone._negative = this._negative;
919
+ clone._nonnegative = this._nonnegative;
920
+ clone._nonpositive = this._nonpositive;
921
+ clone._multipleOf = this._multipleOf;
922
+ clone._finite = this._finite;
923
+ return clone;
924
+ }
925
+ }
926
+
927
+ // src/schemas/string.ts
928
+ class StringSchema extends Schema {
929
+ _min;
930
+ _minMessage;
931
+ _max;
932
+ _maxMessage;
933
+ _length;
934
+ _lengthMessage;
935
+ _regex;
936
+ _startsWith;
937
+ _endsWith;
938
+ _includes;
939
+ _uppercase = false;
940
+ _lowercase = false;
941
+ _trim = false;
942
+ _toLowerCase = false;
943
+ _toUpperCase = false;
944
+ _normalize = false;
945
+ _parse(value, ctx) {
946
+ if (typeof value !== "string") {
947
+ ctx.addIssue({
948
+ code: "invalid_type" /* InvalidType */,
949
+ message: `Expected string, received ${typeof value}`
950
+ });
951
+ return value;
952
+ }
953
+ let v = value;
954
+ if (this._trim) {
955
+ v = v.trim();
956
+ }
957
+ if (this._toLowerCase) {
958
+ v = v.toLowerCase();
959
+ }
960
+ if (this._toUpperCase) {
961
+ v = v.toUpperCase();
962
+ }
963
+ if (this._normalize) {
964
+ v = v.normalize();
965
+ }
966
+ if (this._min !== undefined && v.length < this._min) {
967
+ ctx.addIssue({
968
+ code: "too_small" /* TooSmall */,
969
+ message: this._minMessage ?? `String must contain at least ${this._min} character(s)`
970
+ });
971
+ }
972
+ if (this._max !== undefined && v.length > this._max) {
973
+ ctx.addIssue({
974
+ code: "too_big" /* TooBig */,
975
+ message: this._maxMessage ?? `String must contain at most ${this._max} character(s)`
976
+ });
977
+ }
978
+ if (this._length !== undefined && v.length !== this._length) {
979
+ ctx.addIssue({
980
+ code: "invalid_string" /* InvalidString */,
981
+ message: this._lengthMessage ?? `String must be exactly ${this._length} character(s)`
982
+ });
983
+ }
984
+ if (this._regex !== undefined && !this._regex.test(v)) {
985
+ ctx.addIssue({
986
+ code: "invalid_string" /* InvalidString */,
987
+ message: `Invalid: must match ${this._regex}`
988
+ });
989
+ }
990
+ if (this._startsWith !== undefined && !v.startsWith(this._startsWith)) {
991
+ ctx.addIssue({
992
+ code: "invalid_string" /* InvalidString */,
993
+ message: `Invalid input: must start with "${this._startsWith}"`
994
+ });
995
+ }
996
+ if (this._endsWith !== undefined && !v.endsWith(this._endsWith)) {
997
+ ctx.addIssue({
998
+ code: "invalid_string" /* InvalidString */,
999
+ message: `Invalid input: must end with "${this._endsWith}"`
1000
+ });
1001
+ }
1002
+ if (this._includes !== undefined && !v.includes(this._includes)) {
1003
+ ctx.addIssue({
1004
+ code: "invalid_string" /* InvalidString */,
1005
+ message: `Invalid input: must include "${this._includes}"`
1006
+ });
1007
+ }
1008
+ if (this._uppercase && v !== v.toUpperCase()) {
1009
+ ctx.addIssue({ code: "invalid_string" /* InvalidString */, message: "Expected string to be uppercase" });
1010
+ }
1011
+ if (this._lowercase && v !== v.toLowerCase()) {
1012
+ ctx.addIssue({ code: "invalid_string" /* InvalidString */, message: "Expected string to be lowercase" });
1013
+ }
1014
+ return v;
1015
+ }
1016
+ min(n, message) {
1017
+ const clone = this._clone();
1018
+ clone._min = n;
1019
+ clone._minMessage = message;
1020
+ return clone;
1021
+ }
1022
+ max(n, message) {
1023
+ const clone = this._clone();
1024
+ clone._max = n;
1025
+ clone._maxMessage = message;
1026
+ return clone;
1027
+ }
1028
+ length(n, message) {
1029
+ const clone = this._clone();
1030
+ clone._length = n;
1031
+ clone._lengthMessage = message;
1032
+ return clone;
1033
+ }
1034
+ regex(pattern) {
1035
+ const clone = this._clone();
1036
+ clone._regex = pattern;
1037
+ return clone;
1038
+ }
1039
+ startsWith(prefix) {
1040
+ const clone = this._clone();
1041
+ clone._startsWith = prefix;
1042
+ return clone;
1043
+ }
1044
+ endsWith(suffix) {
1045
+ const clone = this._clone();
1046
+ clone._endsWith = suffix;
1047
+ return clone;
1048
+ }
1049
+ includes(substring) {
1050
+ const clone = this._clone();
1051
+ clone._includes = substring;
1052
+ return clone;
1053
+ }
1054
+ uppercase() {
1055
+ const clone = this._clone();
1056
+ clone._uppercase = true;
1057
+ return clone;
1058
+ }
1059
+ lowercase() {
1060
+ const clone = this._clone();
1061
+ clone._lowercase = true;
1062
+ return clone;
1063
+ }
1064
+ trim() {
1065
+ const clone = this._clone();
1066
+ clone._trim = true;
1067
+ return clone;
1068
+ }
1069
+ toLowerCase() {
1070
+ const clone = this._clone();
1071
+ clone._toLowerCase = true;
1072
+ return clone;
1073
+ }
1074
+ toUpperCase() {
1075
+ const clone = this._clone();
1076
+ clone._toUpperCase = true;
1077
+ return clone;
1078
+ }
1079
+ normalize() {
1080
+ const clone = this._clone();
1081
+ clone._normalize = true;
1082
+ return clone;
1083
+ }
1084
+ _schemaType() {
1085
+ return "string" /* String */;
1086
+ }
1087
+ _toJSONSchema(_tracker) {
1088
+ const schema = { type: "string" };
1089
+ if (this._min !== undefined)
1090
+ schema.minLength = this._min;
1091
+ if (this._max !== undefined)
1092
+ schema.maxLength = this._max;
1093
+ if (this._regex !== undefined)
1094
+ schema.pattern = this._regex.source;
1095
+ return schema;
1096
+ }
1097
+ _clone() {
1098
+ const clone = this._cloneBase(new StringSchema);
1099
+ clone._min = this._min;
1100
+ clone._minMessage = this._minMessage;
1101
+ clone._max = this._max;
1102
+ clone._maxMessage = this._maxMessage;
1103
+ clone._length = this._length;
1104
+ clone._lengthMessage = this._lengthMessage;
1105
+ clone._regex = this._regex;
1106
+ clone._startsWith = this._startsWith;
1107
+ clone._endsWith = this._endsWith;
1108
+ clone._includes = this._includes;
1109
+ clone._uppercase = this._uppercase;
1110
+ clone._lowercase = this._lowercase;
1111
+ clone._trim = this._trim;
1112
+ clone._toLowerCase = this._toLowerCase;
1113
+ clone._toUpperCase = this._toUpperCase;
1114
+ clone._normalize = this._normalize;
1115
+ return clone;
1116
+ }
1117
+ }
1118
+
1119
+ // src/schemas/coerced.ts
1120
+ class CoercedStringSchema extends StringSchema {
1121
+ _parse(value, ctx) {
1122
+ return super._parse(value == null ? "" : String(value), ctx);
1123
+ }
1124
+ _clone() {
1125
+ return Object.assign(new CoercedStringSchema, super._clone());
1126
+ }
1127
+ }
1128
+
1129
+ class CoercedNumberSchema extends NumberSchema {
1130
+ _parse(value, ctx) {
1131
+ return super._parse(Number(value), ctx);
1132
+ }
1133
+ _clone() {
1134
+ return Object.assign(new CoercedNumberSchema, super._clone());
1135
+ }
1136
+ }
1137
+
1138
+ class CoercedBooleanSchema extends BooleanSchema {
1139
+ _parse(value, ctx) {
1140
+ return super._parse(Boolean(value), ctx);
1141
+ }
1142
+ _clone() {
1143
+ return Object.assign(new CoercedBooleanSchema, super._clone());
1144
+ }
1145
+ }
1146
+
1147
+ class CoercedBigIntSchema extends BigIntSchema {
1148
+ _parse(value, ctx) {
1149
+ if (typeof value === "bigint")
1150
+ return super._parse(value, ctx);
1151
+ try {
1152
+ return super._parse(BigInt(value), ctx);
1153
+ } catch {
1154
+ ctx.addIssue({ code: "invalid_type" /* InvalidType */, message: "Expected value coercible to bigint" });
1155
+ return value;
1156
+ }
1157
+ }
1158
+ _clone() {
1159
+ return Object.assign(new CoercedBigIntSchema, super._clone());
1160
+ }
1161
+ }
1162
+
1163
+ class CoercedDateSchema extends DateSchema {
1164
+ _parse(value, ctx) {
1165
+ if (value instanceof Date)
1166
+ return super._parse(value, ctx);
1167
+ return super._parse(new Date(value), ctx);
1168
+ }
1169
+ _clone() {
1170
+ return Object.assign(new CoercedDateSchema, super._clone());
1171
+ }
1172
+ }
1173
+ // src/schemas/custom.ts
1174
+ class CustomSchema extends Schema {
1175
+ _check;
1176
+ _message;
1177
+ constructor(check, message) {
1178
+ super();
1179
+ this._check = check;
1180
+ this._message = message ?? "Custom validation failed";
1181
+ }
1182
+ _parse(value, ctx) {
1183
+ if (!this._check(value)) {
1184
+ ctx.addIssue({ code: "custom" /* Custom */, message: this._message });
1185
+ }
1186
+ return value;
1187
+ }
1188
+ _schemaType() {
1189
+ return "custom" /* Custom */;
1190
+ }
1191
+ _toJSONSchema(_tracker) {
1192
+ return {};
1193
+ }
1194
+ _clone() {
1195
+ return this._cloneBase(new CustomSchema(this._check, this._message));
1196
+ }
1197
+ }
1198
+ // src/schemas/literal.ts
1199
+ class LiteralSchema extends Schema {
1200
+ _value;
1201
+ constructor(value) {
1202
+ super();
1203
+ this._value = value;
1204
+ }
1205
+ get value() {
1206
+ return this._value;
1207
+ }
1208
+ _parse(value, ctx) {
1209
+ if (value !== this._value) {
1210
+ ctx.addIssue({
1211
+ code: "invalid_literal" /* InvalidLiteral */,
1212
+ message: `Expected ${JSON.stringify(this._value)}, received ${JSON.stringify(value)}`
1213
+ });
1214
+ }
1215
+ return value;
1216
+ }
1217
+ _schemaType() {
1218
+ return "literal" /* Literal */;
1219
+ }
1220
+ _toJSONSchema(_tracker) {
1221
+ return { const: this._value };
1222
+ }
1223
+ _clone() {
1224
+ return this._cloneBase(new LiteralSchema(this._value));
1225
+ }
1226
+ }
1227
+
1228
+ // src/schemas/discriminated-union.ts
1229
+ function receivedType(value) {
1230
+ if (value === null)
1231
+ return "null";
1232
+ if (Array.isArray(value))
1233
+ return "array";
1234
+ return typeof value;
1235
+ }
1236
+
1237
+ class DiscriminatedUnionSchema extends Schema {
1238
+ _discriminator;
1239
+ _options;
1240
+ _lookup;
1241
+ constructor(discriminator, options) {
1242
+ super();
1243
+ this._discriminator = discriminator;
1244
+ this._options = options;
1245
+ this._lookup = new Map;
1246
+ for (const option of options) {
1247
+ const discriminatorSchema = option.shape[discriminator];
1248
+ if (!(discriminatorSchema instanceof LiteralSchema)) {
1249
+ throw new Error(`Discriminated union requires all options to have a literal "${discriminator}" property`);
1250
+ }
1251
+ this._lookup.set(discriminatorSchema.value, option);
1252
+ }
1253
+ }
1254
+ _parse(value, ctx) {
1255
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
1256
+ ctx.addIssue({
1257
+ code: "invalid_type" /* InvalidType */,
1258
+ message: `Expected object, received ${receivedType(value)}`
1259
+ });
1260
+ return value;
1261
+ }
1262
+ const obj = value;
1263
+ const discriminatorValue = obj[this._discriminator];
1264
+ if (discriminatorValue === undefined) {
1265
+ ctx.addIssue({
1266
+ code: "invalid_union" /* InvalidUnion */,
1267
+ message: `Missing discriminator property "${this._discriminator}"`
1268
+ });
1269
+ return value;
1270
+ }
1271
+ const matchedSchema = this._lookup.get(discriminatorValue);
1272
+ if (!matchedSchema) {
1273
+ const expected = [...this._lookup.keys()].map((k) => `'${k}'`).join(" | ");
1274
+ ctx.addIssue({
1275
+ code: "invalid_union" /* InvalidUnion */,
1276
+ message: `Invalid discriminator value. Expected ${expected}, received '${discriminatorValue}'`
1277
+ });
1278
+ return value;
1279
+ }
1280
+ return matchedSchema._runPipeline(value, ctx);
1281
+ }
1282
+ _schemaType() {
1283
+ return "discriminatedUnion" /* DiscriminatedUnion */;
1284
+ }
1285
+ _toJSONSchema(tracker) {
1286
+ return {
1287
+ oneOf: this._options.map((option) => option._toJSONSchemaWithRefs(tracker)),
1288
+ discriminator: { propertyName: this._discriminator }
1289
+ };
1290
+ }
1291
+ _clone() {
1292
+ return this._cloneBase(new DiscriminatedUnionSchema(this._discriminator, this._options));
1293
+ }
1294
+ }
1295
+ // src/schemas/enum.ts
1296
+ class EnumSchema extends Schema {
1297
+ _values;
1298
+ constructor(values) {
1299
+ super();
1300
+ this._values = values;
1301
+ }
1302
+ _parse(value, ctx) {
1303
+ if (!this._values.includes(value)) {
1304
+ ctx.addIssue({
1305
+ code: "invalid_enum_value" /* InvalidEnumValue */,
1306
+ message: `Invalid enum value. Expected ${this._values.map((v) => `'${v}'`).join(" | ")}, received '${value}'`
1307
+ });
1308
+ }
1309
+ return value;
1310
+ }
1311
+ _schemaType() {
1312
+ return "enum" /* Enum */;
1313
+ }
1314
+ _toJSONSchema(_tracker) {
1315
+ return { enum: [...this._values] };
1316
+ }
1317
+ exclude(values) {
1318
+ const remaining = this._values.filter((v) => !values.includes(v));
1319
+ const schema = new EnumSchema(remaining);
1320
+ return this._cloneBase(schema);
1321
+ }
1322
+ extract(values) {
1323
+ const schema = new EnumSchema(values);
1324
+ return this._cloneBase(schema);
1325
+ }
1326
+ _clone() {
1327
+ return this._cloneBase(new EnumSchema(this._values));
1328
+ }
1329
+ }
1330
+ // src/schemas/file.ts
1331
+ class FileSchema extends Schema {
1332
+ _parse(value, ctx) {
1333
+ if (!(value instanceof Blob)) {
1334
+ ctx.addIssue({ code: "invalid_type" /* InvalidType */, message: "Expected File or Blob" });
1335
+ return value;
1336
+ }
1337
+ return value;
1338
+ }
1339
+ _schemaType() {
1340
+ return "file" /* File */;
1341
+ }
1342
+ _toJSONSchema(_tracker) {
1343
+ return { type: "string", contentMediaType: "application/octet-stream" };
1344
+ }
1345
+ _clone() {
1346
+ return this._cloneBase(new FileSchema);
1347
+ }
1348
+ }
1349
+ // src/schemas/formats/format-schema.ts
1350
+ class FormatSchema extends StringSchema {
1351
+ _jsonSchemaExtra() {
1352
+ return;
1353
+ }
1354
+ _parse(value, ctx) {
1355
+ const result = super._parse(value, ctx);
1356
+ if (ctx.hasIssues())
1357
+ return result;
1358
+ if (!this._validate(result)) {
1359
+ ctx.addIssue({ code: "invalid_string" /* InvalidString */, message: this._errorMessage });
1360
+ }
1361
+ return result;
1362
+ }
1363
+ _toJSONSchema(tracker) {
1364
+ const extra = this._jsonSchemaExtra();
1365
+ if (!extra)
1366
+ return super._toJSONSchema(tracker);
1367
+ return { ...super._toJSONSchema(tracker), ...extra };
1368
+ }
1369
+ _clone() {
1370
+ const Ctor = this.constructor;
1371
+ return Object.assign(new Ctor, super._clone());
1372
+ }
1373
+ }
1374
+
1375
+ // src/schemas/formats/base64.ts
1376
+ var BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/;
1377
+
1378
+ class Base64Schema extends FormatSchema {
1379
+ _errorMessage = "Invalid base64 string";
1380
+ _validate(value) {
1381
+ return BASE64_RE.test(value) && value.length % 4 === 0;
1382
+ }
1383
+ _jsonSchemaExtra() {
1384
+ return { contentEncoding: "base64" };
1385
+ }
1386
+ }
1387
+ // src/schemas/formats/cuid.ts
1388
+ var CUID_RE = /^c[a-z0-9]{24}$/;
1389
+
1390
+ class CuidSchema extends FormatSchema {
1391
+ _errorMessage = "Invalid CUID";
1392
+ _validate(value) {
1393
+ return CUID_RE.test(value);
1394
+ }
1395
+ }
1396
+ // src/schemas/formats/email.ts
1397
+ var EMAIL_RE = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
1398
+
1399
+ class EmailSchema extends FormatSchema {
1400
+ _errorMessage = "Invalid email";
1401
+ _validate(value) {
1402
+ return EMAIL_RE.test(value);
1403
+ }
1404
+ _jsonSchemaExtra() {
1405
+ return { format: "email" };
1406
+ }
1407
+ }
1408
+ // src/schemas/formats/hex.ts
1409
+ var HEX_RE = /^[0-9a-fA-F]+$/;
1410
+
1411
+ class HexSchema extends FormatSchema {
1412
+ _errorMessage = "Invalid hex string";
1413
+ _validate(value) {
1414
+ return HEX_RE.test(value);
1415
+ }
1416
+ _jsonSchemaExtra() {
1417
+ return { pattern: "^[0-9a-fA-F]+$" };
1418
+ }
1419
+ }
1420
+ // src/schemas/formats/hostname.ts
1421
+ var HOSTNAME_RE = /^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
1422
+
1423
+ class HostnameSchema extends FormatSchema {
1424
+ _errorMessage = "Invalid hostname";
1425
+ _validate(value) {
1426
+ return HOSTNAME_RE.test(value) && value.length <= 253;
1427
+ }
1428
+ _jsonSchemaExtra() {
1429
+ return { format: "hostname" };
1430
+ }
1431
+ }
1432
+ // src/schemas/formats/ipv4.ts
1433
+ var IPV4_RE = /^(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})$/;
1434
+
1435
+ class Ipv4Schema extends FormatSchema {
1436
+ _errorMessage = "Invalid IPv4 address";
1437
+ _validate(value) {
1438
+ const match = IPV4_RE.exec(value);
1439
+ if (!match)
1440
+ return false;
1441
+ return [match[1], match[2], match[3], match[4]].every((o) => Number(o) <= 255);
1442
+ }
1443
+ _jsonSchemaExtra() {
1444
+ return { format: "ipv4" };
1445
+ }
1446
+ }
1447
+ // src/schemas/formats/ipv6.ts
1448
+ var IPV6_RE = /^(([0-9a-fA-F]{1,4}:){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]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$/;
1449
+
1450
+ class Ipv6Schema extends FormatSchema {
1451
+ _errorMessage = "Invalid IPv6 address";
1452
+ _validate(value) {
1453
+ return IPV6_RE.test(value);
1454
+ }
1455
+ _jsonSchemaExtra() {
1456
+ return { format: "ipv6" };
1457
+ }
1458
+ }
1459
+ // src/schemas/formats/iso.ts
1460
+ var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
1461
+ var ISO_TIME_RE = /^\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$/;
1462
+ var ISO_DATETIME_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$/;
1463
+ var ISO_DURATION_RE = /^P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+(\.\d+)?S)?)?$/;
1464
+ function validateDateRange(value) {
1465
+ const parts = value.split("-").map(Number);
1466
+ const y = parts[0] ?? 0;
1467
+ const m = parts[1] ?? 0;
1468
+ const d = parts[2] ?? 0;
1469
+ const date = new Date(y, m - 1, d);
1470
+ return date.getFullYear() === y && date.getMonth() === m - 1 && date.getDate() === d;
1471
+ }
1472
+ function validateTimeRange(value) {
1473
+ const parts = value.split(":").map((v) => Number(v.replace(/[^0-9.]/g, "")));
1474
+ const h = parts[0] ?? 0;
1475
+ const min = parts[1] ?? 0;
1476
+ const s = parts[2] ?? 0;
1477
+ return h >= 0 && h <= 23 && min >= 0 && min <= 59 && s >= 0 && s <= 59;
1478
+ }
1479
+
1480
+ class IsoDateSchema extends FormatSchema {
1481
+ _errorMessage = "Invalid ISO date";
1482
+ _validate(value) {
1483
+ return ISO_DATE_RE.test(value) && validateDateRange(value);
1484
+ }
1485
+ _jsonSchemaExtra() {
1486
+ return { format: "date" };
1487
+ }
1488
+ }
1489
+
1490
+ class IsoTimeSchema extends FormatSchema {
1491
+ _errorMessage = "Invalid ISO time";
1492
+ _validate(value) {
1493
+ return ISO_TIME_RE.test(value) && validateTimeRange(value);
1494
+ }
1495
+ _jsonSchemaExtra() {
1496
+ return { format: "time" };
1497
+ }
1498
+ }
1499
+
1500
+ class IsoDatetimeSchema extends FormatSchema {
1501
+ _errorMessage = "Invalid ISO datetime";
1502
+ _validate(value) {
1503
+ if (!ISO_DATETIME_RE.test(value))
1504
+ return false;
1505
+ const parts = value.split("T");
1506
+ const datePart = parts[0] ?? "";
1507
+ const timePart = parts[1] ?? "";
1508
+ return validateDateRange(datePart) && validateTimeRange(timePart);
1509
+ }
1510
+ _jsonSchemaExtra() {
1511
+ return { format: "date-time" };
1512
+ }
1513
+ }
1514
+
1515
+ class IsoDurationSchema extends FormatSchema {
1516
+ _errorMessage = "Invalid ISO duration";
1517
+ _validate(value) {
1518
+ return ISO_DURATION_RE.test(value) && value !== "P";
1519
+ }
1520
+ _jsonSchemaExtra() {
1521
+ return { format: "duration" };
1522
+ }
1523
+ }
1524
+ // src/schemas/formats/jwt.ts
1525
+ var JWT_RE = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
1526
+
1527
+ class JwtSchema extends FormatSchema {
1528
+ _errorMessage = "Invalid JWT";
1529
+ _validate(value) {
1530
+ return JWT_RE.test(value);
1531
+ }
1532
+ }
1533
+ // src/schemas/formats/nanoid.ts
1534
+ var NANOID_RE = /^[A-Za-z0-9_-]{21}$/;
1535
+
1536
+ class NanoidSchema extends FormatSchema {
1537
+ _errorMessage = "Invalid nanoid";
1538
+ _validate(value) {
1539
+ return NANOID_RE.test(value);
1540
+ }
1541
+ }
1542
+ // src/schemas/formats/ulid.ts
1543
+ var ULID_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/;
1544
+
1545
+ class UlidSchema extends FormatSchema {
1546
+ _errorMessage = "Invalid ULID";
1547
+ _validate(value) {
1548
+ return ULID_RE.test(value);
1549
+ }
1550
+ }
1551
+ // src/schemas/formats/url.ts
1552
+ class UrlSchema extends FormatSchema {
1553
+ _errorMessage = "Invalid URL";
1554
+ _validate(value) {
1555
+ try {
1556
+ new URL(value);
1557
+ return true;
1558
+ } catch {
1559
+ return false;
1560
+ }
1561
+ }
1562
+ _jsonSchemaExtra() {
1563
+ return { format: "uri" };
1564
+ }
1565
+ }
1566
+ // src/schemas/formats/uuid.ts
1567
+ var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
1568
+
1569
+ class UuidSchema extends FormatSchema {
1570
+ _errorMessage = "Invalid UUID";
1571
+ _validate(value) {
1572
+ return UUID_RE.test(value);
1573
+ }
1574
+ _jsonSchemaExtra() {
1575
+ return { format: "uuid" };
1576
+ }
1577
+ }
1578
+ // src/schemas/instanceof.ts
1579
+ class InstanceOfSchema extends Schema {
1580
+ _cls;
1581
+ constructor(cls) {
1582
+ super();
1583
+ this._cls = cls;
1584
+ }
1585
+ _parse(value, ctx) {
1586
+ if (!(value instanceof this._cls)) {
1587
+ ctx.addIssue({
1588
+ code: "invalid_type" /* InvalidType */,
1589
+ message: `Expected instance of ${this._cls.name}`
1590
+ });
1591
+ return value;
1592
+ }
1593
+ return value;
1594
+ }
1595
+ _schemaType() {
1596
+ return "instanceof" /* InstanceOf */;
1597
+ }
1598
+ _toJSONSchema(_tracker) {
1599
+ return {};
1600
+ }
1601
+ _clone() {
1602
+ return this._cloneBase(new InstanceOfSchema(this._cls));
1603
+ }
1604
+ }
1605
+ // src/schemas/intersection.ts
1606
+ class IntersectionSchema extends Schema {
1607
+ _left;
1608
+ _right;
1609
+ constructor(left, right) {
1610
+ super();
1611
+ this._left = left;
1612
+ this._right = right;
1613
+ }
1614
+ _parse(value, ctx) {
1615
+ const leftResult = this._left.safeParse(value);
1616
+ const rightResult = this._right.safeParse(value);
1617
+ if (!leftResult.success || !rightResult.success) {
1618
+ ctx.addIssue({
1619
+ code: "invalid_intersection" /* InvalidIntersection */,
1620
+ message: "Value does not satisfy intersection"
1621
+ });
1622
+ return value;
1623
+ }
1624
+ if (typeof leftResult.data === "object" && leftResult.data !== null && typeof rightResult.data === "object" && rightResult.data !== null) {
1625
+ return { ...leftResult.data, ...rightResult.data };
1626
+ }
1627
+ return leftResult.data;
1628
+ }
1629
+ _schemaType() {
1630
+ return "intersection" /* Intersection */;
1631
+ }
1632
+ _toJSONSchema(tracker) {
1633
+ return {
1634
+ allOf: [
1635
+ this._left._toJSONSchemaWithRefs(tracker),
1636
+ this._right._toJSONSchemaWithRefs(tracker)
1637
+ ]
1638
+ };
1639
+ }
1640
+ _clone() {
1641
+ return this._cloneBase(new IntersectionSchema(this._left, this._right));
1642
+ }
1643
+ }
1644
+ // src/schemas/lazy.ts
1645
+ class LazySchema extends Schema {
1646
+ _getter;
1647
+ _cached;
1648
+ constructor(getter) {
1649
+ super();
1650
+ this._getter = getter;
1651
+ }
1652
+ _resolve() {
1653
+ if (!this._cached) {
1654
+ this._cached = this._getter();
1655
+ }
1656
+ return this._cached;
1657
+ }
1658
+ _parse(value, ctx) {
1659
+ return this._resolve()._runPipeline(value, ctx);
1660
+ }
1661
+ _schemaType() {
1662
+ return "lazy" /* Lazy */;
1663
+ }
1664
+ _toJSONSchema(tracker) {
1665
+ return this._resolve()._toJSONSchemaWithRefs(tracker);
1666
+ }
1667
+ _clone() {
1668
+ return this._cloneBase(new LazySchema(this._getter));
1669
+ }
1670
+ }
1671
+ // src/schemas/map.ts
1672
+ class MapSchema extends Schema {
1673
+ _keySchema;
1674
+ _valueSchema;
1675
+ constructor(keySchema, valueSchema) {
1676
+ super();
1677
+ this._keySchema = keySchema;
1678
+ this._valueSchema = valueSchema;
1679
+ }
1680
+ _parse(value, ctx) {
1681
+ if (!(value instanceof Map)) {
1682
+ ctx.addIssue({
1683
+ code: "invalid_type" /* InvalidType */,
1684
+ message: `Expected Map, received ${typeof value}`
1685
+ });
1686
+ return value;
1687
+ }
1688
+ const result = new Map;
1689
+ let index = 0;
1690
+ for (const [k, v] of value) {
1691
+ ctx.pushPath(index);
1692
+ const parsedKey = this._keySchema._runPipeline(k, ctx);
1693
+ const parsedValue = this._valueSchema._runPipeline(v, ctx);
1694
+ result.set(parsedKey, parsedValue);
1695
+ ctx.popPath();
1696
+ index++;
1697
+ }
1698
+ return result;
1699
+ }
1700
+ _schemaType() {
1701
+ return "map" /* Map */;
1702
+ }
1703
+ _toJSONSchema(tracker) {
1704
+ return {
1705
+ type: "array",
1706
+ items: {
1707
+ type: "array",
1708
+ prefixItems: [
1709
+ this._keySchema._toJSONSchemaWithRefs(tracker),
1710
+ this._valueSchema._toJSONSchemaWithRefs(tracker)
1711
+ ],
1712
+ items: false
1713
+ }
1714
+ };
1715
+ }
1716
+ _clone() {
1717
+ return this._cloneBase(new MapSchema(this._keySchema, this._valueSchema));
1718
+ }
1719
+ }
1720
+ // src/schemas/nan.ts
1721
+ class NanSchema extends Schema {
1722
+ _parse(value, ctx) {
1723
+ if (typeof value !== "number" || !Number.isNaN(value)) {
1724
+ ctx.addIssue({ code: "invalid_type" /* InvalidType */, message: "Expected NaN" });
1725
+ return value;
1726
+ }
1727
+ return value;
1728
+ }
1729
+ _schemaType() {
1730
+ return "nan" /* NaN */;
1731
+ }
1732
+ _toJSONSchema(_tracker) {
1733
+ return { not: {} };
1734
+ }
1735
+ _clone() {
1736
+ return this._cloneBase(new NanSchema);
1737
+ }
1738
+ }
1739
+ // src/schemas/object.ts
1740
+ function receivedType2(value) {
1741
+ if (value === null)
1742
+ return "null";
1743
+ if (Array.isArray(value))
1744
+ return "array";
1745
+ return typeof value;
1746
+ }
1747
+
1748
+ class ObjectSchema extends Schema {
1749
+ _shape;
1750
+ _unknownKeys = "strip";
1751
+ _catchall;
1752
+ constructor(shape) {
1753
+ super();
1754
+ this._shape = shape;
1755
+ }
1756
+ get shape() {
1757
+ return this._shape;
1758
+ }
1759
+ _isOptionalKey(schema) {
1760
+ return schema instanceof OptionalSchema || schema instanceof DefaultSchema;
1761
+ }
1762
+ _parse(value, ctx) {
1763
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
1764
+ ctx.addIssue({
1765
+ code: "invalid_type" /* InvalidType */,
1766
+ message: `Expected object, received ${receivedType2(value)}`
1767
+ });
1768
+ return value;
1769
+ }
1770
+ const obj = value;
1771
+ const result = {};
1772
+ const shapeKeys = new Set(Object.keys(this._shape));
1773
+ for (const key of shapeKeys) {
1774
+ const schema = this._shape[key];
1775
+ if (!(key in obj) && schema && !this._isOptionalKey(schema)) {
1776
+ ctx.addIssue({
1777
+ code: "missing_property" /* MissingProperty */,
1778
+ message: `Missing required property "${key}"`,
1779
+ path: [key]
1780
+ });
1781
+ continue;
1782
+ }
1783
+ ctx.pushPath(key);
1784
+ result[key] = schema?._runPipeline(obj[key], ctx);
1785
+ ctx.popPath();
1786
+ }
1787
+ const unknownKeys = Object.keys(obj).filter((k) => !shapeKeys.has(k));
1788
+ if (unknownKeys.length > 0) {
1789
+ if (this._catchall) {
1790
+ for (const key of unknownKeys) {
1791
+ ctx.pushPath(key);
1792
+ result[key] = this._catchall._runPipeline(obj[key], ctx);
1793
+ ctx.popPath();
1794
+ }
1795
+ } else if (this._unknownKeys === "strict") {
1796
+ ctx.addIssue({
1797
+ code: "unrecognized_keys" /* UnrecognizedKeys */,
1798
+ message: `Unrecognized key(s) in object: ${unknownKeys.map((k) => `"${k}"`).join(", ")}`
1799
+ });
1800
+ } else if (this._unknownKeys === "passthrough") {
1801
+ for (const key of unknownKeys) {
1802
+ result[key] = obj[key];
1803
+ }
1804
+ }
1805
+ }
1806
+ return result;
1807
+ }
1808
+ strict() {
1809
+ const clone = this._clone();
1810
+ clone._unknownKeys = "strict";
1811
+ return clone;
1812
+ }
1813
+ passthrough() {
1814
+ const clone = this._clone();
1815
+ clone._unknownKeys = "passthrough";
1816
+ return clone;
1817
+ }
1818
+ extend(extension) {
1819
+ return new ObjectSchema({ ...this._shape, ...extension });
1820
+ }
1821
+ merge(other) {
1822
+ return new ObjectSchema({ ...this._shape, ...other.shape });
1823
+ }
1824
+ pick(...keys) {
1825
+ const picked = {};
1826
+ for (const key of keys) {
1827
+ const schema = this._shape[key];
1828
+ if (schema)
1829
+ picked[key] = schema;
1830
+ }
1831
+ return new ObjectSchema(picked);
1832
+ }
1833
+ required() {
1834
+ const requiredShape = {};
1835
+ for (const [key, schema] of Object.entries(this._shape)) {
1836
+ if (schema instanceof OptionalSchema || schema instanceof DefaultSchema) {
1837
+ requiredShape[key] = schema.unwrap();
1838
+ } else {
1839
+ requiredShape[key] = schema;
1840
+ }
1841
+ }
1842
+ return new ObjectSchema(requiredShape);
1843
+ }
1844
+ partial() {
1845
+ const partialShape = {};
1846
+ for (const [key, schema] of Object.entries(this._shape)) {
1847
+ partialShape[key] = schema instanceof OptionalSchema ? schema : schema.optional();
1848
+ }
1849
+ return new ObjectSchema(partialShape);
1850
+ }
1851
+ omit(...keys) {
1852
+ const keysToOmit = new Set(keys);
1853
+ const remaining = {};
1854
+ for (const [key, schema] of Object.entries(this._shape)) {
1855
+ if (!keysToOmit.has(key)) {
1856
+ remaining[key] = schema;
1857
+ }
1858
+ }
1859
+ return new ObjectSchema(remaining);
1860
+ }
1861
+ keyof() {
1862
+ return Object.keys(this._shape);
1863
+ }
1864
+ catchall(schema) {
1865
+ const clone = this._clone();
1866
+ clone._catchall = schema;
1867
+ clone._unknownKeys = "strip";
1868
+ return clone;
1869
+ }
1870
+ _schemaType() {
1871
+ return "object" /* Object */;
1872
+ }
1873
+ _toJSONSchema(tracker) {
1874
+ const properties = {};
1875
+ const required = [];
1876
+ for (const [key, schema2] of Object.entries(this._shape)) {
1877
+ properties[key] = schema2._toJSONSchemaWithRefs(tracker);
1878
+ if (!this._isOptionalKey(schema2)) {
1879
+ required.push(key);
1880
+ }
1881
+ }
1882
+ const schema = { type: "object", properties };
1883
+ if (required.length > 0) {
1884
+ schema.required = required;
1885
+ }
1886
+ if (this._unknownKeys === "strict") {
1887
+ schema.additionalProperties = false;
1888
+ }
1889
+ if (this._catchall) {
1890
+ schema.additionalProperties = this._catchall._toJSONSchemaWithRefs(tracker);
1891
+ }
1892
+ return schema;
1893
+ }
1894
+ _clone() {
1895
+ const clone = this._cloneBase(new ObjectSchema(this._shape));
1896
+ clone._unknownKeys = this._unknownKeys;
1897
+ clone._catchall = this._catchall;
1898
+ return clone;
1899
+ }
1900
+ }
1901
+ // src/schemas/record.ts
1902
+ function receivedType3(value) {
1903
+ if (value === null)
1904
+ return "null";
1905
+ if (Array.isArray(value))
1906
+ return "array";
1907
+ return typeof value;
1908
+ }
1909
+
1910
+ class RecordSchema extends Schema {
1911
+ _keySchema;
1912
+ _valueSchema;
1913
+ constructor(keyOrValue, valueSchema) {
1914
+ super();
1915
+ if (valueSchema !== undefined) {
1916
+ this._keySchema = keyOrValue;
1917
+ this._valueSchema = valueSchema;
1918
+ } else {
1919
+ this._keySchema = undefined;
1920
+ this._valueSchema = keyOrValue;
1921
+ }
1922
+ }
1923
+ _parse(value, ctx) {
1924
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
1925
+ ctx.addIssue({
1926
+ code: "invalid_type" /* InvalidType */,
1927
+ message: `Expected object, received ${receivedType3(value)}`
1928
+ });
1929
+ return value;
1930
+ }
1931
+ const obj = value;
1932
+ const result = {};
1933
+ for (const key of Object.keys(obj)) {
1934
+ ctx.pushPath(key);
1935
+ if (this._keySchema) {
1936
+ this._keySchema._runPipeline(key, ctx);
1937
+ }
1938
+ result[key] = this._valueSchema._runPipeline(obj[key], ctx);
1939
+ ctx.popPath();
1940
+ }
1941
+ return result;
1942
+ }
1943
+ _schemaType() {
1944
+ return "record" /* Record */;
1945
+ }
1946
+ _toJSONSchema(tracker) {
1947
+ return {
1948
+ type: "object",
1949
+ additionalProperties: this._valueSchema._toJSONSchemaWithRefs(tracker)
1950
+ };
1951
+ }
1952
+ _clone() {
1953
+ if (this._keySchema) {
1954
+ return this._cloneBase(new RecordSchema(this._keySchema, this._valueSchema));
1955
+ }
1956
+ return this._cloneBase(new RecordSchema(this._valueSchema));
1957
+ }
1958
+ }
1959
+ // src/schemas/set.ts
1960
+ class SetSchema extends Schema {
1961
+ _valueSchema;
1962
+ _min;
1963
+ _max;
1964
+ _size;
1965
+ constructor(valueSchema) {
1966
+ super();
1967
+ this._valueSchema = valueSchema;
1968
+ }
1969
+ _parse(value, ctx) {
1970
+ if (!(value instanceof Set)) {
1971
+ ctx.addIssue({
1972
+ code: "invalid_type" /* InvalidType */,
1973
+ message: `Expected Set, received ${typeof value}`
1974
+ });
1975
+ return value;
1976
+ }
1977
+ const result = new Set;
1978
+ let index = 0;
1979
+ for (const item of value) {
1980
+ ctx.pushPath(index);
1981
+ result.add(this._valueSchema._runPipeline(item, ctx));
1982
+ ctx.popPath();
1983
+ index++;
1984
+ }
1985
+ if (this._min !== undefined && result.size < this._min) {
1986
+ ctx.addIssue({
1987
+ code: "too_small" /* TooSmall */,
1988
+ message: `Set must contain at least ${this._min} element(s)`
1989
+ });
1990
+ }
1991
+ if (this._max !== undefined && result.size > this._max) {
1992
+ ctx.addIssue({
1993
+ code: "too_big" /* TooBig */,
1994
+ message: `Set must contain at most ${this._max} element(s)`
1995
+ });
1996
+ }
1997
+ if (this._size !== undefined && result.size !== this._size) {
1998
+ ctx.addIssue({
1999
+ code: "invalid_type" /* InvalidType */,
2000
+ message: `Set must contain exactly ${this._size} element(s)`
2001
+ });
2002
+ }
2003
+ return result;
2004
+ }
2005
+ min(n) {
2006
+ const clone = this._clone();
2007
+ clone._min = n;
2008
+ return clone;
2009
+ }
2010
+ max(n) {
2011
+ const clone = this._clone();
2012
+ clone._max = n;
2013
+ return clone;
2014
+ }
2015
+ size(n) {
2016
+ const clone = this._clone();
2017
+ clone._size = n;
2018
+ return clone;
2019
+ }
2020
+ _schemaType() {
2021
+ return "set" /* Set */;
2022
+ }
2023
+ _toJSONSchema(tracker) {
2024
+ const schema = {
2025
+ type: "array",
2026
+ uniqueItems: true,
2027
+ items: this._valueSchema._toJSONSchemaWithRefs(tracker)
2028
+ };
2029
+ if (this._min !== undefined)
2030
+ schema.minItems = this._min;
2031
+ if (this._max !== undefined)
2032
+ schema.maxItems = this._max;
2033
+ if (this._size !== undefined) {
2034
+ schema.minItems = this._size;
2035
+ schema.maxItems = this._size;
2036
+ }
2037
+ return schema;
2038
+ }
2039
+ _clone() {
2040
+ const clone = this._cloneBase(new SetSchema(this._valueSchema));
2041
+ clone._min = this._min;
2042
+ clone._max = this._max;
2043
+ clone._size = this._size;
2044
+ return clone;
2045
+ }
2046
+ }
2047
+ // src/schemas/special.ts
2048
+ class AnySchema extends Schema {
2049
+ _parse(value, _ctx) {
2050
+ return value;
2051
+ }
2052
+ _schemaType() {
2053
+ return "any" /* Any */;
2054
+ }
2055
+ _toJSONSchema(_tracker) {
2056
+ return {};
2057
+ }
2058
+ _clone() {
2059
+ return this._cloneBase(new AnySchema);
2060
+ }
2061
+ }
2062
+
2063
+ class UnknownSchema extends Schema {
2064
+ _parse(value, _ctx) {
2065
+ return value;
2066
+ }
2067
+ _schemaType() {
2068
+ return "unknown" /* Unknown */;
2069
+ }
2070
+ _toJSONSchema(_tracker) {
2071
+ return {};
2072
+ }
2073
+ _clone() {
2074
+ return this._cloneBase(new UnknownSchema);
2075
+ }
2076
+ }
2077
+
2078
+ class NullSchema extends Schema {
2079
+ _parse(value, ctx) {
2080
+ if (value !== null) {
2081
+ ctx.addIssue({
2082
+ code: "invalid_type" /* InvalidType */,
2083
+ message: `Expected null, received ${typeof value}`
2084
+ });
2085
+ return null;
2086
+ }
2087
+ return value;
2088
+ }
2089
+ _schemaType() {
2090
+ return "null" /* Null */;
2091
+ }
2092
+ _toJSONSchema(_tracker) {
2093
+ return { type: "null" };
2094
+ }
2095
+ _clone() {
2096
+ return this._cloneBase(new NullSchema);
2097
+ }
2098
+ }
2099
+
2100
+ class UndefinedSchema extends Schema {
2101
+ _parse(value, ctx) {
2102
+ if (value !== undefined) {
2103
+ ctx.addIssue({
2104
+ code: "invalid_type" /* InvalidType */,
2105
+ message: `Expected undefined, received ${typeof value}`
2106
+ });
2107
+ return;
2108
+ }
2109
+ return value;
2110
+ }
2111
+ _schemaType() {
2112
+ return "undefined" /* Undefined */;
2113
+ }
2114
+ _toJSONSchema(_tracker) {
2115
+ return {};
2116
+ }
2117
+ _clone() {
2118
+ return this._cloneBase(new UndefinedSchema);
2119
+ }
2120
+ }
2121
+
2122
+ class VoidSchema extends Schema {
2123
+ _parse(value, ctx) {
2124
+ if (value !== undefined) {
2125
+ ctx.addIssue({
2126
+ code: "invalid_type" /* InvalidType */,
2127
+ message: `Expected void (undefined), received ${typeof value}`
2128
+ });
2129
+ }
2130
+ }
2131
+ _schemaType() {
2132
+ return "void" /* Void */;
2133
+ }
2134
+ _toJSONSchema(_tracker) {
2135
+ return {};
2136
+ }
2137
+ _clone() {
2138
+ return this._cloneBase(new VoidSchema);
2139
+ }
2140
+ }
2141
+
2142
+ class NeverSchema extends Schema {
2143
+ _parse(value, ctx) {
2144
+ ctx.addIssue({ code: "invalid_type" /* InvalidType */, message: "No value is allowed" });
2145
+ return value;
2146
+ }
2147
+ _schemaType() {
2148
+ return "never" /* Never */;
2149
+ }
2150
+ _toJSONSchema(_tracker) {
2151
+ return { not: {} };
2152
+ }
2153
+ _clone() {
2154
+ return this._cloneBase(new NeverSchema);
2155
+ }
2156
+ }
2157
+ // src/schemas/symbol.ts
2158
+ class SymbolSchema extends Schema {
2159
+ _parse(value, ctx) {
2160
+ if (typeof value !== "symbol") {
2161
+ ctx.addIssue({
2162
+ code: "invalid_type" /* InvalidType */,
2163
+ message: `Expected symbol, received ${typeof value}`
2164
+ });
2165
+ return value;
2166
+ }
2167
+ return value;
2168
+ }
2169
+ _schemaType() {
2170
+ return "symbol" /* Symbol */;
2171
+ }
2172
+ _toJSONSchema(_tracker) {
2173
+ return { not: {} };
2174
+ }
2175
+ _clone() {
2176
+ return this._cloneBase(new SymbolSchema);
2177
+ }
2178
+ }
2179
+ // src/schemas/tuple.ts
2180
+ class TupleSchema extends Schema {
2181
+ _items;
2182
+ _rest;
2183
+ constructor(items) {
2184
+ super();
2185
+ this._items = items;
2186
+ }
2187
+ _parse(value, ctx) {
2188
+ if (!Array.isArray(value)) {
2189
+ ctx.addIssue({
2190
+ code: "invalid_type" /* InvalidType */,
2191
+ message: `Expected array, received ${typeof value}`
2192
+ });
2193
+ return value;
2194
+ }
2195
+ if (!this._rest && value.length !== this._items.length) {
2196
+ ctx.addIssue({
2197
+ code: "invalid_type" /* InvalidType */,
2198
+ message: `Expected array of length ${this._items.length}, received ${value.length}`
2199
+ });
2200
+ }
2201
+ if (this._rest && value.length < this._items.length) {
2202
+ ctx.addIssue({
2203
+ code: "invalid_type" /* InvalidType */,
2204
+ message: `Expected at least ${this._items.length} element(s), received ${value.length}`
2205
+ });
2206
+ }
2207
+ const result = [];
2208
+ for (let i = 0;i < this._items.length; i++) {
2209
+ ctx.pushPath(i);
2210
+ result.push(this._items[i]?._runPipeline(value[i], ctx));
2211
+ ctx.popPath();
2212
+ }
2213
+ if (this._rest) {
2214
+ for (let i = this._items.length;i < value.length; i++) {
2215
+ ctx.pushPath(i);
2216
+ result.push(this._rest._runPipeline(value[i], ctx));
2217
+ ctx.popPath();
2218
+ }
2219
+ }
2220
+ return result;
2221
+ }
2222
+ rest(schema) {
2223
+ const clone = this._clone();
2224
+ clone._rest = schema;
2225
+ return clone;
2226
+ }
2227
+ _schemaType() {
2228
+ return "tuple" /* Tuple */;
2229
+ }
2230
+ _toJSONSchema(tracker) {
2231
+ const prefixItems = this._items.map((item) => item._toJSONSchemaWithRefs(tracker));
2232
+ const schema = { type: "array", prefixItems };
2233
+ schema.items = this._rest ? this._rest._toJSONSchemaWithRefs(tracker) : false;
2234
+ return schema;
2235
+ }
2236
+ _clone() {
2237
+ const clone = this._cloneBase(new TupleSchema(this._items));
2238
+ clone._rest = this._rest;
2239
+ return clone;
2240
+ }
2241
+ }
2242
+ // src/schemas/union.ts
2243
+ class UnionSchema extends Schema {
2244
+ _options;
2245
+ constructor(options) {
2246
+ super();
2247
+ this._options = options;
2248
+ }
2249
+ _parse(value, ctx) {
2250
+ for (const option of this._options) {
2251
+ const result = option.safeParse(value);
2252
+ if (result.success) {
2253
+ return result.data;
2254
+ }
2255
+ }
2256
+ ctx.addIssue({
2257
+ code: "invalid_union" /* InvalidUnion */,
2258
+ message: `Invalid input: value does not match any option in the union`
2259
+ });
2260
+ return value;
2261
+ }
2262
+ _schemaType() {
2263
+ return "union" /* Union */;
2264
+ }
2265
+ _toJSONSchema(tracker) {
2266
+ return {
2267
+ anyOf: this._options.map((option) => option._toJSONSchemaWithRefs(tracker))
2268
+ };
2269
+ }
2270
+ _clone() {
2271
+ return this._cloneBase(new UnionSchema(this._options));
2272
+ }
2273
+ }
2274
+ // src/transforms/preprocess.ts
2275
+ class PreprocessSchema extends Schema {
2276
+ _preprocess;
2277
+ _inner;
2278
+ constructor(preprocess, inner) {
2279
+ super();
2280
+ this._preprocess = preprocess;
2281
+ this._inner = inner;
2282
+ }
2283
+ _parse(value, ctx) {
2284
+ let processed;
2285
+ try {
2286
+ processed = this._preprocess(value);
2287
+ } catch (e) {
2288
+ ctx.addIssue({
2289
+ code: "custom" /* Custom */,
2290
+ message: e instanceof Error ? e.message : "Preprocess failed"
2291
+ });
2292
+ return value;
2293
+ }
2294
+ return this._inner._runPipeline(processed, ctx);
2295
+ }
2296
+ _schemaType() {
2297
+ return this._inner._schemaType();
2298
+ }
2299
+ _toJSONSchema(tracker) {
2300
+ return this._inner._toJSONSchemaWithRefs(tracker);
2301
+ }
2302
+ _clone() {
2303
+ return this._cloneBase(new PreprocessSchema(this._preprocess, this._inner));
2304
+ }
2305
+ }
2306
+ function preprocess(fn, schema) {
2307
+ return new PreprocessSchema(fn, schema);
2308
+ }
2309
+ // src/index.ts
2310
+ var s = {
2311
+ string: () => new StringSchema,
2312
+ number: () => new NumberSchema,
2313
+ boolean: () => new BooleanSchema,
2314
+ bigint: () => new BigIntSchema,
2315
+ date: () => new DateSchema,
2316
+ symbol: () => new SymbolSchema,
2317
+ nan: () => new NanSchema,
2318
+ int: () => new NumberSchema().int(),
2319
+ any: () => new AnySchema,
2320
+ unknown: () => new UnknownSchema,
2321
+ null: () => new NullSchema,
2322
+ undefined: () => new UndefinedSchema,
2323
+ void: () => new VoidSchema,
2324
+ never: () => new NeverSchema,
2325
+ object: (shape) => new ObjectSchema(shape),
2326
+ array: (itemSchema) => new ArraySchema(itemSchema),
2327
+ tuple: (items) => new TupleSchema(items),
2328
+ enum: (values) => new EnumSchema(values),
2329
+ literal: (value) => new LiteralSchema(value),
2330
+ union: (options) => new UnionSchema(options),
2331
+ discriminatedUnion: (discriminator, options) => new DiscriminatedUnionSchema(discriminator, options),
2332
+ intersection: (left, right) => new IntersectionSchema(left, right),
2333
+ record: (valueSchema) => new RecordSchema(valueSchema),
2334
+ map: (keySchema, valueSchema) => new MapSchema(keySchema, valueSchema),
2335
+ set: (valueSchema) => new SetSchema(valueSchema),
2336
+ file: () => new FileSchema,
2337
+ custom: (check, message) => new CustomSchema(check, message),
2338
+ instanceof: (cls) => new InstanceOfSchema(cls),
2339
+ lazy: (getter) => new LazySchema(getter),
2340
+ email: () => new EmailSchema,
2341
+ uuid: () => new UuidSchema,
2342
+ url: () => new UrlSchema,
2343
+ hostname: () => new HostnameSchema,
2344
+ ipv4: () => new Ipv4Schema,
2345
+ ipv6: () => new Ipv6Schema,
2346
+ base64: () => new Base64Schema,
2347
+ hex: () => new HexSchema,
2348
+ jwt: () => new JwtSchema,
2349
+ cuid: () => new CuidSchema,
2350
+ ulid: () => new UlidSchema,
2351
+ nanoid: () => new NanoidSchema,
2352
+ iso: {
2353
+ date: () => new IsoDateSchema,
2354
+ time: () => new IsoTimeSchema,
2355
+ datetime: () => new IsoDatetimeSchema,
2356
+ duration: () => new IsoDurationSchema
2357
+ },
2358
+ coerce: {
2359
+ string: () => new CoercedStringSchema,
2360
+ number: () => new CoercedNumberSchema,
2361
+ boolean: () => new CoercedBooleanSchema,
2362
+ bigint: () => new CoercedBigIntSchema,
2363
+ date: () => new CoercedDateSchema
2364
+ }
2365
+ };
2366
+ var schema = s;
2367
+ export {
2368
+ toJSONSchema,
2369
+ schema,
2370
+ s,
2371
+ preprocess,
2372
+ VoidSchema,
2373
+ UuidSchema,
2374
+ UrlSchema,
2375
+ UnknownSchema,
2376
+ UnionSchema,
2377
+ UndefinedSchema,
2378
+ UlidSchema,
2379
+ TupleSchema,
2380
+ TransformSchema,
2381
+ SymbolSchema,
2382
+ SuperRefinedSchema,
2383
+ StringSchema,
2384
+ SetSchema,
2385
+ SchemaType,
2386
+ SchemaRegistry,
2387
+ Schema,
2388
+ RefinedSchema,
2389
+ RefTracker,
2390
+ RecordSchema,
2391
+ ReadonlySchema,
2392
+ PipeSchema,
2393
+ ParseError,
2394
+ ParseContext,
2395
+ OptionalSchema,
2396
+ ObjectSchema,
2397
+ NumberSchema,
2398
+ NullableSchema,
2399
+ NullSchema,
2400
+ NeverSchema,
2401
+ NanoidSchema,
2402
+ NanSchema,
2403
+ MapSchema,
2404
+ LiteralSchema,
2405
+ LazySchema,
2406
+ JwtSchema,
2407
+ IsoTimeSchema,
2408
+ IsoDurationSchema,
2409
+ IsoDatetimeSchema,
2410
+ IsoDateSchema,
2411
+ Ipv6Schema,
2412
+ Ipv4Schema,
2413
+ IntersectionSchema,
2414
+ InstanceOfSchema,
2415
+ HostnameSchema,
2416
+ HexSchema,
2417
+ FileSchema,
2418
+ ErrorCode,
2419
+ EnumSchema,
2420
+ EmailSchema,
2421
+ DiscriminatedUnionSchema,
2422
+ DefaultSchema,
2423
+ DateSchema,
2424
+ CustomSchema,
2425
+ CuidSchema,
2426
+ CoercedStringSchema,
2427
+ CoercedNumberSchema,
2428
+ CoercedDateSchema,
2429
+ CoercedBooleanSchema,
2430
+ CoercedBigIntSchema,
2431
+ CatchSchema,
2432
+ BrandedSchema,
2433
+ BooleanSchema,
2434
+ BigIntSchema,
2435
+ Base64Schema,
2436
+ ArraySchema,
2437
+ AnySchema
2438
+ };