surreal-zod 0.0.0-alpha.1 → 0.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1605 @@
1
+ /** biome-ignore-all lint/suspicious/noExplicitAny: needed for conversion */
2
+ /** biome-ignore-all lint/style/noNonNullAssertion: needed for conversion */
3
+ import {
4
+ BoundQuery,
5
+ escapeIdent,
6
+ RecordId,
7
+ surql,
8
+ Table,
9
+ type RecordIdValue,
10
+ } from "surrealdb";
11
+ import * as core from "zod/v4/core";
12
+ import * as classic from "zod/v4";
13
+ import {
14
+ defineTable,
15
+ inferSurrealType,
16
+ tableToSurql,
17
+ type DefineTableOptions,
18
+ type RemoveTableOptions,
19
+ type TableInfo,
20
+ type TableStructure,
21
+ } from "../surql";
22
+
23
+ //////////////////////////////////////////////
24
+ //////////////////////////////////////////////
25
+ ////////// //////////
26
+ ////////// SurrealZodType //////////
27
+ ////////// //////////
28
+ //////////////////////////////////////////////
29
+ //////////////////////////////////////////////
30
+
31
+ export interface SurrealZodTypeDef extends core.$ZodTypeDef {
32
+ surrealType?:
33
+ | "any"
34
+ | "unknown"
35
+ | "never"
36
+ | "undefined"
37
+ | "boolean"
38
+ | "string"
39
+ | "number"
40
+ | "object"
41
+ | "record_id"
42
+ | "table";
43
+ }
44
+
45
+ export interface SurrealZodInternals {
46
+ type: string;
47
+ }
48
+
49
+ export interface SurrealZodTypeInternals<
50
+ out O = unknown,
51
+ out I = unknown,
52
+ out SurrealInternals extends SurrealZodInternals = SurrealZodInternals,
53
+ > extends core.$ZodTypeInternals<O, I> {
54
+ def: SurrealZodTypeDef & {
55
+ surreal: SurrealInternals;
56
+ };
57
+ }
58
+
59
+ export interface SurrealZodType<
60
+ out O = unknown,
61
+ out I = unknown,
62
+ out Internals extends SurrealZodTypeInternals<
63
+ O,
64
+ I,
65
+ SurrealZodInternals
66
+ > = SurrealZodTypeInternals<O, I, SurrealZodInternals>,
67
+ > extends core.$ZodType<O, I, Internals> {
68
+ // base
69
+ clone(def?: Internals["def"], params?: { parent: boolean }): this;
70
+
71
+ // parsing
72
+ parse(
73
+ data: unknown,
74
+ params?: core.ParseContext<core.$ZodIssue>,
75
+ ): core.output<this>;
76
+ safeParse(
77
+ data: unknown,
78
+ params?: core.ParseContext<core.$ZodIssue>,
79
+ ): classic.ZodSafeParseResult<core.output<this>>;
80
+
81
+ // wrappers
82
+ optional(): SurrealZodOptional<this>;
83
+ nonoptional(): SurrealZodNonOptional<this>;
84
+ nullable(): SurrealZodNullable<this>;
85
+ nullish(): SurrealZodOptional<SurrealZodNullable<this>>;
86
+ }
87
+
88
+ export interface _SurrealZodType<
89
+ Internals extends SurrealZodTypeInternals = SurrealZodTypeInternals,
90
+ > extends SurrealZodType<any, any, Internals> {}
91
+
92
+ export const SurrealZodType: core.$constructor<SurrealZodType> =
93
+ core.$constructor("SurrealZodType", (inst, def) => {
94
+ // @ts-expect-error - unknown assertion error
95
+ core.$ZodType.init(inst, def);
96
+ // Casting as _surreal.type is built while the schema is initialized
97
+ inst._zod.def.surreal ??= {} as SurrealZodInternals;
98
+
99
+ // base methods
100
+ inst.clone = (def, params) => core.clone(inst, def, params);
101
+
102
+ // parsing
103
+ inst.parse = (data, params) => {
104
+ return core.parse(inst, data, params);
105
+ };
106
+ // inst.safeParse = (data, params) => {
107
+ // return core.safeParse(inst, data, params);
108
+ // };
109
+
110
+ // wrappers
111
+ inst.optional = () => optional(inst);
112
+ inst.nonoptional = () => nonoptional(inst);
113
+ inst.nullable = () => nullable(inst);
114
+ inst.nullish = () => nullish(inst);
115
+
116
+ return inst;
117
+ });
118
+
119
+ /////////////////////////////////////////////
120
+ /////////////////////////////////////////////
121
+ ////////// //////////
122
+ ////////// SurrealZodAny //////////
123
+ ////////// //////////
124
+ /////////////////////////////////////////////
125
+ /////////////////////////////////////////////
126
+
127
+ export interface SurrealZodAnyInternals extends core.$ZodAnyInternals {
128
+ def: core.$ZodAnyInternals["def"] & {
129
+ surreal: {
130
+ type: "any";
131
+ };
132
+ };
133
+ }
134
+
135
+ export interface SurrealZodAny
136
+ extends _SurrealZodType<SurrealZodAnyInternals> {}
137
+
138
+ export const SurrealZodAny: core.$constructor<SurrealZodAny> =
139
+ core.$constructor("SurrealZodAny", (inst, def) => {
140
+ // @ts-expect-error - unknown assertion error
141
+ core.$ZodAny.init(inst, def);
142
+ SurrealZodType.init(inst, def);
143
+
144
+ // surreal internals
145
+ inst._zod.def.surreal.type = "any";
146
+ });
147
+
148
+ export function any(): SurrealZodAny {
149
+ return core._any(SurrealZodAny);
150
+ }
151
+
152
+ /////////////////////////////////////////////////
153
+ /////////////////////////////////////////////////
154
+ ////////// //////////
155
+ ////////// SurrealZodUnknown //////////
156
+ ////////// //////////
157
+ /////////////////////////////////////////////////
158
+ /////////////////////////////////////////////////
159
+
160
+ export interface SurrealZodUnknownInternals extends core.$ZodUnknownInternals {
161
+ def: core.$ZodUnknownInternals["def"] & {
162
+ surreal: {
163
+ type: "unknown";
164
+ };
165
+ };
166
+ }
167
+
168
+ export interface SurrealZodUnknown
169
+ extends _SurrealZodType<SurrealZodUnknownInternals> {}
170
+
171
+ export const SurrealZodUnknown: core.$constructor<SurrealZodUnknown> =
172
+ core.$constructor("SurrealZodUnknown", (inst, def) => {
173
+ // @ts-expect-error - unknown assertion error
174
+ core.$ZodUnknown.init(inst, def);
175
+ SurrealZodType.init(inst, def);
176
+
177
+ // surreal internals
178
+ inst._zod.def.surreal.type = "unknown";
179
+ });
180
+
181
+ export function unknown(): SurrealZodUnknown {
182
+ return core._unknown(SurrealZodUnknown);
183
+ }
184
+
185
+ ///////////////////////////////////////////////
186
+ ///////////////////////////////////////////////
187
+ ////////// //////////
188
+ ////////// SurrealZodNever //////////
189
+ ////////// //////////
190
+ ///////////////////////////////////////////////
191
+ ///////////////////////////////////////////////
192
+
193
+ export interface SurrealZodNeverInternals extends core.$ZodNeverInternals {
194
+ def: core.$ZodNeverInternals["def"] & {
195
+ surreal: {
196
+ type: "never";
197
+ };
198
+ };
199
+ }
200
+
201
+ export interface SurrealZodNever
202
+ extends _SurrealZodType<SurrealZodNeverInternals> {}
203
+
204
+ export const SurrealZodNever: core.$constructor<SurrealZodNever> =
205
+ core.$constructor("SurrealZodNever", (inst, def) => {
206
+ // @ts-expect-error - unknown assertion error
207
+ core.$ZodNever.init(inst, def);
208
+ SurrealZodType.init(inst, def);
209
+
210
+ // surreal internals
211
+ inst._zod.def.surreal.type = "never";
212
+ });
213
+
214
+ export function never(params?: string | core.$ZodNeverParams): SurrealZodNever {
215
+ return core._never(SurrealZodNever, params);
216
+ }
217
+
218
+ ///////////////////////////////////////////////////
219
+ ///////////////////////////////////////////////////
220
+ ////////// //////////
221
+ ////////// SurrealZodUndefined //////////
222
+ ////////// //////////
223
+ ///////////////////////////////////////////////////
224
+ ///////////////////////////////////////////////////
225
+
226
+ export interface SurrealZodUndefinedInternals
227
+ extends core.$ZodUndefinedInternals {
228
+ def: core.$ZodUndefinedInternals["def"] & {
229
+ surreal: {
230
+ type: "undefined";
231
+ };
232
+ };
233
+ }
234
+
235
+ export interface SurrealZodUndefined
236
+ extends _SurrealZodType<SurrealZodUndefinedInternals> {}
237
+
238
+ export const SurrealZodUndefined: core.$constructor<SurrealZodUndefined> =
239
+ core.$constructor("SurrealZodUndefined", (inst, def) => {
240
+ // @ts-expect-error - unknown assertion error
241
+ core.$ZodUndefined.init(inst, def);
242
+ SurrealZodType.init(inst, def);
243
+
244
+ // surreal internals
245
+ inst._zod.def.surreal.type = "undefined";
246
+ });
247
+
248
+ function _undefined(
249
+ params?: string | core.$ZodUndefinedParams,
250
+ ): SurrealZodUndefined {
251
+ return core._undefined(SurrealZodUndefined, params);
252
+ }
253
+ export { _undefined as undefined };
254
+
255
+ //////////////////////////////////////////////
256
+ //////////////////////////////////////////////
257
+ ////////// //////////
258
+ ////////// SurrealZodNull //////////
259
+ ////////// //////////
260
+ //////////////////////////////////////////////
261
+ //////////////////////////////////////////////
262
+
263
+ export interface SurrealZodNullInternals extends core.$ZodNullInternals {
264
+ def: core.$ZodNullInternals["def"] & {
265
+ surreal: {
266
+ type: "null";
267
+ };
268
+ };
269
+ }
270
+
271
+ export interface SurrealZodNull
272
+ extends _SurrealZodType<SurrealZodNullInternals> {}
273
+
274
+ export const SurrealZodNull: core.$constructor<SurrealZodNull> =
275
+ core.$constructor("SurrealZodNull", (inst, def) => {
276
+ // @ts-expect-error - unknown assertion error
277
+ core.$ZodNull.init(inst, def);
278
+ SurrealZodType.init(inst, def);
279
+
280
+ // surreal internals
281
+ inst._zod.def.surreal.type = "null";
282
+ });
283
+
284
+ function _null(params?: string | core.$ZodNullParams): SurrealZodNull {
285
+ return core._null(SurrealZodNull, params);
286
+ }
287
+ export { _null as null };
288
+
289
+ /////////////////////////////////////////////////
290
+ /////////////////////////////////////////////////
291
+ ////////// //////////
292
+ ////////// SurrealZodBoolean //////////
293
+ ////////// //////////
294
+ /////////////////////////////////////////////////
295
+ /////////////////////////////////////////////////
296
+
297
+ export interface SurrealZodBooleanInternals extends core.$ZodBooleanInternals {
298
+ def: core.$ZodBooleanInternals["def"] & {
299
+ surreal: {
300
+ type: "boolean";
301
+ };
302
+ };
303
+ }
304
+ export interface SurrealZodBoolean
305
+ extends _SurrealZodType<SurrealZodBooleanInternals> {}
306
+
307
+ export const SurrealZodBoolean: core.$constructor<SurrealZodBoolean> =
308
+ core.$constructor("SurrealZodBoolean", (inst, def) => {
309
+ // @ts-expect-error - unknown assertion error
310
+ core.$ZodBoolean.init(inst, def);
311
+ SurrealZodType.init(inst, def);
312
+
313
+ // surreal internals
314
+ inst._zod.def.surreal.type = "boolean";
315
+ });
316
+
317
+ export function boolean(
318
+ params?: string | core.$ZodBooleanParams,
319
+ ): SurrealZodBoolean {
320
+ return core._boolean(SurrealZodBoolean, params);
321
+ }
322
+
323
+ ////////////////////////////////////////////////
324
+ ////////////////////////////////////////////////
325
+ ////////// //////////
326
+ ////////// SurrealZodString //////////
327
+ ////////// //////////
328
+ ////////////////////////////////////////////////
329
+ ////////////////////////////////////////////////
330
+
331
+ export interface SurrealZodStringInternals
332
+ extends core.$ZodStringInternals<string> {
333
+ def: core.$ZodStringInternals<string>["def"] & {
334
+ surreal: {
335
+ type: "string";
336
+ };
337
+ };
338
+ }
339
+
340
+ export interface SurrealZodString
341
+ extends _SurrealZodType<SurrealZodStringInternals> {}
342
+
343
+ export const SurrealZodString: core.$constructor<SurrealZodString> =
344
+ core.$constructor("SurrealZodString", (inst, def) => {
345
+ // @ts-expect-error - unknown assertion error
346
+ core.$ZodString.init(inst, def);
347
+ SurrealZodType.init(inst, def);
348
+
349
+ // surreal internals
350
+ inst._zod.def.surreal.type = "string";
351
+ });
352
+
353
+ export function string(
354
+ params?: string | core.$ZodStringParams,
355
+ ): SurrealZodString {
356
+ return core._string(SurrealZodString, params);
357
+ }
358
+
359
+ ////////////////////////////////////////////////
360
+ ////////////////////////////////////////////////
361
+ ////////// //////////
362
+ ////////// SurrealZodNumber //////////
363
+ ////////// //////////
364
+ ////////////////////////////////////////////////
365
+ ////////////////////////////////////////////////
366
+
367
+ export interface SurrealZodNumberInternals extends core.$ZodNumberInternals {
368
+ def: core.$ZodNumberInternals["def"] & {
369
+ surreal: {
370
+ type: "number";
371
+ };
372
+ };
373
+ }
374
+
375
+ export interface SurrealZodNumber
376
+ extends _SurrealZodType<SurrealZodNumberInternals> {}
377
+
378
+ export const SurrealZodNumber: core.$constructor<SurrealZodNumber> =
379
+ core.$constructor("SurrealZodNumber", (inst, def) => {
380
+ // @ts-expect-error - unknown assertion error
381
+ core.$ZodNumber.init(inst, def);
382
+ SurrealZodType.init(inst, def);
383
+
384
+ // surreal internals
385
+ inst._zod.def.surreal.type = "number";
386
+ });
387
+
388
+ export function number(
389
+ params?: string | core.$ZodNumberParams,
390
+ ): SurrealZodNumber {
391
+ return core._number(SurrealZodNumber, params);
392
+ }
393
+
394
+ ////////////////////////////////////////////////
395
+ ////////////////////////////////////////////////
396
+ ////////// //////////
397
+ ////////// SurrealZodBigInt //////////
398
+ ////////// //////////
399
+ ////////////////////////////////////////////////
400
+ ////////////////////////////////////////////////
401
+
402
+ export interface SurrealZodBigIntInternals extends core.$ZodBigIntInternals {
403
+ def: core.$ZodBigIntInternals["def"] & {
404
+ surreal: {
405
+ type: "bigint";
406
+ };
407
+ };
408
+ }
409
+
410
+ export interface SurrealZodBigInt
411
+ extends _SurrealZodType<SurrealZodBigIntInternals> {}
412
+
413
+ export const SurrealZodBigInt: core.$constructor<SurrealZodBigInt> =
414
+ core.$constructor("SurrealZodBigInt", (inst, def) => {
415
+ // @ts-expect-error - unknown assertion error
416
+ core.$ZodBigInt.init(inst, def);
417
+ SurrealZodType.init(inst, def);
418
+
419
+ // surreal internals
420
+ inst._zod.def.surreal.type = "bigint";
421
+ });
422
+
423
+ export function bigint(
424
+ params?: string | core.$ZodBigIntParams,
425
+ ): SurrealZodBigInt {
426
+ return core._bigint(SurrealZodBigInt, params);
427
+ }
428
+
429
+ ////////////////////////////////////////////////
430
+ ////////////////////////////////////////////////
431
+ ////////// //////////
432
+ ////////// SurrealZodObject //////////
433
+ ////////// //////////
434
+ ////////////////////////////////////////////////
435
+ ////////////////////////////////////////////////
436
+
437
+ export interface SurrealZodObjectInternals<
438
+ // @ts-expect-error - unknown assertion error
439
+ out Shape extends core.$ZodShape = core.$ZodLooseShape,
440
+ out Config extends core.$ZodObjectConfig = core.$strip,
441
+ > extends core.$ZodObjectInternals<
442
+ Shape,
443
+ Config
444
+ > /*, core.$ZodObject<Shape, Config> */ {
445
+ def: core.$ZodObjectInternals<Shape, Config>["def"] & {
446
+ surreal: {
447
+ type: "object";
448
+ flexible: boolean;
449
+ };
450
+ };
451
+ }
452
+
453
+ export interface SurrealZodObject<
454
+ // @ts-expect-error - unknown assertion error
455
+ out Shape extends core.$ZodShape = core.$ZodLooseShape,
456
+ out Config extends core.$ZodObjectConfig = core.$strip,
457
+ > extends _SurrealZodType<SurrealZodObjectInternals<Shape, Config>> {
458
+ loose(): this;
459
+ /**
460
+ * @alias loose
461
+ */
462
+ flexible(): this;
463
+ strict(): this;
464
+
465
+ extend<U extends core.$ZodLooseShape>(
466
+ shape: U,
467
+ ): SurrealZodObject<core.util.Extend<Shape, U>, Config>;
468
+ safeExtend<U extends core.$ZodLooseShape>(
469
+ shape: classic.SafeExtendShape<Shape, U> &
470
+ Partial<Record<keyof Shape, core.SomeType>>,
471
+ ): SurrealZodObject<core.util.Extend<Shape, U>, Config>;
472
+ }
473
+
474
+ export const SurrealZodObject: core.$constructor<SurrealZodObject> =
475
+ core.$constructor("SurrealZodObject", (inst, def) => {
476
+ // TODO: Inline implementation and use core instead
477
+ // @ts-expect-error - unknown assertion error
478
+ core.$ZodObject.init(inst, def);
479
+ SurrealZodType.init(inst, def);
480
+
481
+ // surreal internals
482
+ inst._zod.def.surreal.type = "object";
483
+ // inst._zod.def.surreal.flexible = false;
484
+
485
+ inst.loose = () =>
486
+ inst.clone({
487
+ ...def,
488
+ catchall: unknown(),
489
+ });
490
+ inst.flexible = inst.loose;
491
+
492
+ inst.strict = () =>
493
+ inst.clone({
494
+ ...def,
495
+ catchall: never(),
496
+ });
497
+
498
+ inst.extend = (incoming: any) => core.util.extend(inst, incoming);
499
+ inst.safeExtend = (incoming: any) => core.util.safeExtend(inst, incoming);
500
+ });
501
+
502
+ export function object<
503
+ T extends core.$ZodLooseShape = Partial<Record<never, core.SomeType>>,
504
+ >(
505
+ shape?: T,
506
+ params?: string | core.$ZodObjectParams,
507
+ ): SurrealZodObject<core.util.Writeable<T>, core.$strip> {
508
+ const def: core.$ZodObjectDef = {
509
+ type: "object",
510
+ shape: shape ?? {},
511
+ ...core.util.normalizeParams(params),
512
+ };
513
+
514
+ return new SurrealZodObject({
515
+ ...def,
516
+ surreal: {
517
+ type: "object",
518
+ flexible: false,
519
+ },
520
+ }) as any;
521
+ }
522
+
523
+ //////////////////////////////////////////////////
524
+ //////////////////////////////////////////////////
525
+ ////////// //////////
526
+ ////////// SurrealZodRecordId //////////
527
+ ////////// //////////
528
+ //////////////////////////////////////////////////
529
+ //////////////////////////////////////////////////
530
+
531
+ export type SurrealZodRecordIdValue =
532
+ | SurrealZodAny
533
+ // | core.$ZodAny
534
+ | SurrealZodUnknown
535
+ // | core.$ZodUnknown
536
+ | SurrealZodString
537
+ // | core.$ZodString
538
+ | SurrealZodNumber
539
+ // | core.$ZodNumber
540
+ | SurrealZodBigInt
541
+ // | core.$ZodBigInt
542
+ | SurrealZodObject;
543
+ // | core.$ZodObject
544
+ // | SurrealZodArray
545
+ // | core.$ZodArray;
546
+
547
+ export interface SurrealZodRecordIdDef<
548
+ Table extends string = string,
549
+ Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue,
550
+ > extends SurrealZodTypeDef {
551
+ innerType: Id;
552
+ table?: Table[];
553
+
554
+ surreal: {
555
+ type: "record_id";
556
+ };
557
+ }
558
+
559
+ export type RecordIdValueOutput<Id extends SurrealZodRecordIdValue> =
560
+ Id extends {
561
+ _zod: {
562
+ output: any;
563
+ };
564
+ }
565
+ ? Id["_zod"]["output"]
566
+ : RecordIdValue;
567
+
568
+ export interface SurrealZodRecordIdInternals<
569
+ Table extends string = string,
570
+ Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue,
571
+ > extends SurrealZodTypeInternals<
572
+ RecordId<Table, RecordIdValueOutput<Id>>,
573
+ RecordIdValue
574
+ > {
575
+ def: SurrealZodRecordIdDef<Table, Id>;
576
+ }
577
+
578
+ export interface SurrealZodRecordId<
579
+ Table extends string = string,
580
+ Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue,
581
+ > extends _SurrealZodType<SurrealZodRecordIdInternals<Table, Id>> {
582
+ anytable(): SurrealZodRecordId<string, Id>;
583
+
584
+ table<NewTable extends string | string[]>(
585
+ table: NewTable,
586
+ ): SurrealZodRecordId<
587
+ NewTable extends string ? NewTable : NewTable[number],
588
+ Id
589
+ >;
590
+
591
+ type<NewType extends SurrealZodRecordIdValue>(
592
+ innerType: NewType,
593
+ ): SurrealZodRecordId<Table, NewType>;
594
+ }
595
+
596
+ function normalizeRecordIdDef(def: SurrealZodRecordIdDef) {
597
+ const invalidType = getInvalidRecordIdValueSchema(def.innerType);
598
+ if (invalidType) {
599
+ throw new Error(`${invalidType} is not valid as a RecordId's value`);
600
+ }
601
+
602
+ return {
603
+ ...def,
604
+ };
605
+ }
606
+
607
+ function getInvalidRecordIdValueSchema(schema: core.$ZodType) {
608
+ const def = schema._zod.def;
609
+ switch (def.type) {
610
+ case "any":
611
+ case "string":
612
+ case "number":
613
+ return "";
614
+ default:
615
+ return def.type;
616
+ }
617
+ }
618
+
619
+ export const SurrealZodRecordId: core.$constructor<SurrealZodRecordId> =
620
+ core.$constructor("SurrealZodRecordId", (inst, def) => {
621
+ SurrealZodType.init(inst, def);
622
+
623
+ // surreal internals
624
+ inst._zod.def.surreal.type = "record_id";
625
+ const normalized = normalizeRecordIdDef(def);
626
+
627
+ inst.anytable = () => {
628
+ return inst.clone({
629
+ ...def,
630
+ table: undefined,
631
+ }) as any;
632
+ };
633
+
634
+ inst.table = (table) => {
635
+ return inst.clone({
636
+ ...inst._zod.def,
637
+ table: Array.isArray(table) ? table : [table],
638
+ }) as any;
639
+ };
640
+
641
+ inst.type = (innerType) => {
642
+ return inst.clone({
643
+ ...inst._zod.def,
644
+ innerType,
645
+ }) as any;
646
+ };
647
+
648
+ inst._zod.parse = (payload, ctx) => {
649
+ if (payload.value instanceof RecordId) {
650
+ if (
651
+ normalized.table &&
652
+ !normalized.table.includes(payload.value.table.name)
653
+ ) {
654
+ payload.issues.push({
655
+ code: "invalid_value",
656
+ values: normalized.table,
657
+ input: payload.value.table.name,
658
+ message:
659
+ normalized.table.length > 1
660
+ ? `Expected RecordId's table to be one of ${normalized.table.map(escapeIdent).join(" | ")} but found ${payload.value.table.name}`
661
+ : `Expected RecordId's table to be ${normalized.table[0]} but found ${payload.value.table.name}`,
662
+ });
663
+ }
664
+
665
+ const schema = normalized.innerType._zod;
666
+ const result = schema.run({ value: payload.value.id, issues: [] }, ctx);
667
+
668
+ if (result instanceof Promise) {
669
+ return result.then((result) => {
670
+ if (result.issues.length) {
671
+ payload.issues.push(
672
+ ...core.util.prefixIssues("id", result.issues),
673
+ );
674
+ }
675
+ payload.value = new RecordId(
676
+ payload.value.table.name,
677
+ result.value as any,
678
+ );
679
+ return payload;
680
+ });
681
+ } else if (result.issues.length) {
682
+ payload.issues.push(...core.util.prefixIssues("id", result.issues));
683
+ }
684
+ payload.value = new RecordId(
685
+ payload.value.table.name,
686
+ result.value as any,
687
+ );
688
+ } else {
689
+ payload.issues.push({
690
+ code: "invalid_type",
691
+ // TODO: Surreal specific issues
692
+ expected: "custom",
693
+ input: payload.value,
694
+ });
695
+ }
696
+
697
+ return payload;
698
+ };
699
+
700
+ return inst;
701
+ });
702
+
703
+ export function recordId<const W extends string | string[]>(
704
+ what?: W,
705
+ innerType?: SurrealZodRecordIdValue,
706
+ ): SurrealZodRecordId<W extends string ? W : W[number]> {
707
+ return new SurrealZodRecordId({
708
+ // Zod would not be happy if we have a custom type here, so we use any
709
+ type: "any",
710
+ table: what ? (Array.isArray(what) ? what : [what]) : undefined,
711
+ innerType: innerType ?? any(),
712
+
713
+ surreal: {
714
+ type: "record_id",
715
+ },
716
+ }) as any;
717
+ }
718
+
719
+ export type inferRecordIdTable<T extends SurrealZodRecordId<string, any>> =
720
+ T extends SurrealZodRecordId<infer N> ? N : never;
721
+
722
+ ///////////////////////////////////////////////
723
+ ///////////////////////////////////////////////
724
+ ////////// //////////
725
+ ////////// SurrealZodTable //////////
726
+ ////////// //////////
727
+ ///////////////////////////////////////////////
728
+ ///////////////////////////////////////////////
729
+
730
+ export type SurrealZodTableFields = {
731
+ [key: string]: SurrealZodType;
732
+ };
733
+
734
+ /**
735
+ * Normalizes the fields of a table schema to include the id field if it is not present.
736
+ * If the id field is present, it will be normalized using the table name and the inner type.
737
+ */
738
+ type NormalizedIdField<
739
+ TableName extends string,
740
+ Fields extends SurrealZodTableFields,
741
+ FieldName extends string,
742
+ > = Fields extends {
743
+ [K in FieldName]: SurrealZodType;
744
+ }
745
+ ? Fields[FieldName] extends SurrealZodRecordId<infer _N, infer T>
746
+ ? Omit<Fields, FieldName> & {
747
+ [K in FieldName]: SurrealZodRecordId<TableName, T>;
748
+ }
749
+ : Fields[FieldName] extends SurrealZodRecordIdValue
750
+ ? Omit<Fields, FieldName> & {
751
+ [K in FieldName]: SurrealZodRecordId<TableName, Fields[FieldName]>;
752
+ }
753
+ : Omit<Fields, FieldName> & {
754
+ [K in FieldName]: SurrealZodRecordId<TableName>;
755
+ }
756
+ : Fields & {
757
+ [K in FieldName]: SurrealZodRecordId<TableName>;
758
+ };
759
+
760
+ export type NormalizedFields<
761
+ TableName extends string = string,
762
+ Fields extends SurrealZodTableFields = {},
763
+ > = core.util.Prettify<NormalizedIdField<TableName, Fields, "id">>;
764
+
765
+ type SetConfig<Key extends string, Value> = {
766
+ [key in Key]: Value;
767
+ };
768
+ type MergeConfig<
769
+ A extends Partial<SurrealZodTableConfig>,
770
+ B extends Partial<SurrealZodTableConfig>,
771
+ > = core.util.Prettify<Omit<A, keyof B> & B>;
772
+ type SurrealZodTableConfigSchemafull = SetConfig<"catchall", {}>;
773
+ type SurrealZodTableConfigSchemaless = SetConfig<
774
+ "catchall",
775
+ Record<string, SurrealZodType>
776
+ >;
777
+ type SurrealZodTableConfig = {
778
+ catchall: any;
779
+ dto: boolean;
780
+ };
781
+
782
+ export interface SurrealZodTableDef<
783
+ Name extends string = string,
784
+ Fields extends SurrealZodTableFields = {},
785
+ Config extends SurrealZodTableConfig = SurrealZodTableConfig,
786
+ > extends SurrealZodTypeDef {
787
+ name: Name;
788
+ fields: (Config["dto"] extends true
789
+ ? Omit<NormalizedFields<Name, Fields>, "id"> & {
790
+ id: SurrealZodOptional<NormalizedFields<Name, Fields>["id"]>;
791
+ }
792
+ : NormalizedFields<Name, Fields>) &
793
+ Config["catchall"];
794
+ catchall?: SurrealZodType;
795
+ dto: Config["dto"];
796
+
797
+ surreal: {
798
+ type: "table";
799
+ tableType: "any" | "normal" | "relation";
800
+ schemafull: boolean;
801
+ drop: boolean;
802
+ comment?: string;
803
+ };
804
+ }
805
+
806
+ export interface SurrealZodTableInternals<
807
+ Name extends string = string,
808
+ Fields extends SurrealZodTableFields = {},
809
+ Config extends SurrealZodTableConfig = MergeConfig<
810
+ SurrealZodTableConfig,
811
+ SurrealZodTableConfigSchemaless
812
+ >,
813
+ > extends SurrealZodTypeInternals {
814
+ def: SurrealZodTableDef<Name, Fields, Config>;
815
+ }
816
+
817
+ export interface SurrealZodTable<
818
+ Name extends string = string,
819
+ Fields extends SurrealZodTableFields = {},
820
+ Config extends SurrealZodTableConfig = MergeConfig<
821
+ SurrealZodTableConfig,
822
+ SurrealZodTableConfigSchemaless
823
+ >,
824
+ > extends _SurrealZodType<SurrealZodTableInternals<Name, Fields, Config>> {
825
+ // type specific, must be overriden in super types
826
+ name<NewName extends string>(name: NewName): SurrealZodTable<NewName, Fields>;
827
+ fields<NewFields extends SurrealZodTableFields>(
828
+ fields: NewFields,
829
+ ): SurrealZodTable<Name, NewFields, Config>;
830
+ schemafull(): SurrealZodTable<
831
+ Name,
832
+ Fields,
833
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
834
+ >;
835
+ schemaless(): SurrealZodTable<
836
+ Name,
837
+ Fields,
838
+ MergeConfig<Config, SurrealZodTableConfigSchemaless>
839
+ >;
840
+
841
+ // super type changing
842
+ any(): SurrealZodTable<Name, Fields, Config>;
843
+ normal(): SurrealZodTableNormal<Name, Fields, Config>;
844
+ relation(): SurrealZodTableRelation<
845
+ Name,
846
+ SurrealZodRecordId<string, SurrealZodRecordIdValue>,
847
+ SurrealZodRecordId<string, SurrealZodRecordIdValue>,
848
+ Fields,
849
+ Config
850
+ >;
851
+
852
+ drop(): this;
853
+ nodrop(): this;
854
+ comment(comment: string): this;
855
+
856
+ record(): this["_zod"]["def"]["fields"]["id"];
857
+ dto(): SurrealZodTable<
858
+ Name,
859
+ Fields,
860
+ MergeConfig<Config, SetConfig<"dto", true>>
861
+ >;
862
+ entity(): SurrealZodTable<
863
+ Name,
864
+ Fields,
865
+ MergeConfig<Config, SetConfig<"dto", false>>
866
+ >;
867
+
868
+ toSurql(
869
+ statement?: "define",
870
+ options?: DefineTableOptions,
871
+ ): BoundQuery<[undefined]>;
872
+ toSurql(
873
+ statement: "remove",
874
+ options?: RemoveTableOptions,
875
+ ): BoundQuery<[undefined]>;
876
+ toSurql(statement: "info"): BoundQuery<[TableInfo]>;
877
+ toSurql(statement: "structure"): BoundQuery<[TableStructure]>;
878
+ }
879
+
880
+ function handleFieldResult(
881
+ result: core.ParsePayload,
882
+ final: core.ParsePayload,
883
+ field: PropertyKey,
884
+ input: Record<PropertyKey, unknown>,
885
+ ) {
886
+ if (result.issues.length) {
887
+ final.issues.push(...core.util.prefixIssues(field, result.issues));
888
+ }
889
+
890
+ if (result.value === undefined) {
891
+ if (field in input) {
892
+ // @ts-expect-error: field not index-checked on final.value
893
+ final.value[field] = undefined;
894
+ }
895
+ } else {
896
+ // @ts-expect-error: field not index-checked on final.value
897
+ final.value[field] = result.value;
898
+ }
899
+ }
900
+
901
+ function handleCatchall(
902
+ promises: Promise<any>[],
903
+ input: Record<PropertyKey, unknown>,
904
+ payload: core.ParsePayload,
905
+ ctx: core.ParseContext,
906
+ def: ReturnType<typeof normalizeTableDef>,
907
+ inst: SurrealZodTable,
908
+ ) {
909
+ const unrecognized: string[] = [];
910
+ const known = def.fieldNamesSet;
911
+ const _catchall = def.catchall!._zod;
912
+ const type = _catchall.def.type;
913
+ for (const field in input) {
914
+ if (known.has(field)) continue;
915
+ if (type === "never") {
916
+ unrecognized.push(field);
917
+ continue;
918
+ }
919
+
920
+ const result = _catchall.run({ value: input[field], issues: [] }, ctx);
921
+ if (result instanceof Promise) {
922
+ promises.push(
923
+ result.then((result) =>
924
+ handleFieldResult(result, payload, field, input),
925
+ ),
926
+ );
927
+ } else {
928
+ handleFieldResult(result, payload, field, input);
929
+ }
930
+ }
931
+
932
+ if (unrecognized.length) {
933
+ payload.issues.push({
934
+ code: "unrecognized_keys",
935
+ keys: unrecognized,
936
+ input,
937
+ inst,
938
+ });
939
+ }
940
+
941
+ if (!promises.length) return payload;
942
+ return Promise.all(promises).then(() => payload);
943
+ }
944
+
945
+ function normalizeTableDef(def: SurrealZodTableDef) {
946
+ const fields: Record<string, SurrealZodType> = {};
947
+ const fieldNames = Object.keys(def.fields);
948
+
949
+ if (def.fields.id) {
950
+ if (def.fields.id instanceof SurrealZodRecordId) {
951
+ fields.id = def.fields.id.table(def.name);
952
+ } else {
953
+ fields.id = recordId(def.name).type(def.fields.id);
954
+ }
955
+ } else {
956
+ fields.id = recordId(def.name).type(any());
957
+ fieldNames.push("id");
958
+ }
959
+
960
+ if (def.dto && !(fields.id instanceof SurrealZodOptional)) {
961
+ fields.id = optional(fields.id!);
962
+ }
963
+
964
+ for (const field of fieldNames) {
965
+ if (field === "id") continue;
966
+ // if (!def.fields[field]?._zod.traits.has("SurrealZodType")) {
967
+ // throw new Error(
968
+ // `Invalid field definition for "${field}": expected a Surreal Zod schema`,
969
+ // );
970
+ // }
971
+ fields[field] = def.fields[field];
972
+ }
973
+
974
+ return {
975
+ ...def,
976
+ fields,
977
+ fieldNames,
978
+ fieldNamesSet: new Set(fieldNames),
979
+ };
980
+ }
981
+
982
+ export const SurrealZodTable: core.$constructor<SurrealZodTable> =
983
+ core.$constructor("SurrealZodTable", (inst, def) => {
984
+ SurrealZodType.init(inst, def);
985
+
986
+ const normalized = normalizeTableDef(def);
987
+ // @ts-expect-error - through normalization id is always present
988
+ inst._zod.def.fields = normalized.fields;
989
+ const catchall = normalized.catchall;
990
+
991
+ inst.name = (name) => {
992
+ return inst.clone({
993
+ ...inst._zod.def,
994
+ name,
995
+ }) as any;
996
+ };
997
+ inst.fields = (fields) => {
998
+ return inst.clone({
999
+ ...inst._zod.def,
1000
+ // @ts-expect-error - id may or may not be provided
1001
+ fields,
1002
+ }) as any;
1003
+ };
1004
+ inst.any = () => {
1005
+ return inst.clone({
1006
+ ...inst._zod.def,
1007
+ surreal: {
1008
+ ...inst._zod.def.surreal,
1009
+ tableType: "any",
1010
+ },
1011
+ });
1012
+ };
1013
+ inst.normal = () => {
1014
+ return new SurrealZodTableNormal({
1015
+ ...inst._zod.def,
1016
+ surreal: {
1017
+ ...inst._zod.def.surreal,
1018
+ tableType: "normal",
1019
+ },
1020
+ });
1021
+ };
1022
+ inst.relation = () => {
1023
+ // @ts-expect-error - id set in constructor
1024
+ return new SurrealZodTableRelation({
1025
+ ...inst._zod.def,
1026
+ // fields: {
1027
+ // in: recordId().type(any()),
1028
+ // out: recordId().type(any()),
1029
+ // ...def.fields,
1030
+ // },
1031
+ surreal: {
1032
+ ...inst._zod.def.surreal,
1033
+
1034
+ tableType: "relation",
1035
+ },
1036
+ }) as any;
1037
+ };
1038
+ inst.comment = (comment) => {
1039
+ return inst.clone({
1040
+ ...inst._zod.def,
1041
+ surreal: {
1042
+ ...inst._zod.def.surreal,
1043
+ comment,
1044
+ },
1045
+ });
1046
+ };
1047
+ inst.schemafull = () => {
1048
+ return inst.clone({
1049
+ ...inst._zod.def,
1050
+ catchall: never(),
1051
+ surreal: {
1052
+ ...inst._zod.def.surreal,
1053
+ schemafull: true,
1054
+ },
1055
+ });
1056
+ };
1057
+ inst.schemaless = () => {
1058
+ return inst.clone({
1059
+ ...inst._zod.def,
1060
+ catchall: unknown(),
1061
+ surreal: {
1062
+ ...inst._zod.def.surreal,
1063
+ schemafull: false,
1064
+ },
1065
+ });
1066
+ };
1067
+ inst.drop = () => {
1068
+ return inst.clone({
1069
+ ...inst._zod.def,
1070
+ surreal: {
1071
+ ...inst._zod.def.surreal,
1072
+ drop: true,
1073
+ },
1074
+ });
1075
+ };
1076
+ inst.nodrop = () => {
1077
+ return inst.clone({
1078
+ ...inst._zod.def,
1079
+ surreal: {
1080
+ ...inst._zod.def.surreal,
1081
+ drop: false,
1082
+ },
1083
+ });
1084
+ };
1085
+ // @ts-expect-error - through normalization id is always present
1086
+ inst.record = () => normalized.fields.id;
1087
+ inst.dto = () => {
1088
+ return inst.clone({
1089
+ ...inst._zod.def,
1090
+ dto: true,
1091
+ }) as any;
1092
+ };
1093
+ inst.entity = () => {
1094
+ let id: any = normalized.fields.id;
1095
+ while (id && id instanceof SurrealZodOptional) {
1096
+ id = id.unwrap();
1097
+ }
1098
+
1099
+ return inst.clone({
1100
+ ...inst._zod.def,
1101
+ dto: false,
1102
+ fields: {
1103
+ ...normalized.fields,
1104
+ id,
1105
+ },
1106
+ }) as any;
1107
+ };
1108
+ // @ts-expect-error - overloaded
1109
+ inst.toSurql = (statement = "define", options) =>
1110
+ // @ts-expect-error - overloaded
1111
+ tableToSurql(inst, statement, options);
1112
+
1113
+ inst._zod.parse = (payload, ctx) => {
1114
+ const input = payload.value;
1115
+
1116
+ if (!core.util.isObject(input)) {
1117
+ payload.issues.push({
1118
+ expected: "object",
1119
+ code: "invalid_type",
1120
+ input,
1121
+ inst,
1122
+ });
1123
+ return payload;
1124
+ }
1125
+
1126
+ payload.value = {};
1127
+ const promises: Promise<any>[] = [];
1128
+ const fields = normalized.fields;
1129
+
1130
+ for (const field of normalized.fieldNames) {
1131
+ const schema = fields[field]!;
1132
+ const result = schema._zod.run(
1133
+ { value: input[field], issues: [] },
1134
+ ctx,
1135
+ );
1136
+ if (result instanceof Promise) {
1137
+ promises.push(
1138
+ result.then((result) => {
1139
+ handleFieldResult(result, payload, field, input);
1140
+ }),
1141
+ );
1142
+ } else {
1143
+ handleFieldResult(result, payload, field, input);
1144
+ }
1145
+ }
1146
+
1147
+ if (!catchall) {
1148
+ return promises.length
1149
+ ? Promise.all(promises).then(() => payload)
1150
+ : payload;
1151
+ }
1152
+
1153
+ return handleCatchall(promises, input, payload, ctx, normalized, inst);
1154
+ };
1155
+
1156
+ return inst;
1157
+ });
1158
+
1159
+ export function table<Name extends string = string>(name: Name) {
1160
+ return new SurrealZodTable({
1161
+ type: "any",
1162
+ name,
1163
+ // @ts-expect-error - id set in constructor
1164
+ fields: {},
1165
+ catchall: unknown(),
1166
+
1167
+ surreal: {
1168
+ type: "table",
1169
+ tableType: "any",
1170
+ schemafull: false,
1171
+ drop: false,
1172
+ comment: undefined,
1173
+ },
1174
+ }) as SurrealZodTable<Name>;
1175
+ }
1176
+
1177
+ /////////////////////////////////////////////////////
1178
+ /////////////////////////////////////////////////////
1179
+ ////////// //////////
1180
+ ////////// SurrealZodTableNormal //////////
1181
+ ////////// //////////
1182
+ /////////////////////////////////////////////////////
1183
+ /////////////////////////////////////////////////////
1184
+
1185
+ export interface SurrealZodTableNormal<
1186
+ Name extends string = string,
1187
+ Fields extends SurrealZodTableFields = {},
1188
+ Config extends SurrealZodTableConfig = MergeConfig<
1189
+ SurrealZodTableConfig,
1190
+ SurrealZodTableConfigSchemaless
1191
+ >,
1192
+ > extends SurrealZodTable<Name, Fields, Config> {
1193
+ // override base methods
1194
+ name<NewName extends string>(
1195
+ name: NewName,
1196
+ ): SurrealZodTableNormal<NewName, Fields, Config>;
1197
+ fields<NewFields extends SurrealZodTableFields>(
1198
+ fields: NewFields,
1199
+ ): SurrealZodTableNormal<Name, NewFields, Config>;
1200
+ schemafull(): SurrealZodTableNormal<
1201
+ Name,
1202
+ Fields,
1203
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1204
+ >;
1205
+ schemaless(): SurrealZodTableNormal<
1206
+ Name,
1207
+ Fields,
1208
+ MergeConfig<Config, SurrealZodTableConfigSchemaless>
1209
+ >;
1210
+
1211
+ dto(): SurrealZodTableNormal<
1212
+ Name,
1213
+ Fields,
1214
+ MergeConfig<Config, SetConfig<"dto", true>>
1215
+ >;
1216
+ entity(): SurrealZodTableNormal<
1217
+ Name,
1218
+ Fields,
1219
+ MergeConfig<Config, SetConfig<"dto", false>>
1220
+ >;
1221
+ }
1222
+
1223
+ export const SurrealZodTableNormal: core.$constructor<SurrealZodTableNormal> =
1224
+ core.$constructor("SurrealZodTableNormal", (inst, def) => {
1225
+ SurrealZodTable.init(inst, def);
1226
+ });
1227
+
1228
+ export function normalTable<Name extends string = string>(
1229
+ name: Name,
1230
+ ): SurrealZodTableNormal<Name> {
1231
+ return table(name).normal();
1232
+ }
1233
+
1234
+ ///////////////////////////////////////////////////////
1235
+ ///////////////////////////////////////////////////////
1236
+ ////////// //////////
1237
+ ////////// SurrealZodTableRelation //////////
1238
+ ////////// //////////
1239
+ ///////////////////////////////////////////////////////
1240
+ ///////////////////////////////////////////////////////
1241
+
1242
+ export interface SurrealZodTableRelation<
1243
+ Name extends string = string,
1244
+ From extends SurrealZodRecordId<
1245
+ string,
1246
+ SurrealZodRecordIdValue
1247
+ > = SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1248
+ To extends SurrealZodRecordId<
1249
+ string,
1250
+ SurrealZodRecordIdValue
1251
+ > = SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1252
+ Fields extends SurrealZodTableFields = {},
1253
+ Config extends SurrealZodTableConfig = MergeConfig<
1254
+ SurrealZodTableConfig,
1255
+ SurrealZodTableConfigSchemaless
1256
+ >,
1257
+ > extends SurrealZodTable<
1258
+ Name,
1259
+ NormalizedIdField<
1260
+ inferRecordIdTable<To>,
1261
+ NormalizedIdField<
1262
+ inferRecordIdTable<From>,
1263
+ Fields & {
1264
+ in: From;
1265
+ out: To;
1266
+ },
1267
+ "in"
1268
+ >,
1269
+ "out"
1270
+ >,
1271
+ Config
1272
+ > {
1273
+ name<NewName extends string>(
1274
+ name: NewName,
1275
+ ): SurrealZodTableRelation<NewName, From, To, Fields, Config>;
1276
+ fields<NewFields extends SurrealZodTableFields>(
1277
+ fields: NewFields,
1278
+ ): SurrealZodTableRelation<Name, From, To, NewFields, Config>;
1279
+ schemafull(): SurrealZodTableRelation<
1280
+ Name,
1281
+ From,
1282
+ To,
1283
+ Fields,
1284
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1285
+ >;
1286
+ schemaless(): SurrealZodTableRelation<
1287
+ Name,
1288
+ From,
1289
+ To,
1290
+ Fields,
1291
+ MergeConfig<Config, SurrealZodTableConfigSchemaless>
1292
+ >;
1293
+
1294
+ from<
1295
+ NewFrom extends
1296
+ | string
1297
+ | string[]
1298
+ | SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1299
+ >(
1300
+ from: NewFrom,
1301
+ ): SurrealZodTableRelation<
1302
+ Name extends string ? Name : Name[number],
1303
+ toRecordId<NewFrom>,
1304
+ To,
1305
+ Fields,
1306
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1307
+ >;
1308
+ to<
1309
+ NewTo extends
1310
+ | string
1311
+ | string[]
1312
+ | SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1313
+ >(
1314
+ to: NewTo,
1315
+ ): SurrealZodTableRelation<
1316
+ Name extends string ? Name : Name[number],
1317
+ From,
1318
+ toRecordId<NewTo>,
1319
+ Fields,
1320
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1321
+ >;
1322
+ in<
1323
+ NewFrom extends
1324
+ | string
1325
+ | string[]
1326
+ | SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1327
+ >(
1328
+ from: NewFrom,
1329
+ ): SurrealZodTableRelation<
1330
+ Name extends string ? Name : Name[number],
1331
+ toRecordId<NewFrom>,
1332
+ To,
1333
+ Fields,
1334
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1335
+ >;
1336
+ out<
1337
+ NewTo extends
1338
+ | string
1339
+ | string[]
1340
+ | SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1341
+ >(
1342
+ to: NewTo,
1343
+ ): SurrealZodTableRelation<
1344
+ Name extends string ? Name : Name[number],
1345
+ From,
1346
+ toRecordId<NewTo>,
1347
+ Fields,
1348
+ MergeConfig<Config, SurrealZodTableConfigSchemafull>
1349
+ >;
1350
+
1351
+ dto(): SurrealZodTableRelation<
1352
+ Name,
1353
+ From,
1354
+ To,
1355
+ Fields,
1356
+ MergeConfig<Config, SetConfig<"dto", true>>
1357
+ >;
1358
+ entity(): SurrealZodTableRelation<
1359
+ Name,
1360
+ From,
1361
+ To,
1362
+ Fields,
1363
+ MergeConfig<Config, SetConfig<"dto", false>>
1364
+ >;
1365
+ }
1366
+
1367
+ type toRecordId<
1368
+ T extends
1369
+ | string
1370
+ | string[]
1371
+ | SurrealZodRecordId<string, SurrealZodRecordIdValue>,
1372
+ > = T extends string
1373
+ ? T extends SurrealZodRecordId<infer N, infer I>
1374
+ ? SurrealZodRecordId<N, I>
1375
+ : SurrealZodRecordId<T>
1376
+ : T extends string[]
1377
+ ? SurrealZodRecordId<T[number]>
1378
+ : T extends SurrealZodRecordId<string, SurrealZodRecordIdValue>
1379
+ ? T
1380
+ : never;
1381
+
1382
+ export const SurrealZodTableRelation: core.$constructor<SurrealZodTableRelation> =
1383
+ core.$constructor("SurrealZodTableRelation", (inst, def) => {
1384
+ SurrealZodTable.init(inst, def);
1385
+
1386
+ inst.from = (from) => {
1387
+ return new SurrealZodTableRelation({
1388
+ ...def,
1389
+ fields: {
1390
+ ...def.fields,
1391
+ in: from instanceof SurrealZodRecordId ? from : recordId(from),
1392
+ },
1393
+ }) as any;
1394
+ };
1395
+ inst.to = (to) => {
1396
+ return new SurrealZodTableRelation({
1397
+ ...def,
1398
+ fields: {
1399
+ ...def.fields,
1400
+ out: to instanceof SurrealZodRecordId ? to : recordId(to),
1401
+ },
1402
+ }) as any;
1403
+ };
1404
+ inst.in = inst.from;
1405
+ inst.out = inst.to;
1406
+
1407
+ inst.fields = (fields) => {
1408
+ return new SurrealZodTableRelation({
1409
+ ...def,
1410
+ fields: {
1411
+ ...def.fields,
1412
+ ...fields,
1413
+ },
1414
+ }) as any;
1415
+ };
1416
+
1417
+ return inst;
1418
+ });
1419
+
1420
+ export function relationTable<Name extends string = string>(
1421
+ name: Name,
1422
+ ): SurrealZodTableRelation<Name> {
1423
+ return table(name).relation();
1424
+ }
1425
+
1426
+ //////////////////////////////////////////////////
1427
+ //////////////////////////////////////////////////
1428
+ ////////// //////////
1429
+ ////////// SurrealZodOptional //////////
1430
+ ////////// //////////
1431
+ //////////////////////////////////////////////////
1432
+ //////////////////////////////////////////////////
1433
+
1434
+ export interface SurrealZodOptionalDef<
1435
+ T extends SurrealZodType = SurrealZodType,
1436
+ > extends core.$ZodOptionalDef<T> {
1437
+ innerType: T;
1438
+
1439
+ surreal: {
1440
+ type: "optional";
1441
+ };
1442
+ }
1443
+
1444
+ export interface SurrealZodOptionalInternals<
1445
+ T extends SurrealZodType = SurrealZodType,
1446
+ > extends core.$ZodOptionalInternals<T> {
1447
+ def: SurrealZodOptionalDef<T>;
1448
+ }
1449
+
1450
+ export interface SurrealZodOptional<T extends SurrealZodType = SurrealZodType>
1451
+ extends _SurrealZodType<SurrealZodOptionalInternals<T>> {
1452
+ unwrap(): T;
1453
+ }
1454
+
1455
+ export const SurrealZodOptional: core.$constructor<SurrealZodOptional> =
1456
+ core.$constructor("SurrealZodOptional", (inst, def) => {
1457
+ // @ts-expect-error - unknown assertion error
1458
+ core.$ZodOptional.init(inst, def);
1459
+ SurrealZodType.init(inst, def);
1460
+
1461
+ inst.unwrap = () => {
1462
+ return inst._zod.def.innerType;
1463
+ };
1464
+ });
1465
+
1466
+ export function optional<T extends SurrealZodType = SurrealZodType>(
1467
+ innerType: T,
1468
+ ): SurrealZodOptional<T> {
1469
+ return new SurrealZodOptional({
1470
+ type: "optional",
1471
+ innerType,
1472
+
1473
+ surreal: {
1474
+ type: "optional",
1475
+ },
1476
+ }) as any;
1477
+ }
1478
+
1479
+ /////////////////////////////////////////////////////
1480
+ /////////////////////////////////////////////////////
1481
+ ////////// //////////
1482
+ ////////// SurrealZodNonOptional //////////
1483
+ ////////// //////////
1484
+ /////////////////////////////////////////////////////
1485
+ /////////////////////////////////////////////////////
1486
+
1487
+ export interface SurrealZodNonOptionalDef<
1488
+ T extends SurrealZodType = SurrealZodType,
1489
+ > extends core.$ZodNonOptionalDef<T> {
1490
+ innerType: T;
1491
+
1492
+ surreal: {
1493
+ type: "nonoptional";
1494
+ };
1495
+ }
1496
+
1497
+ export interface SurrealZodNonOptionalInternals<
1498
+ T extends SurrealZodType = SurrealZodType,
1499
+ > extends core.$ZodNonOptionalInternals<T> {
1500
+ def: SurrealZodNonOptionalDef<T>;
1501
+ }
1502
+
1503
+ export interface SurrealZodNonOptional<
1504
+ T extends SurrealZodType = SurrealZodType,
1505
+ > extends _SurrealZodType<SurrealZodNonOptionalInternals<T>> {}
1506
+
1507
+ export const SurrealZodNonOptional: core.$constructor<SurrealZodNonOptional> =
1508
+ core.$constructor("SurrealZodNonOptional", (inst, def) => {
1509
+ // @ts-expect-error - unknown assertion error
1510
+ core.$ZodNonOptional.init(inst, def);
1511
+ SurrealZodType.init(inst, def);
1512
+ });
1513
+
1514
+ export function nonoptional<T extends SurrealZodType = SurrealZodType>(
1515
+ innerType: T,
1516
+ ): SurrealZodNonOptional<T> {
1517
+ return new SurrealZodNonOptional({
1518
+ type: "nonoptional",
1519
+ innerType,
1520
+
1521
+ surreal: {
1522
+ type: "nonoptional",
1523
+ },
1524
+ }) as any;
1525
+ }
1526
+
1527
+ //////////////////////////////////////////////////
1528
+ //////////////////////////////////////////////////
1529
+ ////////// //////////
1530
+ ////////// SurrealZodNullable //////////
1531
+ ////////// //////////
1532
+ //////////////////////////////////////////////////
1533
+ //////////////////////////////////////////////////
1534
+
1535
+ export interface SurrealZodNullableDef<
1536
+ T extends SurrealZodType = SurrealZodType,
1537
+ > extends core.$ZodNullableDef<T> {
1538
+ innerType: T;
1539
+
1540
+ surreal: {
1541
+ type: "nullable";
1542
+ };
1543
+ }
1544
+
1545
+ export interface SurrealZodNullableInternals<
1546
+ T extends SurrealZodType = SurrealZodType,
1547
+ > extends core.$ZodNullableInternals<T> {
1548
+ def: SurrealZodNullableDef<T>;
1549
+ }
1550
+
1551
+ export interface SurrealZodNullable<T extends SurrealZodType = SurrealZodType>
1552
+ extends _SurrealZodType<SurrealZodNullableInternals<T>> {}
1553
+
1554
+ export const SurrealZodNullable: core.$constructor<SurrealZodNullable> =
1555
+ core.$constructor("SurrealZodNullable", (inst, def) => {
1556
+ // @ts-expect-error - unknown assertion error
1557
+ core.$ZodNullable.init(inst, def);
1558
+ SurrealZodType.init(inst, def);
1559
+ });
1560
+
1561
+ export function nullable<T extends SurrealZodType = SurrealZodType>(
1562
+ innerType: T,
1563
+ ): SurrealZodNullable<T> {
1564
+ return new SurrealZodNullable({
1565
+ type: "nullable",
1566
+ innerType,
1567
+
1568
+ surreal: {
1569
+ type: "nullable",
1570
+ },
1571
+ }) as any;
1572
+ }
1573
+
1574
+ /////////////////////////////////////////////////
1575
+ /////////////////////////////////////////////////
1576
+ ////////// //////////
1577
+ ////////// SurrealZodNullish //////////
1578
+ ////////// //////////
1579
+ /////////////////////////////////////////////////
1580
+ /////////////////////////////////////////////////
1581
+
1582
+ export function nullish<T extends SurrealZodType = SurrealZodType>(
1583
+ innerType: T,
1584
+ ): SurrealZodOptional<SurrealZodNullable<T>> {
1585
+ return optional(nullable(innerType));
1586
+ }
1587
+
1588
+ export type SurrealZodTypes =
1589
+ | SurrealZodAny
1590
+ | SurrealZodUnknown
1591
+ | SurrealZodNever
1592
+ | SurrealZodUndefined
1593
+ | SurrealZodOptional
1594
+ | SurrealZodNonOptional
1595
+ | SurrealZodNull
1596
+ | SurrealZodNullable
1597
+ | SurrealZodBoolean
1598
+ | SurrealZodString
1599
+ | SurrealZodNumber
1600
+ | SurrealZodBigInt
1601
+ | SurrealZodObject
1602
+
1603
+ // Surreal Specific Types
1604
+ | SurrealZodRecordId
1605
+ | SurrealZodTable;