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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/zod/schema.js CHANGED
@@ -1,180 +1,2013 @@
1
- /** biome-ignore-all lint/suspicious/noExplicitAny: needed for conversion */
2
- /** biome-ignore-all lint/style/noNonNullAssertion: needed for conversion */
3
- import { BoundQuery, escapeIdent, RecordId, surql, Table, } from "surrealdb";
1
+ import { BoundQuery, DateTime, Duration, escapeIdent, RecordId, StringRecordId, surql, Uuid, Table, } from "surrealdb";
4
2
  import * as core from "zod/v4/core";
5
3
  import * as classic from "zod/v4";
6
- import { defineTable, inferSurrealType, tableToSurql, } from "../surql";
7
- export const SurrealZodType = core.$constructor("SurrealZodType", (inst, def) => {
8
- // @ts-expect-error - unknown assertion error
4
+ import { inferSurrealType, inlineQueryParameters, tableToSurql, } from "../surql";
5
+ import * as _core_ from "./core";
6
+ import { assignParsingMethods, } from "./parse";
7
+ import { allProcessors } from "./json-schema";
8
+ export const ZodSurrealType = core.$constructor("ZodSurrealType", (inst, def) => {
9
+ // @ts-expect-error
9
10
  core.$ZodType.init(inst, def);
10
- // Casting as _surreal.type is built while the schema is initialized
11
11
  inst._zod.def.surreal ??= {};
12
+ Object.assign(inst["~standard"], {
13
+ jsonSchema: {
14
+ input: core.createStandardJSONSchemaMethod(
15
+ // Not overriding json schema stuff
16
+ inst, "input"),
17
+ output: core.createStandardJSONSchemaMethod(
18
+ // Not overriding json schema stuff
19
+ inst, "output"),
20
+ },
21
+ });
22
+ inst.toJSONSchema = core.createToJSONSchemaMethod(inst, {});
12
23
  // base methods
13
- inst.clone = (def, params) => core.clone(inst, def, params);
14
- // parsing
15
- inst.parse = (data, params) => {
16
- return core.parse(inst, data, params);
24
+ inst.check = (...checks) => {
25
+ return inst.clone(core.util.mergeDefs(def, {
26
+ checks: [
27
+ ...(def.checks ?? []),
28
+ ...checks.map((ch) => typeof ch === "function"
29
+ ? {
30
+ _zod: { check: ch, def: { check: "custom" }, onattach: [] },
31
+ }
32
+ : ch),
33
+ ],
34
+ }), {
35
+ parent: true,
36
+ });
17
37
  };
18
- // inst.safeParse = (data, params) => {
19
- // return core.safeParse(inst, data, params);
20
- // };
38
+ inst.with = inst.check;
39
+ inst.clone = (def, params) => core.clone(inst, def, params);
40
+ inst.brand = () => inst;
41
+ inst.register = ((reg, meta) => {
42
+ reg.add(inst, meta);
43
+ return inst;
44
+ });
45
+ assignParsingMethods(inst);
21
46
  // wrappers
22
47
  inst.optional = () => optional(inst);
23
- inst.nonoptional = () => nonoptional(inst);
48
+ inst.exactOptional = () => exactOptional(inst);
24
49
  inst.nullable = () => nullable(inst);
25
- inst.nullish = () => nullish(inst);
50
+ inst.nullish = () => optional(nullable(inst));
51
+ inst.nonoptional = (params) => nonoptional(inst, params);
52
+ inst.array = () => array(inst);
53
+ inst.or = (arg) => union([inst, arg]);
54
+ inst.and = (arg) => intersection(inst, arg);
55
+ inst.transform = (tx) => pipe(inst, transform(tx));
56
+ inst.default = (def) => _default(inst, def);
57
+ inst.prefault = (def) => prefault(inst, def);
58
+ // inst.coalesce = (def, params) => coalesce(inst, def, params);
59
+ inst.catch = (params) => _catch(inst, params);
60
+ inst.pipe = (target) => pipe(inst, target);
61
+ inst.readonly = () => readonly(inst);
62
+ return inst;
63
+ });
64
+ export const ZodSurrealField = core.$constructor("ZodSurrealField", (inst, def) => {
65
+ // @ts-expect-error
66
+ core.$ZodType.init(inst, def);
67
+ def.surreal.field ??= {};
68
+ const isField = inst._zod.traits.size === 2;
69
+ if (def.surreal.field.default) {
70
+ inst._zod.optin = "optional";
71
+ inst._zod.optout = "optional";
72
+ inst._zod.dboptin = "optional";
73
+ }
74
+ assignParsingMethods(inst);
75
+ // ----------- Database Only Methods -----------
76
+ inst.$default = (value) => {
77
+ return new ZodSurrealField({
78
+ type: "field",
79
+ ...(isField ? inst._zod.def : {}),
80
+ innerType: isField ? inst._zod.def.innerType : inst,
81
+ surreal: {
82
+ ...(isField ? inst._zod.def.surreal : {}),
83
+ field: {
84
+ ...(isField ? inst._zod.def.surreal.field : {}),
85
+ default: {
86
+ value: value instanceof BoundQuery ? value : surql `${value}`,
87
+ always: false,
88
+ parse: false,
89
+ },
90
+ },
91
+ },
92
+ });
93
+ };
94
+ inst.$prefault = (value) => {
95
+ return new ZodSurrealField({
96
+ type: "field",
97
+ ...(isField ? inst._zod.def : {}),
98
+ innerType: isField ? inst._zod.def.innerType : inst,
99
+ surreal: {
100
+ ...(isField ? inst._zod.def.surreal : {}),
101
+ field: {
102
+ ...(isField ? inst._zod.def.surreal.field : {}),
103
+ default: {
104
+ value: value instanceof BoundQuery ? value : surql `${value}`,
105
+ always: false,
106
+ parse: true,
107
+ },
108
+ },
109
+ },
110
+ });
111
+ };
112
+ inst.$defaultAlways = (value) => {
113
+ return new ZodSurrealField({
114
+ type: "field",
115
+ ...(isField ? inst._zod.def : {}),
116
+ innerType: isField ? inst._zod.def.innerType : inst,
117
+ surreal: {
118
+ ...(isField ? inst._zod.def.surreal : {}),
119
+ field: {
120
+ ...(isField ? inst._zod.def.surreal.field : {}),
121
+ default: {
122
+ value: value instanceof BoundQuery ? value : surql `${value}`,
123
+ always: true,
124
+ },
125
+ },
126
+ },
127
+ });
128
+ };
129
+ inst.$prefaultAlways = (value) => {
130
+ return new ZodSurrealField({
131
+ type: "field",
132
+ ...(isField ? inst._zod.def : {}),
133
+ innerType: isField ? inst._zod.def.innerType : inst,
134
+ surreal: {
135
+ ...(isField ? inst._zod.def.surreal : {}),
136
+ field: {
137
+ ...(isField ? inst._zod.def.surreal.field : {}),
138
+ default: {
139
+ value: value instanceof BoundQuery ? value : surql `${value}`,
140
+ always: true,
141
+ parse: true,
142
+ },
143
+ },
144
+ },
145
+ });
146
+ };
147
+ inst.$readonly = (readonly = true) => {
148
+ return new ZodSurrealField({
149
+ type: "field",
150
+ ...(isField ? inst._zod.def : {}),
151
+ innerType: isField ? inst._zod.def.innerType : inst,
152
+ surreal: {
153
+ ...(isField ? inst._zod.def.surreal : {}),
154
+ field: {
155
+ ...inst._zod.def.surreal.field,
156
+ readonly,
157
+ },
158
+ },
159
+ });
160
+ };
161
+ inst.$comment = (comment) => {
162
+ return new ZodSurrealField({
163
+ type: "field",
164
+ ...(isField ? inst._zod.def : {}),
165
+ innerType: isField ? inst._zod.def.innerType : inst,
166
+ surreal: {
167
+ ...(isField ? inst._zod.def.surreal : {}),
168
+ field: {
169
+ ...(isField ? inst._zod.def.surreal.field : {}),
170
+ comment,
171
+ },
172
+ },
173
+ });
174
+ };
175
+ inst.$assert = (assert) => {
176
+ return new ZodSurrealField({
177
+ type: "field",
178
+ ...(isField ? inst._zod.def : {}),
179
+ innerType: isField ? inst._zod.def.innerType : inst,
180
+ surreal: {
181
+ ...(isField ? inst._zod.def.surreal : {}),
182
+ field: {
183
+ ...(isField ? inst._zod.def.surreal.field : {}),
184
+ assert,
185
+ },
186
+ },
187
+ });
188
+ };
189
+ inst.$value = (value) => {
190
+ return new ZodSurrealField({
191
+ type: "field",
192
+ ...(isField ? inst._zod.def : {}),
193
+ innerType: isField ? inst._zod.def.innerType : inst,
194
+ surreal: {
195
+ ...(isField ? inst._zod.def.surreal : {}),
196
+ field: {
197
+ ...(isField ? inst._zod.def.surreal.field : {}),
198
+ value,
199
+ },
200
+ },
201
+ });
202
+ };
203
+ inst.unwrap = () => inst._zod.def.innerType;
204
+ if (isField) {
205
+ inst._zod.parse = (payload, ctx) => {
206
+ if (ctx.direction === "backward") {
207
+ return def.innerType._zod.run(payload, ctx);
208
+ }
209
+ // .default/.prefault take precedence
210
+ if (def.innerType._zod.optin === "optional") {
211
+ const result = def.innerType._zod.run(payload, ctx);
212
+ if (result instanceof Promise)
213
+ return result.then((r) => {
214
+ if (r.issues.length && payload.value === undefined) {
215
+ return { issues: [], value: undefined };
216
+ }
217
+ return result;
218
+ });
219
+ if (result.issues.length && payload.value === undefined) {
220
+ return { issues: [], value: undefined };
221
+ }
222
+ return result;
223
+ }
224
+ // If database default is to be resolved
225
+ if (def.surreal.field.default && payload.value === undefined) {
226
+ if (!ctx.db) {
227
+ return payload;
228
+ }
229
+ return ctx.db
230
+ .query(`{ ${inlineQueryParameters(def.surreal.field.default.value)} }`)
231
+ .then(([result]) => {
232
+ payload.value = result;
233
+ // $prefault() does validation on the resolved value
234
+ if (def.surreal.field.default?.parse) {
235
+ return def.innerType._zod.run(payload, ctx);
236
+ }
237
+ /**
238
+ * $default() returns the default value immediately in forward direction.
239
+ * It doesn't pass the default value into the validator ("prefault").
240
+ * There's no reason to pass the default value through validation.
241
+ * The validity of the default is enforced by TypeScript statically.
242
+ * Otherwise, it's the responsibility of the user to ensure the default is valid.
243
+ * In the case of pipes with divergent in/out types, you can specify
244
+ * the default on the `in` schema of your ZodPipe to set a "prefault" for the pipe.
245
+ */
246
+ return payload;
247
+ });
248
+ }
249
+ return def.innerType._zod.run(payload, ctx);
250
+ };
251
+ }
252
+ return inst;
253
+ });
254
+ export const _ZodSurrealString = core.$constructor("_ZodSurrealString", (inst, def) => {
255
+ // @ts-expect-error
256
+ core.$ZodString.init(inst, def);
257
+ ZodSurrealType.init(inst, def);
258
+ ZodSurrealField.init(inst, def);
259
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.string(inst, ctx, json, params);
260
+ def.surreal.type ??= "string";
261
+ const bag = inst._zod.bag;
262
+ inst.format = bag.format ?? null;
263
+ inst.minLength = bag.minimum ?? null;
264
+ inst.maxLength = bag.maximum ?? null;
265
+ // validations
266
+ inst.regex = (...args) => inst.check(core._regex(...args));
267
+ inst.includes = (...args) => inst.check(core._includes(...args));
268
+ inst.startsWith = (...args) => inst.check(core._startsWith(...args));
269
+ inst.endsWith = (...args) => inst.check(core._endsWith(...args));
270
+ inst.min = (...args) => inst.check(core._minLength(...args));
271
+ inst.max = (...args) => inst.check(core._maxLength(...args));
272
+ inst.length = (...args) => inst.check(core._length(...args));
273
+ inst.nonempty = (...args) => inst.check(core._minLength(1, ...args));
274
+ inst.lowercase = (params) => inst.check(core._lowercase(params));
275
+ inst.uppercase = (params) => inst.check(core._uppercase(params));
276
+ // transforms
277
+ inst.trim = () => inst.check(core._trim());
278
+ inst.normalize = (...args) => inst.check(core._normalize(...args));
279
+ inst.toLowerCase = () => inst.check(core._toLowerCase());
280
+ inst.toUpperCase = () => inst.check(core._toUpperCase());
281
+ inst.slugify = () => inst.check(core._slugify());
282
+ return inst;
283
+ });
284
+ export const ZodSurrealString = core.$constructor("ZodSurrealString", (inst, def) => {
285
+ _ZodSurrealString.init(inst, def);
286
+ inst.email = (params) => inst.check(core._email(ZodSurrealEmail, params));
287
+ inst.url = (params) => inst.check(url(params));
288
+ inst.jwt = (params) => inst.check(jwt(params));
289
+ inst.emoji = (params) => inst.check(emoji(params));
290
+ inst.guid = (params) => inst.check(guid(params));
291
+ inst.uuid = (params) => inst.check(uuid(params));
292
+ inst.uuidv4 = (params) => inst.check(uuidv4(params));
293
+ inst.uuidv6 = (params) => inst.check(uuidv6(params));
294
+ inst.uuidv7 = (params) => inst.check(uuidv7(params));
295
+ inst.nanoid = (params) => inst.check(nanoid(params));
296
+ inst.guid = (params) => inst.check(guid(params));
297
+ inst.cuid = (params) => inst.check(cuid(params));
298
+ inst.cuid2 = (params) => inst.check(cuid2(params));
299
+ inst.ulid = (params) => inst.check(ulid(params));
300
+ inst.base64 = (params) => inst.check(base64(params));
301
+ inst.base64url = (params) => inst.check(base64url(params));
302
+ inst.xid = (params) => inst.check(xid(params));
303
+ inst.ksuid = (params) => inst.check(ksuid(params));
304
+ inst.ipv4 = (params) => inst.check(ipv4(params));
305
+ inst.ipv6 = (params) => inst.check(ipv6(params));
306
+ inst.cidrv4 = (params) => inst.check(cidrv4(params));
307
+ inst.cidrv6 = (params) => inst.check(cidrv6(params));
308
+ inst.e164 = (params) => inst.check(e164(params));
309
+ return inst;
310
+ });
311
+ export function string(params) {
312
+ return new ZodSurrealString({
313
+ type: "string",
314
+ ...core.util.normalizeParams(params),
315
+ surreal: { type: "string" },
316
+ });
317
+ }
318
+ export const ZodSurrealStringFormat = core.$constructor("ZodSurrealStringFormat", (inst, def) => {
319
+ // @ts-expect-error
320
+ core.$ZodStringFormat.init(inst, def);
321
+ _ZodSurrealString.init(inst, def);
322
+ });
323
+ export const ZodSurrealEmail = core.$constructor("ZodSurrealEmail", (inst, def) => {
324
+ // @ts-expect-error
325
+ core.$ZodEmail.init(inst, def);
326
+ ZodSurrealStringFormat.init(inst, def);
327
+ });
328
+ export function email(params) {
329
+ return new ZodSurrealEmail({
330
+ type: "string",
331
+ format: "email",
332
+ check: "string_format",
333
+ abort: false,
334
+ ...core.util.normalizeParams(params),
335
+ surreal: { type: "string" },
336
+ });
337
+ }
338
+ export const ZodSurrealGUID = core.$constructor("ZodSurrealGUID", (inst, def) => {
339
+ // @ts-expect-error
340
+ core.$ZodGUID.init(inst, def);
341
+ // @ts-expect-error
342
+ ZodSurrealStringFormat.init(inst, def);
343
+ const originalParse = inst._zod.parse;
344
+ inst._zod.parse = (payload, ctx) => {
345
+ if (payload.value instanceof Uuid) {
346
+ payload.value = payload.value.toString();
347
+ }
348
+ return originalParse(payload, ctx);
349
+ };
350
+ });
351
+ export function guid(params) {
352
+ return new ZodSurrealGUID({
353
+ type: "string",
354
+ format: "guid",
355
+ check: "string_format",
356
+ abort: false,
357
+ // TODO: Use surreal uuid type instead
358
+ ...core.util.normalizeParams(params),
359
+ surreal: { type: "string" },
360
+ });
361
+ }
362
+ export const ZodSurrealUUID = core.$constructor("ZodSurrealUUID", (inst, def) => {
363
+ // @ts-expect-error
364
+ core.$ZodUUID.init(inst, def);
365
+ // @ts-expect-error
366
+ ZodSurrealStringFormat.init(inst, def);
367
+ const originalParse = inst._zod.parse;
368
+ inst._zod.parse = (payload, ctx) => {
369
+ if (payload.value instanceof Uuid) {
370
+ payload.value = payload.value.toString();
371
+ }
372
+ return originalParse(payload, ctx);
373
+ };
374
+ });
375
+ export function uuid(params) {
376
+ return new ZodSurrealUUID({
377
+ type: "string",
378
+ format: "uuid",
379
+ check: "string_format",
380
+ abort: false,
381
+ // TODO: Use surreal uuid type instead
382
+ ...core.util.normalizeParams(params),
383
+ surreal: { type: "string" },
384
+ });
385
+ }
386
+ export function uuidv4(params) {
387
+ return new ZodSurrealUUID({
388
+ type: "string",
389
+ format: "uuid",
390
+ check: "string_format",
391
+ abort: false,
392
+ version: "v4",
393
+ // TODO: Use surreal uuid type instead
394
+ ...core.util.normalizeParams(params),
395
+ surreal: { type: "string" },
396
+ });
397
+ }
398
+ // ZodSurrealUUIDv6
399
+ export function uuidv6(params) {
400
+ return new ZodSurrealUUID({
401
+ type: "string",
402
+ format: "uuid",
403
+ check: "string_format",
404
+ abort: false,
405
+ version: "v6",
406
+ // TODO: Use surreal uuid type instead
407
+ ...core.util.normalizeParams(params),
408
+ surreal: { type: "string" },
409
+ });
410
+ }
411
+ // ZodSurrealUUIDv7
412
+ export function uuidv7(params) {
413
+ return new ZodSurrealUUID({
414
+ type: "string",
415
+ format: "uuid",
416
+ check: "string_format",
417
+ abort: false,
418
+ version: "v7",
419
+ // TODO: Use surreal uuid type instead
420
+ ...core.util.normalizeParams(params),
421
+ surreal: { type: "string" },
422
+ });
423
+ }
424
+ export const ZodSurrealURL = core.$constructor("ZodSurrealURL", (inst, def) => {
425
+ // @ts-expect-error
426
+ core.$ZodURL.init(inst, def);
427
+ // @ts-expect-error
428
+ ZodSurrealStringFormat.init(inst, def);
429
+ const originalParse = inst._zod.parse;
430
+ inst._zod.parse = (payload, ctx) => {
431
+ if (payload.value instanceof URL) {
432
+ payload.value = payload.value.toString();
433
+ }
434
+ return originalParse(payload, ctx);
435
+ };
436
+ });
437
+ export function url(params) {
438
+ return new ZodSurrealURL({
439
+ type: "string",
440
+ format: "url",
441
+ check: "string_format",
442
+ abort: false,
443
+ ...core.util.normalizeParams(params),
444
+ surreal: { type: "string" },
445
+ });
446
+ }
447
+ export const ZodSurrealEmoji = core.$constructor("ZodSurrealEmoji", (inst, def) => {
448
+ // @ts-expect-error
449
+ core.$ZodEmoji.init(inst, def);
450
+ ZodSurrealStringFormat.init(inst, def);
451
+ });
452
+ export function emoji(params) {
453
+ return new ZodSurrealEmoji({
454
+ type: "string",
455
+ format: "emoji",
456
+ check: "string_format",
457
+ abort: false,
458
+ ...core.util.normalizeParams(params),
459
+ surreal: { type: "string" },
460
+ });
461
+ }
462
+ export const ZodSurrealNanoID = core.$constructor("ZodSurrealNanoID", (inst, def) => {
463
+ // @ts-expect-error
464
+ core.$ZodNanoID.init(inst, def);
465
+ ZodSurrealStringFormat.init(inst, def);
466
+ });
467
+ export function nanoid(params) {
468
+ return new ZodSurrealNanoID({
469
+ type: "string",
470
+ format: "nanoid",
471
+ check: "string_format",
472
+ abort: false,
473
+ ...core.util.normalizeParams(params),
474
+ surreal: { type: "string" },
475
+ });
476
+ }
477
+ export const ZodSurrealCUID = core.$constructor("ZodSurrealCUID", (inst, def) => {
478
+ // @ts-expect-error
479
+ core.$ZodCUID.init(inst, def);
480
+ ZodSurrealStringFormat.init(inst, def);
481
+ });
482
+ export function cuid(params) {
483
+ return new ZodSurrealCUID({
484
+ type: "string",
485
+ format: "cuid",
486
+ check: "string_format",
487
+ abort: false,
488
+ ...core.util.normalizeParams(params),
489
+ surreal: { type: "string" },
490
+ });
491
+ }
492
+ export const ZodSurrealCUID2 = core.$constructor("ZodSurrealCUID2", (inst, def) => {
493
+ // @ts-expect-error
494
+ core.$ZodCUID2.init(inst, def);
495
+ ZodSurrealStringFormat.init(inst, def);
496
+ });
497
+ export function cuid2(params) {
498
+ return new ZodSurrealCUID2({
499
+ type: "string",
500
+ format: "cuid2",
501
+ check: "string_format",
502
+ abort: false,
503
+ ...core.util.normalizeParams(params),
504
+ surreal: { type: "string" },
505
+ });
506
+ }
507
+ export const ZodSurrealULID = core.$constructor("ZodSurrealULID", (inst, def) => {
508
+ // @ts-expect-error
509
+ core.$ZodULID.init(inst, def);
510
+ ZodSurrealStringFormat.init(inst, def);
511
+ });
512
+ export function ulid(params) {
513
+ return new ZodSurrealULID({
514
+ type: "string",
515
+ format: "ulid",
516
+ check: "string_format",
517
+ abort: false,
518
+ ...core.util.normalizeParams(params),
519
+ surreal: { type: "string" },
520
+ });
521
+ }
522
+ export const ZodSurrealXID = core.$constructor("ZodSurrealXID", (inst, def) => {
523
+ // @ts-expect-error
524
+ core.$ZodXID.init(inst, def);
525
+ ZodSurrealStringFormat.init(inst, def);
526
+ });
527
+ export function xid(params) {
528
+ return new ZodSurrealXID({
529
+ type: "string",
530
+ format: "xid",
531
+ check: "string_format",
532
+ abort: false,
533
+ ...core.util.normalizeParams(params),
534
+ surreal: { type: "string" },
535
+ });
536
+ }
537
+ export const ZodSurrealKSUID = core.$constructor("ZodSurrealKSUID", (inst, def) => {
538
+ // @ts-expect-error
539
+ core.$ZodKSUID.init(inst, def);
540
+ ZodSurrealStringFormat.init(inst, def);
541
+ });
542
+ export function ksuid(params) {
543
+ return new ZodSurrealKSUID({
544
+ type: "string",
545
+ format: "ksuid",
546
+ check: "string_format",
547
+ abort: false,
548
+ ...core.util.normalizeParams(params),
549
+ surreal: { type: "string" },
550
+ });
551
+ }
552
+ export const ZodSurrealIPv4 = core.$constructor("ZodSurrealIPv4", (inst, def) => {
553
+ // @ts-expect-error
554
+ core.$ZodIPv4.init(inst, def);
555
+ ZodSurrealStringFormat.init(inst, def);
556
+ });
557
+ export function ipv4(params) {
558
+ return new ZodSurrealIPv4({
559
+ type: "string",
560
+ format: "ipv4",
561
+ check: "string_format",
562
+ abort: false,
563
+ ...core.util.normalizeParams(params),
564
+ surreal: { type: "string" },
565
+ });
566
+ }
567
+ export const ZodSurrealMAC = core.$constructor("ZodSurrealMAC", (inst, def) => {
568
+ // @ts-expect-error
569
+ core.$ZodMAC.init(inst, def);
570
+ ZodSurrealStringFormat.init(inst, def);
571
+ });
572
+ export function mac(params) {
573
+ return new ZodSurrealMAC({
574
+ type: "string",
575
+ format: "mac",
576
+ check: "string_format",
577
+ abort: false,
578
+ ...core.util.normalizeParams(params),
579
+ surreal: { type: "string" },
580
+ });
581
+ }
582
+ export const ZodSurrealIPv6 = core.$constructor("ZodSurrealIPv6", (inst, def) => {
583
+ // @ts-expect-error
584
+ core.$ZodIPv6.init(inst, def);
585
+ ZodSurrealStringFormat.init(inst, def);
586
+ });
587
+ export function ipv6(params) {
588
+ return new ZodSurrealIPv6({
589
+ type: "string",
590
+ format: "ipv6",
591
+ check: "string_format",
592
+ abort: false,
593
+ ...core.util.normalizeParams(params),
594
+ surreal: { type: "string" },
595
+ });
596
+ }
597
+ export const ZodSurrealCIDRv4 = core.$constructor("ZodSurrealCIDRv4", (inst, def) => {
598
+ // @ts-expect-error
599
+ core.$ZodCIDRv4.init(inst, def);
600
+ ZodSurrealStringFormat.init(inst, def);
601
+ });
602
+ export function cidrv4(params) {
603
+ return new ZodSurrealCIDRv4({
604
+ type: "string",
605
+ format: "cidrv4",
606
+ check: "string_format",
607
+ abort: false,
608
+ ...core.util.normalizeParams(params),
609
+ surreal: { type: "string" },
610
+ });
611
+ }
612
+ export const ZodSurrealCIDRv6 = core.$constructor("ZodSurrealCIDRv6", (inst, def) => {
613
+ // @ts-expect-error
614
+ core.$ZodCIDRv6.init(inst, def);
615
+ ZodSurrealStringFormat.init(inst, def);
616
+ });
617
+ export function cidrv6(params) {
618
+ return new ZodSurrealCIDRv6({
619
+ type: "string",
620
+ format: "cidrv6",
621
+ check: "string_format",
622
+ abort: false,
623
+ ...core.util.normalizeParams(params),
624
+ surreal: { type: "string" },
625
+ });
626
+ }
627
+ export const ZodSurrealBase64 = core.$constructor("ZodSurrealBase64", (inst, def) => {
628
+ // @ts-expect-error
629
+ core.$ZodBase64.init(inst, def);
630
+ ZodSurrealStringFormat.init(inst, def);
631
+ });
632
+ export function base64(params) {
633
+ return new ZodSurrealBase64({
634
+ type: "string",
635
+ format: "base64",
636
+ check: "string_format",
637
+ abort: false,
638
+ ...core.util.normalizeParams(params),
639
+ surreal: { type: "string" },
640
+ });
641
+ }
642
+ export const ZodSurrealBase64URL = core.$constructor("ZodSurrealBase64URL", (inst, def) => {
643
+ // @ts-expect-error
644
+ core.$ZodBase64URL.init(inst, def);
645
+ ZodSurrealStringFormat.init(inst, def);
646
+ });
647
+ export function base64url(params) {
648
+ return new ZodSurrealBase64URL({
649
+ type: "string",
650
+ format: "base64url",
651
+ check: "string_format",
652
+ abort: false,
653
+ ...core.util.normalizeParams(params),
654
+ surreal: { type: "string" },
655
+ });
656
+ }
657
+ export const ZodSurrealE164 = core.$constructor("ZodSurrealE164", (inst, def) => {
658
+ // @ts-expect-error
659
+ core.$ZodE164.init(inst, def);
660
+ ZodSurrealStringFormat.init(inst, def);
661
+ });
662
+ export function e164(params) {
663
+ return new ZodSurrealE164({
664
+ type: "string",
665
+ format: "e164",
666
+ check: "string_format",
667
+ abort: false,
668
+ ...core.util.normalizeParams(params),
669
+ surreal: { type: "string" },
670
+ });
671
+ }
672
+ export const ZodSurrealJWT = core.$constructor("ZodSurrealJWT", (inst, def) => {
673
+ // @ts-expect-error
674
+ core.$ZodJWT.init(inst, def);
675
+ ZodSurrealStringFormat.init(inst, def);
676
+ });
677
+ export function jwt(params) {
678
+ return new ZodSurrealJWT({
679
+ type: "string",
680
+ format: "jwt",
681
+ check: "string_format",
682
+ abort: false,
683
+ ...core.util.normalizeParams(params),
684
+ surreal: { type: "string" },
685
+ });
686
+ }
687
+ export const ZodSurrealCustomStringFormat = core.$constructor("ZodSurrealCustomStringFormat", (inst, def) => {
688
+ // @ts-expect-error
689
+ core.$ZodCustomStringFormat.init(inst, def);
690
+ ZodSurrealStringFormat.init(inst, def);
691
+ });
692
+ export function stringFormat(format, fnOrRegex, _params = {}) {
693
+ const params = _core_.normalizeParams(_params, { type: "string" });
694
+ const def = {
695
+ ..._core_.normalizeParams(_params, { type: "string" }),
696
+ check: "string_format",
697
+ type: "string",
698
+ format,
699
+ fn: typeof fnOrRegex === "function"
700
+ ? fnOrRegex
701
+ : (val) => fnOrRegex.test(val),
702
+ ...params,
703
+ };
704
+ if (fnOrRegex instanceof RegExp) {
705
+ def.pattern = fnOrRegex;
706
+ }
707
+ const inst = new ZodSurrealCustomStringFormat(def);
26
708
  return inst;
709
+ }
710
+ export function hostname(_params) {
711
+ return stringFormat("hostname", core.regexes.hostname, _params);
712
+ }
713
+ export function hex(_params) {
714
+ return stringFormat("hex", core.regexes.hex, _params);
715
+ }
716
+ export function hash(alg, params) {
717
+ const enc = params?.enc ?? "hex";
718
+ const format = `${alg}_${enc}`;
719
+ const regex = core.regexes[format];
720
+ if (!regex)
721
+ throw new Error(`Unrecognized hash format: ${format}`);
722
+ return stringFormat(format, regex, params);
723
+ }
724
+ export const ZodSurrealNumber = core.$constructor("ZodSurrealNumber", (inst, def) => {
725
+ // @ts-expect-error
726
+ core.$ZodNumber.init(inst, def);
727
+ ZodSurrealType.init(inst, def);
728
+ ZodSurrealField.init(inst, def);
729
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.number(inst, ctx, json, params);
730
+ inst.gt = (value, params) => inst.check(core._gt(value, params));
731
+ inst.gte = (value, params) => inst.check(core._gte(value, params));
732
+ inst.min = (value, params) => inst.check(core._gte(value, params));
733
+ inst.lt = (value, params) => inst.check(core._lt(value, params));
734
+ inst.lte = (value, params) => inst.check(core._lte(value, params));
735
+ inst.max = (value, params) => inst.check(core._lte(value, params));
736
+ inst.int = (params) => inst.check(int(params));
737
+ inst.safe = (params) => inst.check(int(params));
738
+ inst.positive = (params) => inst.check(core._gt(0, params));
739
+ inst.nonnegative = (params) => inst.check(core._gte(0, params));
740
+ inst.negative = (params) => inst.check(core._lt(0, params));
741
+ inst.nonpositive = (params) => inst.check(core._lte(0, params));
742
+ inst.multipleOf = (value, params) => inst.check(core._multipleOf(value, params));
743
+ inst.step = (value, params) => inst.check(core._multipleOf(value, params));
744
+ // inst.finite = (params) => inst.check(core.finite(params));
745
+ inst.finite = () => inst;
746
+ def.surreal.type ??= "number";
747
+ const bag = inst._zod.bag;
748
+ inst.minValue =
749
+ Math.max(bag.minimum ?? Number.NEGATIVE_INFINITY, bag.exclusiveMinimum ?? Number.NEGATIVE_INFINITY) ?? null;
750
+ inst.maxValue =
751
+ Math.min(bag.maximum ?? Number.POSITIVE_INFINITY, bag.exclusiveMaximum ?? Number.POSITIVE_INFINITY) ?? null;
752
+ inst.isInt =
753
+ (bag.format ?? "").includes("int") ||
754
+ Number.isSafeInteger(bag.multipleOf ?? 0.5);
755
+ inst.isFinite = true;
756
+ inst.format = bag.format ?? null;
757
+ });
758
+ export function number(params) {
759
+ return new ZodSurrealNumber({
760
+ type: "number",
761
+ checks: [],
762
+ ...core.util.normalizeParams(params),
763
+ surreal: { type: "number" },
764
+ });
765
+ }
766
+ export const ZodSurrealNumberFormat = core.$constructor("ZodSurrealNumberFormat", (inst, def) => {
767
+ // @ts-expect-error
768
+ core.$ZodNumberFormat.init(inst, def);
769
+ ZodSurrealNumber.init(inst, def);
770
+ });
771
+ export function int(params) {
772
+ return new ZodSurrealNumberFormat({
773
+ type: "number",
774
+ check: "number_format",
775
+ abort: false,
776
+ format: "safeint",
777
+ ...core.util.normalizeParams(params),
778
+ surreal: { type: "int" },
779
+ });
780
+ }
781
+ export function float32(params) {
782
+ return new ZodSurrealNumberFormat({
783
+ type: "number",
784
+ check: "number_format",
785
+ abort: false,
786
+ format: "float32",
787
+ ...core.util.normalizeParams(params),
788
+ surreal: { type: "float" },
789
+ });
790
+ }
791
+ export function float64(params) {
792
+ return new ZodSurrealNumberFormat({
793
+ type: "number",
794
+ check: "number_format",
795
+ abort: false,
796
+ format: "float64",
797
+ ...core.util.normalizeParams(params),
798
+ surreal: { type: "float" },
799
+ });
800
+ }
801
+ export function int32(params) {
802
+ return new ZodSurrealNumberFormat({
803
+ type: "number",
804
+ check: "number_format",
805
+ abort: false,
806
+ format: "int32",
807
+ ...core.util.normalizeParams(params),
808
+ surreal: { type: "int" },
809
+ });
810
+ }
811
+ export function uint32(params) {
812
+ return new ZodSurrealNumberFormat({
813
+ type: "number",
814
+ check: "number_format",
815
+ abort: false,
816
+ format: "uint32",
817
+ ...core.util.normalizeParams(params),
818
+ surreal: { type: "int" },
819
+ });
820
+ }
821
+ export const ZodSurrealBoolean = core.$constructor("ZodBoolean", (inst, def) => {
822
+ // @ts-expect-error
823
+ core.$ZodBoolean.init(inst, def);
824
+ ZodSurrealType.init(inst, def);
825
+ ZodSurrealField.init(inst, def);
826
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.boolean(inst, ctx, json, params);
827
+ def.surreal.type ??= "bool";
828
+ });
829
+ export function boolean(params) {
830
+ return new ZodSurrealBoolean({
831
+ type: "boolean",
832
+ ...core.util.normalizeParams(params),
833
+ surreal: { type: "bool" },
834
+ });
835
+ }
836
+ export const ZodSurrealBigInt = core.$constructor("ZodSurrealBigInt", (inst, def) => {
837
+ // @ts-expect-error
838
+ core.$ZodBigInt.init(inst, def);
839
+ ZodSurrealType.init(inst, def);
840
+ // @ts-expect-error
841
+ ZodSurrealField.init(inst, def);
842
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.bigint(inst, ctx, json, params);
843
+ inst.gte = (value, params) => inst.check(core._gte(value, params));
844
+ inst.min = (value, params) => inst.check(core._gte(value, params));
845
+ inst.gt = (value, params) => inst.check(core._gt(value, params));
846
+ inst.gte = (value, params) => inst.check(core._gte(value, params));
847
+ inst.min = (value, params) => inst.check(core._gte(value, params));
848
+ inst.lt = (value, params) => inst.check(core._lt(value, params));
849
+ inst.lte = (value, params) => inst.check(core._lte(value, params));
850
+ inst.max = (value, params) => inst.check(core._lte(value, params));
851
+ inst.positive = (params) => inst.check(core._gt(BigInt(0), params));
852
+ inst.negative = (params) => inst.check(core._lt(BigInt(0), params));
853
+ inst.nonpositive = (params) => inst.check(core._lte(BigInt(0), params));
854
+ inst.nonnegative = (params) => inst.check(core._gte(BigInt(0), params));
855
+ inst.multipleOf = (value, params) => inst.check(core._multipleOf(value, params));
856
+ def.surreal.type ??= "int";
857
+ const bag = inst._zod.bag;
858
+ inst.minValue = bag.minimum ?? null;
859
+ inst.maxValue = bag.maximum ?? null;
860
+ inst.format = bag.format ?? null;
861
+ });
862
+ export function bigint(params) {
863
+ return new ZodSurrealBigInt({
864
+ type: "bigint",
865
+ ...core.util.normalizeParams(params),
866
+ surreal: { type: "int" },
867
+ });
868
+ }
869
+ export const ZodSurrealBigIntFormat = core.$constructor("ZodSurrealBigIntFormat", (inst, def) => {
870
+ // @ts-expect-error
871
+ core.$ZodBigIntFormat.init(inst, def);
872
+ ZodSurrealBigInt.init(inst, def);
873
+ });
874
+ // int64
875
+ export function int64(params) {
876
+ return new ZodSurrealBigIntFormat({
877
+ type: "bigint",
878
+ check: "bigint_format",
879
+ abort: false,
880
+ format: "int64",
881
+ ...core.util.normalizeParams(params),
882
+ surreal: { type: "int" },
883
+ });
884
+ }
885
+ // uint64
886
+ export function uint64(params) {
887
+ return new ZodSurrealBigIntFormat({
888
+ type: "bigint",
889
+ check: "bigint_format",
890
+ abort: false,
891
+ format: "uint64",
892
+ ...core.util.normalizeParams(params),
893
+ surreal: { type: "int" },
894
+ });
895
+ }
896
+ export const ZodSurrealSymbol = core.$constructor("ZodSurrealSymbol", (inst, def) => {
897
+ // @ts-expect-error
898
+ core.$ZodSymbol.init(inst, def);
899
+ ZodSurrealType.init(inst, def);
900
+ ZodSurrealField.init(inst, def);
901
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.symbol(inst, ctx, json, params);
902
+ });
903
+ export function symbol(params) {
904
+ return new ZodSurrealSymbol({
905
+ type: "symbol",
906
+ ...core.util.normalizeParams(params),
907
+ surreal: {},
908
+ });
909
+ }
910
+ export const ZodSurrealUndefined = core.$constructor("ZodSurrealUndefined", (inst, def) => {
911
+ // @ts-expect-error
912
+ core.$ZodUndefined.init(inst, def);
913
+ ZodSurrealType.init(inst, def);
914
+ ZodSurrealField.init(inst, def);
915
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.undefined(inst, ctx, json, params);
916
+ def.surreal.type ??= "none";
917
+ });
918
+ function _undefined(params) {
919
+ return new ZodSurrealUndefined({
920
+ type: "undefined",
921
+ ...core.util.normalizeParams(params),
922
+ surreal: { type: "none" },
923
+ });
924
+ }
925
+ export { _undefined as undefined };
926
+ export const ZodSurrealNull = core.$constructor("ZodSurrealNull", (inst, def) => {
927
+ // @ts-expect-error
928
+ core.$ZodNull.init(inst, def);
929
+ ZodSurrealType.init(inst, def);
930
+ ZodSurrealField.init(inst, def);
931
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.null(inst, ctx, json, params);
932
+ def.surreal.type ??= "null";
933
+ });
934
+ function _null(params) {
935
+ return new ZodSurrealNull({
936
+ type: "null",
937
+ ...core.util.normalizeParams(params),
938
+ surreal: { type: "null" },
939
+ });
940
+ }
941
+ export { _null as null };
942
+ export const ZodSurrealAny = core.$constructor("ZodSurrealAny", (inst, def) => {
943
+ // @ts-expect-error
944
+ core.$ZodAny.init(inst, def);
945
+ ZodSurrealType.init(inst, def);
946
+ ZodSurrealField.init(inst, def);
947
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.any(inst, ctx, json, params);
948
+ def.surreal.type ??= "any";
949
+ });
950
+ export function any() {
951
+ return new ZodSurrealAny({
952
+ type: "any",
953
+ surreal: { type: "any" },
954
+ });
955
+ }
956
+ export const ZodSurrealUnknown = core.$constructor("ZodSurrealUnknown", (inst, def) => {
957
+ // @ts-expect-error
958
+ core.$ZodUnknown.init(inst, def);
959
+ ZodSurrealType.init(inst, def);
960
+ ZodSurrealField.init(inst, def);
961
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.unknown(inst, ctx, json, params);
962
+ def.surreal.type ??= "any";
963
+ });
964
+ export function unknown() {
965
+ return new ZodSurrealUnknown({
966
+ type: "unknown",
967
+ surreal: { type: "any" },
968
+ });
969
+ }
970
+ export const ZodSurrealNever = core.$constructor("ZodSurrealNever", (inst, def) => {
971
+ // @ts-expect-error
972
+ core.$ZodNever.init(inst, def);
973
+ ZodSurrealType.init(inst, def);
974
+ ZodSurrealField.init(inst, def);
975
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.never(inst, ctx, json, params);
976
+ def.surreal.type = "none";
977
+ });
978
+ export function never(params) {
979
+ return new ZodSurrealNever({
980
+ type: "never",
981
+ ...core.util.normalizeParams(params),
982
+ surreal: { type: "none" },
983
+ });
984
+ }
985
+ export const ZodSurrealVoid = core.$constructor("ZodSurrealVoid", (inst, def) => {
986
+ // @ts-expect-error
987
+ core.$ZodVoid.init(inst, def);
988
+ ZodSurrealType.init(inst, def);
989
+ ZodSurrealField.init(inst, def);
990
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.void(inst, ctx, json, params);
991
+ def.surreal.type ??= "none";
992
+ });
993
+ function _void(params) {
994
+ return new ZodSurrealVoid({
995
+ type: "void",
996
+ ...core.util.normalizeParams(params),
997
+ surreal: { type: "none" },
998
+ });
999
+ }
1000
+ export { _void as void };
1001
+ export const ZodSurrealDate = core.$constructor("ZodSurrealDate", (inst, def) => {
1002
+ // @ts-expect-error
1003
+ core.$ZodDate.init(inst, def);
1004
+ ZodSurrealType.init(inst, def);
1005
+ // @ts-expect-error
1006
+ ZodSurrealField.init(inst, def);
1007
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.date(inst, ctx, json, params);
1008
+ inst.min = (value, params) => inst.check(core._gte(value instanceof DateTime ? value.toDate() : value, params));
1009
+ inst.max = (value, params) => inst.check(core._lte(value instanceof DateTime ? value.toDate() : value, params));
1010
+ def.surreal.type ??= "datetime";
1011
+ const c = inst._zod.bag;
1012
+ inst.minDate = c.minimum ? new Date(c.minimum) : null;
1013
+ inst.maxDate = c.maximum ? new Date(c.maximum) : null;
1014
+ const originalParse = inst._zod.parse;
1015
+ inst._zod.parse = (payload, ctx) => {
1016
+ if (payload.value instanceof DateTime) {
1017
+ payload.value = payload.value.toDate();
1018
+ }
1019
+ return originalParse(payload, ctx);
1020
+ };
1021
+ });
1022
+ function _date(params) {
1023
+ return new ZodSurrealDate({
1024
+ type: "date",
1025
+ ...core.util.normalizeParams(params),
1026
+ surreal: { type: "datetime" },
1027
+ });
1028
+ }
1029
+ export { _date as date };
1030
+ export const ZodSurrealArray = core.$constructor("ZodSurrealArray", (inst, def) => {
1031
+ // @ts-expect-error
1032
+ core.$ZodArray.init(inst, def);
1033
+ ZodSurrealType.init(inst, def);
1034
+ ZodSurrealField.init(inst, def);
1035
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.array(inst, ctx, json, params);
1036
+ inst.element = def.element;
1037
+ inst.min = (minLength, params) => inst.check(core._minLength(minLength, params));
1038
+ inst.nonempty = (params) => inst.check(core._minLength(1, params));
1039
+ inst.max = (maxLength, params) => inst.check(core._maxLength(maxLength, params));
1040
+ inst.length = (len, params) => inst.check(core._length(len, params));
1041
+ inst.unwrap = () => inst.element;
1042
+ });
1043
+ export function array(element, params) {
1044
+ return new ZodSurrealArray({
1045
+ type: "array",
1046
+ element,
1047
+ ...core.util.normalizeParams(params),
1048
+ surreal: {},
1049
+ });
1050
+ }
1051
+ // .keyof
1052
+ export function keyof(schema) {
1053
+ const shape = schema._zod.def.shape;
1054
+ return _enum(Object.keys(shape));
1055
+ }
1056
+ export const ZodSurrealObject = core.$constructor("ZodSurrealObject", (inst, def) => {
1057
+ // @ts-expect-error
1058
+ core.$ZodObjectJIT.init(inst, def);
1059
+ ZodSurrealType.init(inst, def);
1060
+ ZodSurrealField.init(inst, def);
1061
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.object(inst, ctx, json, params);
1062
+ core.util.defineLazy(inst, "shape", () => {
1063
+ return def.shape;
1064
+ });
1065
+ inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
1066
+ inst.catchall = (catchall) => inst.clone({
1067
+ ...inst._zod.def,
1068
+ catchall: catchall,
1069
+ });
1070
+ inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
1071
+ inst.loose = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
1072
+ inst.strict = () => inst.clone({ ...inst._zod.def, catchall: never() });
1073
+ inst.strip = () => inst.clone({ ...inst._zod.def, catchall: undefined });
1074
+ inst.extend = (incoming) => {
1075
+ return core.util.extend(inst, incoming);
1076
+ };
1077
+ inst.safeExtend = (incoming) => {
1078
+ return core.util.safeExtend(inst, incoming);
1079
+ };
1080
+ inst.merge = (other) => core.util.merge(inst, other);
1081
+ inst.pick = (mask) => core.util.pick(inst, mask);
1082
+ inst.omit = (mask) => core.util.omit(inst, mask);
1083
+ inst.partial = (...args) => core.util.partial(ZodSurrealOptional, inst, args[0]);
1084
+ inst.required = (...args) => core.util.required(ZodSurrealNonOptional, inst, args[0]);
1085
+ });
1086
+ export function object(shape, params) {
1087
+ const def = {
1088
+ type: "object",
1089
+ shape: shape ?? {},
1090
+ ...core.util.normalizeParams(params),
1091
+ surreal: {},
1092
+ };
1093
+ return new ZodSurrealObject(def);
1094
+ }
1095
+ // strictObject
1096
+ export function strictObject(shape, params) {
1097
+ return new ZodSurrealObject({
1098
+ type: "object",
1099
+ shape,
1100
+ catchall: never(),
1101
+ ...core.util.normalizeParams(params),
1102
+ surreal: {},
1103
+ });
1104
+ }
1105
+ // looseObject
1106
+ export function looseObject(shape, params) {
1107
+ return new ZodSurrealObject({
1108
+ type: "object",
1109
+ shape,
1110
+ catchall: unknown(),
1111
+ ...core.util.normalizeParams(params),
1112
+ surreal: {},
1113
+ });
1114
+ }
1115
+ export const ZodSurrealUnion = core.$constructor("ZodSurrealUnion", (inst, def) => {
1116
+ // @ts-expect-error
1117
+ core.$ZodUnion.init(inst, def);
1118
+ ZodSurrealType.init(inst, def);
1119
+ ZodSurrealField.init(inst, def);
1120
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.union(inst, ctx, json, params);
1121
+ inst.options = def.options;
1122
+ });
1123
+ export function union(options, params) {
1124
+ return new ZodSurrealUnion({
1125
+ type: "union",
1126
+ options: options,
1127
+ ...core.util.normalizeParams(params),
1128
+ surreal: {},
1129
+ });
1130
+ }
1131
+ export const ZodSurrealXor =
1132
+ /*@__PURE__*/ core.$constructor("ZodSurrealXor", (inst, def) => {
1133
+ ZodSurrealUnion.init(inst, def);
1134
+ // @ts-expect-error
1135
+ core.$ZodXor.init(inst, def);
1136
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.union(inst, ctx, json, params);
1137
+ inst.options = def.options;
1138
+ });
1139
+ /** Creates an exclusive union (XOR) where exactly one option must match.
1140
+ * Unlike regular unions that succeed when any option matches, xor fails if
1141
+ * zero or more than one option matches the input. */
1142
+ export function xor(options, params) {
1143
+ return new ZodSurrealXor({
1144
+ type: "union",
1145
+ options: options,
1146
+ inclusive: false,
1147
+ ...core.util.normalizeParams(params),
1148
+ surreal: {},
1149
+ });
1150
+ }
1151
+ export const ZodSurrealDiscriminatedUnion = core.$constructor("ZodSurrealDiscriminatedUnion", (inst, def) => {
1152
+ ZodSurrealUnion.init(inst, def);
1153
+ // @ts-expect-error
1154
+ core.$ZodDiscriminatedUnion.init(inst, def);
1155
+ });
1156
+ export function discriminatedUnion(discriminator, options, params) {
1157
+ // const [options, params] = args;
1158
+ return new ZodSurrealDiscriminatedUnion({
1159
+ type: "union",
1160
+ options,
1161
+ discriminator,
1162
+ ...core.util.normalizeParams(params),
1163
+ surreal: {},
1164
+ });
1165
+ }
1166
+ export const ZodIntersection =
1167
+ /*@__PURE__*/ core.$constructor("ZodIntersection", (inst, def) => {
1168
+ // @ts-expect-error
1169
+ core.$ZodIntersection.init(inst, def);
1170
+ ZodSurrealType.init(inst, def);
1171
+ ZodSurrealField.init(inst, def);
1172
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.intersection(inst, ctx, json, params);
1173
+ });
1174
+ export function intersection(left, right) {
1175
+ return new ZodIntersection({
1176
+ type: "intersection",
1177
+ left: left,
1178
+ right: right,
1179
+ surreal: {},
1180
+ });
1181
+ }
1182
+ export const ZodSurrealTuple = core.$constructor("ZodSurrealTuple", (inst, def) => {
1183
+ // @ts-expect-error
1184
+ core.$ZodTuple.init(inst, def);
1185
+ ZodSurrealType.init(inst, def);
1186
+ ZodSurrealField.init(inst, def);
1187
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.tuple(inst, ctx, json, params);
1188
+ inst.rest = (rest) => inst.clone({
1189
+ ...inst._zod.def,
1190
+ rest: rest,
1191
+ });
1192
+ });
1193
+ export function tuple(items, _paramsOrRest, _params) {
1194
+ const hasRest = _paramsOrRest instanceof ZodSurrealType;
1195
+ const params = hasRest ? _params : _paramsOrRest;
1196
+ const rest = hasRest ? _paramsOrRest : null;
1197
+ return new ZodSurrealTuple({
1198
+ type: "tuple",
1199
+ items: items,
1200
+ rest,
1201
+ ...core.util.normalizeParams(params),
1202
+ surreal: {},
1203
+ });
1204
+ }
1205
+ export const ZodSurrealRecord = core.$constructor("ZodSurrealRecord", (inst, def) => {
1206
+ // @ts-expect-error
1207
+ core.$ZodRecord.init(inst, def);
1208
+ ZodSurrealType.init(inst, def);
1209
+ ZodSurrealField.init(inst, def);
1210
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.record(inst, ctx, json, params);
1211
+ inst.keyType = def.keyType;
1212
+ inst.valueType = def.valueType;
1213
+ });
1214
+ export function record(keyType, valueType, params) {
1215
+ return new ZodSurrealRecord({
1216
+ type: "record",
1217
+ keyType,
1218
+ valueType: valueType,
1219
+ ...core.util.normalizeParams(params),
1220
+ surreal: {},
1221
+ });
1222
+ }
1223
+ export function partialRecord(keyType, valueType, params) {
1224
+ const k = core.clone(keyType);
1225
+ k._zod.values = undefined;
1226
+ return new ZodSurrealRecord({
1227
+ type: "record",
1228
+ keyType: k,
1229
+ valueType: valueType,
1230
+ ...core.util.normalizeParams(params),
1231
+ surreal: {},
1232
+ });
1233
+ }
1234
+ export function looseRecord(keyType, valueType, params) {
1235
+ return new ZodSurrealRecord({
1236
+ type: "record",
1237
+ keyType,
1238
+ valueType: valueType,
1239
+ mode: "loose",
1240
+ ...core.util.normalizeParams(params),
1241
+ surreal: {},
1242
+ });
1243
+ }
1244
+ export const ZodSurrealMap = core.$constructor("ZodSurrealMap", (inst, def) => {
1245
+ // @ts-expect-error
1246
+ core.$ZodMap.init(inst, def);
1247
+ ZodSurrealType.init(inst, def);
1248
+ ZodSurrealField.init(inst, def);
1249
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.map(inst, ctx, json, params);
1250
+ inst.keyType = def.keyType;
1251
+ inst.valueType = def.valueType;
1252
+ inst.min = (...args) => inst.check(core._minSize(...args));
1253
+ inst.nonempty = (params) => inst.check(core._minSize(1, params));
1254
+ inst.max = (...args) => inst.check(core._maxSize(...args));
1255
+ inst.size = (...args) => inst.check(core._size(...args));
1256
+ });
1257
+ export function map(keyType, valueType, params) {
1258
+ return new ZodSurrealMap({
1259
+ type: "map",
1260
+ keyType: keyType,
1261
+ valueType: valueType,
1262
+ ...core.util.normalizeParams(params),
1263
+ surreal: {},
1264
+ });
1265
+ }
1266
+ export const ZodSurrealSet = core.$constructor("ZodSurrealSet", (inst, def) => {
1267
+ // @ts-expect-error
1268
+ core.$ZodSet.init(inst, def);
1269
+ ZodSurrealType.init(inst, def);
1270
+ ZodSurrealField.init(inst, def);
1271
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.set(inst, ctx, json, params);
1272
+ inst.min = (...args) => inst.check(core._minSize(...args));
1273
+ inst.nonempty = (params) => inst.check(core._minSize(1, params));
1274
+ inst.max = (...args) => inst.check(core._maxSize(...args));
1275
+ inst.size = (...args) => inst.check(core._size(...args));
1276
+ });
1277
+ export function set(valueType, params) {
1278
+ return new ZodSurrealSet({
1279
+ type: "set",
1280
+ valueType: valueType,
1281
+ ...core.util.normalizeParams(params),
1282
+ surreal: {},
1283
+ });
1284
+ }
1285
+ export const ZodSurrealEnum =
1286
+ /*@__PURE__*/ core.$constructor("ZodEnum", (inst, def) => {
1287
+ // @ts-expect-error
1288
+ core.$ZodEnum.init(inst, def);
1289
+ ZodSurrealType.init(inst, def);
1290
+ ZodSurrealField.init(inst, def);
1291
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.enum(inst, ctx, json, params);
1292
+ inst.enum = def.entries;
1293
+ inst.options = Object.values(def.entries);
1294
+ const keys = new Set(Object.keys(def.entries));
1295
+ inst.extract = (values, params) => {
1296
+ const newEntries = {};
1297
+ for (const value of values) {
1298
+ if (keys.has(value)) {
1299
+ newEntries[value] = def.entries[value];
1300
+ }
1301
+ else
1302
+ throw new Error(`Key ${value} not found in enum`);
1303
+ }
1304
+ return new ZodSurrealEnum({
1305
+ ...def,
1306
+ checks: [],
1307
+ ...core.util.normalizeParams(params),
1308
+ entries: newEntries,
1309
+ });
1310
+ };
1311
+ inst.exclude = (values, params) => {
1312
+ const newEntries = { ...def.entries };
1313
+ for (const value of values) {
1314
+ if (keys.has(value)) {
1315
+ delete newEntries[value];
1316
+ }
1317
+ else
1318
+ throw new Error(`Key ${value} not found in enum`);
1319
+ }
1320
+ return new ZodSurrealEnum({
1321
+ ...def,
1322
+ checks: [],
1323
+ ...core.util.normalizeParams(params),
1324
+ entries: newEntries,
1325
+ });
1326
+ };
1327
+ });
1328
+ function _enum(values, params) {
1329
+ const entries = Array.isArray(values)
1330
+ ? Object.fromEntries(values.map((v) => [v, v]))
1331
+ : values;
1332
+ return new ZodSurrealEnum({
1333
+ type: "enum",
1334
+ entries,
1335
+ ...core.util.normalizeParams(params),
1336
+ surreal: {},
1337
+ });
1338
+ }
1339
+ export { _enum as enum };
1340
+ /** @deprecated This API has been merged into `z.enum()`. Use `z.enum()` instead.
1341
+ *
1342
+ * ```ts
1343
+ * enum Colors { red, green, blue }
1344
+ * z.enum(Colors);
1345
+ * ```
1346
+ */
1347
+ export function nativeEnum(entries, params) {
1348
+ return new ZodSurrealEnum({
1349
+ type: "enum",
1350
+ entries,
1351
+ ...core.util.normalizeParams(params),
1352
+ surreal: {},
1353
+ });
1354
+ }
1355
+ export const ZodSurrealLiteral = core.$constructor("ZodSurrealLiteral", (inst, def) => {
1356
+ // @ts-expect-error
1357
+ core.$ZodLiteral.init(inst, def);
1358
+ ZodSurrealType.init(inst, def);
1359
+ ZodSurrealField.init(inst, def);
1360
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.literal(inst, ctx, json, params);
1361
+ inst.values = new Set(def.values);
1362
+ Object.defineProperty(inst, "value", {
1363
+ get() {
1364
+ if (def.values.length > 1) {
1365
+ throw new Error("This schema contains multiple valid literal values. Use `.values` instead.");
1366
+ }
1367
+ return def.values[0];
1368
+ },
1369
+ });
1370
+ });
1371
+ export function literal(value, params) {
1372
+ return new ZodSurrealLiteral({
1373
+ type: "literal",
1374
+ values: Array.isArray(value) ? value : [value],
1375
+ ...core.util.normalizeParams(params),
1376
+ surreal: {},
1377
+ });
1378
+ }
1379
+ export const ZodSurrealFile = core.$constructor("ZodSurrealFile", (inst, def) => {
1380
+ // @ts-expect-error
1381
+ core.$ZodFile.init(inst, def);
1382
+ ZodSurrealType.init(inst, def);
1383
+ ZodSurrealField.init(inst, def);
1384
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.file(inst, ctx, json, params);
1385
+ inst.min = (size, params) => inst.check(core._minSize(size, params));
1386
+ inst.max = (size, params) => inst.check(core._maxSize(size, params));
1387
+ inst.mime = (types, params) => inst.check(core._mime(Array.isArray(types) ? types : [types], params));
1388
+ });
1389
+ export function file(params) {
1390
+ return new ZodSurrealFile({
1391
+ type: "file",
1392
+ ...core.util.normalizeParams(params),
1393
+ surreal: {},
1394
+ });
1395
+ }
1396
+ export const ZodSurrealTransform = core.$constructor("ZodSurrealTransform", (inst, def) => {
1397
+ // @ts-expect-error
1398
+ core.$ZodTransform.init(inst, def);
1399
+ ZodSurrealType.init(inst, def);
1400
+ ZodSurrealField.init(inst, def);
1401
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.transform(inst, ctx, json, params);
1402
+ inst._zod.parse = (payload, _ctx) => {
1403
+ if (_ctx.direction === "backward") {
1404
+ throw new core.$ZodEncodeError(inst.constructor.name);
1405
+ }
1406
+ payload.addIssue = (issue) => {
1407
+ if (typeof issue === "string") {
1408
+ payload.issues.push(core.util.issue(issue, payload.value, def));
1409
+ }
1410
+ else {
1411
+ // for Zod 3 backwards compatibility
1412
+ const _issue = issue;
1413
+ if (_issue.fatal)
1414
+ _issue.continue = false;
1415
+ _issue.code ??= "custom";
1416
+ _issue.input ??= payload.value;
1417
+ _issue.inst ??= inst;
1418
+ // _issue.continue ??= true;
1419
+ payload.issues.push(core.util.issue(_issue));
1420
+ }
1421
+ };
1422
+ const output = def.transform(payload.value, payload);
1423
+ if (output instanceof Promise) {
1424
+ return output.then((output) => {
1425
+ payload.value = output;
1426
+ return payload;
1427
+ });
1428
+ }
1429
+ payload.value = output;
1430
+ return payload;
1431
+ };
1432
+ });
1433
+ export function transform(fn) {
1434
+ return new ZodSurrealTransform({
1435
+ type: "transform",
1436
+ transform: fn,
1437
+ surreal: {},
1438
+ });
1439
+ }
1440
+ export const ZodSurrealOptional = core.$constructor("ZodSurrealOptional", (inst, def) => {
1441
+ // @ts-expect-error
1442
+ core.$ZodOptional.init(inst, def);
1443
+ ZodSurrealType.init(inst, def);
1444
+ ZodSurrealField.init(inst, def);
1445
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.optional(inst, ctx, json, params);
1446
+ inst.unwrap = () => inst._zod.def.innerType;
1447
+ });
1448
+ export function optional(innerType) {
1449
+ return new ZodSurrealOptional({
1450
+ type: "optional",
1451
+ innerType: innerType,
1452
+ surreal: {},
1453
+ });
1454
+ }
1455
+ export const ZodSurrealExactOptional = core.$constructor("ZodSurrealExactOptional", (inst, def) => {
1456
+ // @ts-expect-error
1457
+ core.$ZodExactOptional.init(inst, def);
1458
+ ZodSurrealType.init(inst, def);
1459
+ ZodSurrealField.init(inst, def);
1460
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.optional(inst, ctx, json, params);
1461
+ inst.unwrap = () => inst._zod.def.innerType;
27
1462
  });
28
- export const SurrealZodAny = core.$constructor("SurrealZodAny", (inst, def) => {
29
- // @ts-expect-error - unknown assertion error
30
- core.$ZodAny.init(inst, def);
31
- SurrealZodType.init(inst, def);
32
- // surreal internals
33
- inst._zod.def.surreal.type = "any";
1463
+ export function exactOptional(innerType) {
1464
+ return new ZodSurrealExactOptional({
1465
+ type: "optional",
1466
+ innerType: innerType,
1467
+ surreal: {},
1468
+ });
1469
+ }
1470
+ export const ZodSurrealNullable =
1471
+ /*@__PURE__*/ core.$constructor("ZodSurrealNullable", (inst, def) => {
1472
+ // @ts-expect-error
1473
+ core.$ZodNullable.init(inst, def);
1474
+ ZodSurrealType.init(inst, def);
1475
+ ZodSurrealField.init(inst, def);
1476
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.nullable(inst, ctx, json, params);
1477
+ inst.unwrap = () => inst._zod.def.innerType;
34
1478
  });
35
- export function any() {
36
- return core._any(SurrealZodAny);
1479
+ export function nullable(innerType) {
1480
+ return new ZodSurrealNullable({
1481
+ type: "nullable",
1482
+ innerType: innerType,
1483
+ surreal: {},
1484
+ });
37
1485
  }
38
- export const SurrealZodUnknown = core.$constructor("SurrealZodUnknown", (inst, def) => {
39
- // @ts-expect-error - unknown assertion error
40
- core.$ZodUnknown.init(inst, def);
41
- SurrealZodType.init(inst, def);
42
- // surreal internals
43
- inst._zod.def.surreal.type = "unknown";
1486
+ // nullish
1487
+ export function nullish(innerType) {
1488
+ return optional(nullable(innerType));
1489
+ }
1490
+ export const ZodSurrealDefault = core.$constructor("ZodSurrealDefault", (inst, def) => {
1491
+ // @ts-expect-error
1492
+ core.$ZodDefault.init(inst, def);
1493
+ ZodSurrealType.init(inst, def);
1494
+ ZodSurrealField.init(inst, def);
1495
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.default(inst, ctx, json, params);
1496
+ inst.unwrap = () => inst._zod.def.innerType;
1497
+ inst.removeDefault = inst.unwrap;
44
1498
  });
45
- export function unknown() {
46
- return core._unknown(SurrealZodUnknown);
1499
+ export function _default(innerType, defaultValue) {
1500
+ return new ZodSurrealDefault({
1501
+ type: "default",
1502
+ innerType: innerType,
1503
+ get defaultValue() {
1504
+ return typeof defaultValue === "function"
1505
+ ? defaultValue()
1506
+ : core.util.shallowClone(defaultValue);
1507
+ },
1508
+ surreal: {},
1509
+ });
47
1510
  }
48
- export const SurrealZodNever = core.$constructor("SurrealZodNever", (inst, def) => {
49
- // @ts-expect-error - unknown assertion error
50
- core.$ZodNever.init(inst, def);
51
- SurrealZodType.init(inst, def);
52
- // surreal internals
53
- inst._zod.def.surreal.type = "never";
1511
+ export const ZodSurrealPrefault = core.$constructor("ZodSurrealPrefault", (inst, def) => {
1512
+ // @ts-expect-error
1513
+ core.$ZodPrefault.init(inst, def);
1514
+ ZodSurrealType.init(inst, def);
1515
+ ZodSurrealField.init(inst, def);
1516
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.prefault(inst, ctx, json, params);
1517
+ inst.unwrap = () => inst._zod.def.innerType;
54
1518
  });
55
- export function never(params) {
56
- return core._never(SurrealZodNever, params);
1519
+ export function prefault(innerType, defaultValue) {
1520
+ return new ZodSurrealPrefault({
1521
+ type: "prefault",
1522
+ innerType: innerType,
1523
+ get defaultValue() {
1524
+ return typeof defaultValue === "function"
1525
+ ? defaultValue()
1526
+ : core.util.shallowClone(defaultValue);
1527
+ },
1528
+ surreal: {},
1529
+ });
57
1530
  }
58
- export const SurrealZodUndefined = core.$constructor("SurrealZodUndefined", (inst, def) => {
59
- // @ts-expect-error - unknown assertion error
60
- core.$ZodUndefined.init(inst, def);
61
- SurrealZodType.init(inst, def);
62
- // surreal internals
63
- inst._zod.def.surreal.type = "undefined";
1531
+ export const ZodSurrealNonOptional = core.$constructor("ZodSurrealNonOptional", (inst, def) => {
1532
+ // @ts-expect-error
1533
+ core.$ZodNonOptional.init(inst, def);
1534
+ ZodSurrealType.init(inst, def);
1535
+ ZodSurrealField.init(inst, def);
1536
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.nonoptional(inst, ctx, json, params);
1537
+ inst.unwrap = () => inst._zod.def.innerType;
64
1538
  });
65
- function _undefined(params) {
66
- return core._undefined(SurrealZodUndefined, params);
1539
+ export function nonoptional(innerType, params) {
1540
+ return new ZodSurrealNonOptional({
1541
+ type: "nonoptional",
1542
+ innerType: innerType,
1543
+ ...core.util.normalizeParams(params),
1544
+ surreal: {},
1545
+ });
67
1546
  }
68
- export { _undefined as undefined };
69
- export const SurrealZodNull = core.$constructor("SurrealZodNull", (inst, def) => {
70
- // @ts-expect-error - unknown assertion error
71
- core.$ZodNull.init(inst, def);
72
- SurrealZodType.init(inst, def);
73
- // surreal internals
74
- inst._zod.def.surreal.type = "null";
1547
+ export const ZodSurrealSuccess = core.$constructor("ZodSuccess", (inst, def) => {
1548
+ // @ts-expect-error
1549
+ core.$ZodSuccess.init(inst, def);
1550
+ ZodSurrealType.init(inst, def);
1551
+ ZodSurrealField.init(inst, def);
1552
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.success(inst, ctx, json, params);
1553
+ inst.unwrap = () => inst._zod.def.innerType;
75
1554
  });
76
- function _null(params) {
77
- return core._null(SurrealZodNull, params);
1555
+ export function success(innerType) {
1556
+ return new ZodSurrealSuccess({
1557
+ type: "success",
1558
+ innerType: innerType,
1559
+ surreal: {},
1560
+ });
78
1561
  }
79
- export { _null as null };
80
- export const SurrealZodBoolean = core.$constructor("SurrealZodBoolean", (inst, def) => {
81
- // @ts-expect-error - unknown assertion error
82
- core.$ZodBoolean.init(inst, def);
83
- SurrealZodType.init(inst, def);
84
- // surreal internals
85
- inst._zod.def.surreal.type = "boolean";
1562
+ export const ZodSurrealCatch = core.$constructor("ZodSurrealCatch", (inst, def) => {
1563
+ // @ts-expect-error
1564
+ core.$ZodCatch.init(inst, def);
1565
+ ZodSurrealType.init(inst, def);
1566
+ ZodSurrealField.init(inst, def);
1567
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.catch(inst, ctx, json, params);
1568
+ inst.unwrap = () => inst._zod.def.innerType;
1569
+ inst.removeCatch = inst.unwrap;
86
1570
  });
87
- export function boolean(params) {
88
- return core._boolean(SurrealZodBoolean, params);
1571
+ function _catch(innerType, catchValue) {
1572
+ return new ZodSurrealCatch({
1573
+ type: "catch",
1574
+ innerType: innerType,
1575
+ catchValue: (typeof catchValue === "function"
1576
+ ? catchValue
1577
+ : () => catchValue),
1578
+ surreal: {},
1579
+ });
89
1580
  }
90
- export const SurrealZodString = core.$constructor("SurrealZodString", (inst, def) => {
91
- // @ts-expect-error - unknown assertion error
92
- core.$ZodString.init(inst, def);
93
- SurrealZodType.init(inst, def);
94
- // surreal internals
95
- inst._zod.def.surreal.type = "string";
1581
+ export { _catch as catch };
1582
+ export const ZodSurrealNaN = core.$constructor("ZodSurrealNaN", (inst, def) => {
1583
+ // @ts-expect-error
1584
+ core.$ZodNaN.init(inst, def);
1585
+ ZodSurrealType.init(inst, def);
1586
+ ZodSurrealField.init(inst, def);
1587
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.nan(inst, ctx, json, params);
96
1588
  });
97
- export function string(params) {
98
- return core._string(SurrealZodString, params);
1589
+ export function nan(params) {
1590
+ return new ZodSurrealNaN({
1591
+ type: "nan",
1592
+ ...core.util.normalizeParams(params),
1593
+ surreal: {
1594
+ type: "number",
1595
+ },
1596
+ });
99
1597
  }
100
- export const SurrealZodNumber = core.$constructor("SurrealZodNumber", (inst, def) => {
101
- // @ts-expect-error - unknown assertion error
102
- core.$ZodNumber.init(inst, def);
103
- SurrealZodType.init(inst, def);
104
- // surreal internals
105
- inst._zod.def.surreal.type = "number";
1598
+ export const ZodSurrealPipe = core.$constructor("ZodSurrealPipe", (inst, def) => {
1599
+ // @ts-expect-error
1600
+ core.$ZodPipe.init(inst, def);
1601
+ ZodSurrealType.init(inst, def);
1602
+ ZodSurrealField.init(inst, def);
1603
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.pipe(inst, ctx, json, params);
1604
+ inst.in = def.in;
1605
+ inst.out = def.out;
106
1606
  });
107
- export function number(params) {
108
- return core._number(SurrealZodNumber, params);
1607
+ export function pipe(in_, out) {
1608
+ return new ZodSurrealPipe({
1609
+ type: "pipe",
1610
+ in: in_,
1611
+ out: out,
1612
+ // ...util.normalizeParams(params),
1613
+ surreal: {},
1614
+ });
109
1615
  }
110
- export const SurrealZodBigInt = core.$constructor("SurrealZodBigInt", (inst, def) => {
111
- // @ts-expect-error - unknown assertion error
112
- core.$ZodBigInt.init(inst, def);
113
- SurrealZodType.init(inst, def);
114
- // surreal internals
115
- inst._zod.def.surreal.type = "bigint";
1616
+ export const ZodSurrealCodec = core.$constructor("ZodSurrealCodec", (inst, def) => {
1617
+ // @ts-expect-error
1618
+ core.$ZodCodec.init(inst, def);
1619
+ ZodSurrealType.init(inst, def);
1620
+ ZodSurrealField.init(inst, def);
116
1621
  });
117
- export function bigint(params) {
118
- return core._bigint(SurrealZodBigInt, params);
1622
+ export function codec(in_, out, params) {
1623
+ return new ZodSurrealCodec({
1624
+ type: "pipe",
1625
+ in: in_,
1626
+ out: out,
1627
+ transform: params.decode,
1628
+ reverseTransform: params.encode,
1629
+ surreal: {},
1630
+ });
119
1631
  }
120
- export const SurrealZodObject = core.$constructor("SurrealZodObject", (inst, def) => {
121
- // TODO: Inline implementation and use core instead
122
- // @ts-expect-error - unknown assertion error
123
- core.$ZodObject.init(inst, def);
124
- SurrealZodType.init(inst, def);
125
- // surreal internals
126
- inst._zod.def.surreal.type = "object";
127
- // inst._zod.def.surreal.flexible = false;
128
- inst.loose = () => inst.clone({
129
- ...def,
130
- catchall: unknown(),
1632
+ export const ZodSurrealReadonly = core.$constructor("ZodSurrealReadonly", (inst, def) => {
1633
+ // @ts-expect-error
1634
+ core.$ZodReadonly.init(inst, def);
1635
+ ZodSurrealType.init(inst, def);
1636
+ ZodSurrealField.init(inst, def);
1637
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.readonly(inst, ctx, json, params);
1638
+ inst.unwrap = () => inst._zod.def.innerType;
1639
+ });
1640
+ export function readonly(innerType) {
1641
+ return new ZodSurrealReadonly({
1642
+ type: "readonly",
1643
+ innerType: innerType,
1644
+ surreal: {},
131
1645
  });
132
- inst.flexible = inst.loose;
133
- inst.strict = () => inst.clone({
134
- ...def,
135
- catchall: never(),
1646
+ }
1647
+ export const ZodSurrealTemplateLiteral = core.$constructor("ZodSurrealTemplateLiteral", (inst, def) => {
1648
+ // @ts-expect-error
1649
+ core.$ZodTemplateLiteral.init(inst, def);
1650
+ ZodSurrealType.init(inst, def);
1651
+ ZodSurrealField.init(inst, def);
1652
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.template_literal(inst, ctx, json, params);
1653
+ });
1654
+ export function templateLiteral(parts, params) {
1655
+ return new ZodSurrealTemplateLiteral({
1656
+ type: "template_literal",
1657
+ parts,
1658
+ ...core.util.normalizeParams(params),
1659
+ surreal: {},
136
1660
  });
137
- inst.extend = (incoming) => core.util.extend(inst, incoming);
138
- inst.safeExtend = (incoming) => core.util.safeExtend(inst, incoming);
1661
+ }
1662
+ export const ZodSurrealLazy = core.$constructor("ZodSurrealLazy", (inst, def) => {
1663
+ // @ts-expect-error
1664
+ core.$ZodLazy.init(inst, def);
1665
+ ZodSurrealType.init(inst, def);
1666
+ ZodSurrealField.init(inst, def);
1667
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.lazy(inst, ctx, json, params);
1668
+ inst.unwrap = () => inst._zod.def.getter();
139
1669
  });
140
- export function object(shape, params) {
141
- const def = {
142
- type: "object",
143
- shape: shape ?? {},
1670
+ export function lazy(getter) {
1671
+ return new ZodSurrealLazy({
1672
+ type: "lazy",
1673
+ getter: getter,
1674
+ surreal: {},
1675
+ });
1676
+ }
1677
+ export const ZodSurrealPromise = core.$constructor("ZodSurrealPromise", (inst, def) => {
1678
+ // @ts-expect-error
1679
+ core.$ZodPromise.init(inst, def);
1680
+ ZodSurrealType.init(inst, def);
1681
+ ZodSurrealField.init(inst, def);
1682
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.promise(inst, ctx, json, params);
1683
+ inst.unwrap = () => inst._zod.def.innerType;
1684
+ });
1685
+ export function promise(innerType) {
1686
+ return new ZodSurrealPromise({
1687
+ type: "promise",
1688
+ innerType: innerType,
1689
+ surreal: {},
1690
+ });
1691
+ }
1692
+ export const ZodSurrealFunction = core.$constructor("ZodSurrealFunction", (inst, def) => {
1693
+ // @ts-expect-error
1694
+ core.$ZodFunction.init(inst, def);
1695
+ ZodSurrealType.init(inst, def);
1696
+ // @ts-expect-error
1697
+ ZodSurrealField.init(inst, def);
1698
+ inst._zod.processJSONSchema = (ctx, json, params) =>
1699
+ // @ts-expect-error
1700
+ allProcessors.function(inst, ctx, json, params);
1701
+ });
1702
+ export function _function(params) {
1703
+ return new ZodSurrealFunction({
1704
+ type: "function",
1705
+ input: Array.isArray(params?.input)
1706
+ ? tuple(params?.input)
1707
+ : (params?.input ?? array(unknown())),
1708
+ output: params?.output ?? unknown(),
1709
+ surreal: {},
1710
+ });
1711
+ }
1712
+ export { _function as function };
1713
+ export const ZodSurrealCustom = core.$constructor("ZodSurrealCustom", (inst, def) => {
1714
+ // @ts-expect-error
1715
+ core.$ZodCustom.init(inst, def);
1716
+ ZodSurrealType.init(inst, def);
1717
+ ZodSurrealField.init(inst, def);
1718
+ inst._zod.processJSONSchema = (ctx, json, params) => allProcessors.custom(inst, ctx, json, params);
1719
+ });
1720
+ // custom checks
1721
+ export function check(fn) {
1722
+ const ch = new core.$ZodCheck({
1723
+ check: "custom",
1724
+ // ...util.normalizeParams(params),
1725
+ });
1726
+ ch._zod.check = fn;
1727
+ return ch;
1728
+ }
1729
+ export function custom(fn, _params) {
1730
+ const norm = core.util.normalizeParams(_params);
1731
+ norm.abort ??= true; // default to abort:false
1732
+ const schema = new ZodSurrealCustom({
1733
+ type: "custom",
1734
+ check: "custom",
1735
+ fn: fn ?? (() => true),
1736
+ ...norm,
1737
+ surreal: {},
1738
+ });
1739
+ return schema;
1740
+ }
1741
+ export function refine(fn, _params = {}) {
1742
+ const schema = new ZodSurrealCustom({
1743
+ type: "custom",
1744
+ check: "custom",
1745
+ fn: fn,
1746
+ ...core.util.normalizeParams(_params),
1747
+ surreal: {},
1748
+ });
1749
+ return schema;
1750
+ }
1751
+ // superRefine
1752
+ export function superRefine(fn) {
1753
+ const ch = core._check((payload) => {
1754
+ payload.addIssue = (issue) => {
1755
+ if (typeof issue === "string") {
1756
+ payload.issues.push(core.util.issue(issue, payload.value, ch._zod.def));
1757
+ }
1758
+ else {
1759
+ // for Zod 3 backwards compatibility
1760
+ const _issue = issue;
1761
+ if (_issue.fatal)
1762
+ _issue.continue = false;
1763
+ _issue.code ??= "custom";
1764
+ _issue.input ??= payload.value;
1765
+ _issue.inst ??= ch;
1766
+ _issue.continue ??= !ch._zod.def.abort; // abort is always undefined, so this is always true...
1767
+ payload.issues.push(core.util.issue(_issue));
1768
+ }
1769
+ };
1770
+ return fn(payload.value, payload);
1771
+ });
1772
+ return ch;
1773
+ }
1774
+ // Re-export describe and meta from core
1775
+ export const describe = core.describe;
1776
+ export const meta = core.meta;
1777
+ function _instanceof(cls, params = {}) {
1778
+ const inst = new ZodSurrealCustom({
1779
+ type: "custom",
1780
+ check: "custom",
1781
+ fn: (data) => data instanceof cls,
1782
+ abort: true,
144
1783
  ...core.util.normalizeParams(params),
145
- };
146
- return new SurrealZodObject({
147
- ...def,
148
1784
  surreal: {
149
- type: "object",
150
- flexible: false,
1785
+ type: "custom",
151
1786
  },
152
1787
  });
1788
+ inst._zod.bag.Class = cls;
1789
+ // Override check to emit invalid_type instead of custom
1790
+ inst._zod.check = (payload) => {
1791
+ if (!(payload.value instanceof cls)) {
1792
+ payload.issues.push({
1793
+ code: "invalid_type",
1794
+ expected: cls.name,
1795
+ input: payload.value,
1796
+ inst,
1797
+ path: [...(inst._zod.def.path ?? [])],
1798
+ });
1799
+ }
1800
+ };
1801
+ return inst;
1802
+ }
1803
+ export { _instanceof as instanceof };
1804
+ // stringbool
1805
+ export const stringbool = (_params) => {
1806
+ const params = core.util.normalizeParams(_params);
1807
+ let truthyArray = params.truthy ?? ["true", "1", "yes", "on", "y", "enabled"];
1808
+ let falsyArray = params.falsy ?? ["false", "0", "no", "off", "n", "disabled"];
1809
+ if (params.case !== "sensitive") {
1810
+ truthyArray = truthyArray.map((v) => typeof v === "string" ? v.toLowerCase() : v);
1811
+ falsyArray = falsyArray.map((v) => typeof v === "string" ? v.toLowerCase() : v);
1812
+ }
1813
+ const truthySet = new Set(truthyArray);
1814
+ const falsySet = new Set(falsyArray);
1815
+ const stringSchema = new ZodSurrealString({
1816
+ type: "string",
1817
+ error: params.error,
1818
+ surreal: { type: "string" },
1819
+ });
1820
+ const booleanSchema = new ZodSurrealBoolean({
1821
+ type: "boolean",
1822
+ error: params.error,
1823
+ surreal: { type: "bool" },
1824
+ });
1825
+ const codec = new ZodSurrealCodec({
1826
+ type: "pipe",
1827
+ in: stringSchema,
1828
+ out: booleanSchema,
1829
+ transform: ((input, payload) => {
1830
+ let data = input;
1831
+ if (params.case !== "sensitive")
1832
+ data = data.toLowerCase();
1833
+ if (truthySet.has(data)) {
1834
+ return true;
1835
+ }
1836
+ else if (falsySet.has(data)) {
1837
+ return false;
1838
+ }
1839
+ else {
1840
+ payload.issues.push({
1841
+ code: "invalid_value",
1842
+ expected: "stringbool",
1843
+ values: [...truthySet, ...falsySet],
1844
+ input: payload.value,
1845
+ inst: codec,
1846
+ continue: false,
1847
+ });
1848
+ return {};
1849
+ }
1850
+ }),
1851
+ reverseTransform: ((input, _payload) => {
1852
+ if (input === true) {
1853
+ return truthyArray[0] || "true";
1854
+ }
1855
+ else {
1856
+ return falsyArray[0] || "false";
1857
+ }
1858
+ }),
1859
+ error: params.error,
1860
+ surreal: {},
1861
+ });
1862
+ return codec;
1863
+ };
1864
+ export function json(params) {
1865
+ const jsonSchema = lazy(() => {
1866
+ return union([
1867
+ string(params),
1868
+ number(),
1869
+ boolean(),
1870
+ _null(),
1871
+ array(jsonSchema),
1872
+ record(string(), jsonSchema),
1873
+ ]);
1874
+ });
1875
+ return jsonSchema;
1876
+ }
1877
+ ////////////////////////////////////////////////////
1878
+ ////////////////////////////////////////////////////
1879
+ ////////// //////////
1880
+ ////////// ZodSurrealPreprocess //////////
1881
+ ////////// //////////
1882
+ ////////////////////////////////////////////////////
1883
+ ////////////////////////////////////////////////////
1884
+ // /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
1885
+ export function preprocess(fn, schema) {
1886
+ return pipe(transform(fn), schema);
153
1887
  }
154
1888
  function normalizeRecordIdDef(def) {
155
- const invalidType = getInvalidRecordIdValueSchema(def.innerType);
156
- if (invalidType) {
157
- throw new Error(`${invalidType} is not valid as a RecordId's value`);
1889
+ const { type, context } = inferSurrealType(def.innerType);
1890
+ const isValid = Array.from(context.type).every((option) => ["any", "string", "number", "int", "array", "object"].includes(option) ||
1891
+ option.startsWith("array<") ||
1892
+ option.startsWith("[") ||
1893
+ option.startsWith("{") ||
1894
+ option.startsWith("'") ||
1895
+ option.startsWith('"') ||
1896
+ /^\d+(\.\d+)?f?$/.test(option));
1897
+ if (!isValid) {
1898
+ throw new Error(`${type} is not valid as a RecordId's value`);
158
1899
  }
159
1900
  return {
160
1901
  ...def,
161
1902
  };
162
1903
  }
163
- function getInvalidRecordIdValueSchema(schema) {
164
- const def = schema._zod.def;
165
- switch (def.type) {
166
- case "any":
167
- case "string":
168
- case "number":
169
- return "";
170
- default:
171
- return def.type;
172
- }
173
- }
174
- export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst, def) => {
175
- SurrealZodType.init(inst, def);
1904
+ // /* instanbul ignore next */
1905
+ // function parseRecordIdString(id: string) {
1906
+ // let table = "";
1907
+ // let value: RecordIdValue = "";
1908
+ // const match = id.match(/^(?:⟨(.*)⟩|`(.*)`|(.*)):(?:⟨(.*)⟩|`(.*)`|(.*))$/);
1909
+ // if (!match) {
1910
+ // throw new Error(`Invalid record id string: ${id}`);
1911
+ // }
1912
+ // table = (match[1] ?? match[2] ?? match[2] ?? "").replace(/\\⟩/g, "⟩");
1913
+ // value = match[4] ?? match[5] ?? match[6] ?? "";
1914
+ // // check if value is a number
1915
+ // value = parseSurrealValue(value);
1916
+ // // console.log("result:", value);
1917
+ // return new RecordId(table, value);
1918
+ // }
1919
+ // /* instanbul ignore stop */
1920
+ // type ParserContext = {
1921
+ // in: "root" | "array" | "object";
1922
+ // acc: string;
1923
+ // path: ("array" | "object")[];
1924
+ // };
1925
+ // function parseSurrealValue(str: string) {
1926
+ // const stack: {
1927
+ // in: "root" | "array" | "object";
1928
+ // value: any;
1929
+ // }[] = [];
1930
+ // let ctx: ParserContext = {
1931
+ // in: "root",
1932
+ // acc: "",
1933
+ // path: [],
1934
+ // };
1935
+ // let value: any;
1936
+ // function expr() {
1937
+ // const parsed = ctx.acc;
1938
+ // // Decimal with optional exponent
1939
+ // if (/^[-+]?\d+(?:\.\d+)?(?:e[-+]?\d+)?dec$/i.test(parsed)) {
1940
+ // return new Decimal(parsed.slice(0, -3));
1941
+ // }
1942
+ // // Strict integer → Number | BigInt
1943
+ // if (/^[-+]?\d+f?$/.test(parsed)) {
1944
+ // const asBigInt = BigInt(parsed.replace(/f$/i, ""));
1945
+ // if (
1946
+ // asBigInt > BigInt(Number.MAX_SAFE_INTEGER) ||
1947
+ // asBigInt < BigInt(Number.MIN_SAFE_INTEGER)
1948
+ // ) {
1949
+ // return asBigInt;
1950
+ // }
1951
+ // return Number(parsed.replace(/f$/i, ""));
1952
+ // }
1953
+ // // Float or exponent → Number
1954
+ // if (/^[-+]?\d+(?:\.\d+)?(?:e[-+]?\d+)?f?$/i.test(parsed)) {
1955
+ // return Number(parsed.replace(/f$/, ""));
1956
+ // }
1957
+ // return parsed;
1958
+ // }
1959
+ // function enter(type: "array" | "object") {
1960
+ // if (ctx.in !== "root") {
1961
+ // stack.push({ in: ctx.in, value });
1962
+ // }
1963
+ // ctx.in = type;
1964
+ // if (type === "array") {
1965
+ // value = [];
1966
+ // } else if (type === "object") {
1967
+ // value = {};
1968
+ // }
1969
+ // }
1970
+ // function exit() {
1971
+ // if (ctx.in === "array" && ctx.acc) {
1972
+ // nextArray();
1973
+ // }
1974
+ // const popped = stack.pop();
1975
+ // if (!popped) {
1976
+ // return;
1977
+ // }
1978
+ // value = popped.value;
1979
+ // ctx.in = popped.in;
1980
+ // ctx.acc = "";
1981
+ // }
1982
+ // function nextArray() {
1983
+ // (value as any[]).push(expr());
1984
+ // }
1985
+ // for (let i = 0; i < str.length; i++) {
1986
+ // const ch = str[i];
1987
+ // if (ch === "[") {
1988
+ // enter("array");
1989
+ // } else if (ch === "{") {
1990
+ // enter("object");
1991
+ // } else if (ch === "]") {
1992
+ // exit();
1993
+ // } else if (ch === "}") {
1994
+ // exit();
1995
+ // } else if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
1996
+ // continue;
1997
+ // } else if (ch === ",") {
1998
+ // if (ctx.in === "array") nextArray();
1999
+ // ctx.acc = "";
2000
+ // } else {
2001
+ // ctx.acc += ch;
2002
+ // }
2003
+ // // console.log(inspect({ ...ctx, stack }, { depth: Infinity, colors: true }));
2004
+ // }
2005
+ // return value;
2006
+ // }
2007
+ export const ZodSurrealdRecordId = core.$constructor("ZodSurrealRecordId", (inst, def) => {
2008
+ ZodSurrealType.init(inst, def);
2009
+ ZodSurrealField.init(inst, def);
176
2010
  // surreal internals
177
- inst._zod.def.surreal.type = "record_id";
178
2011
  const normalized = normalizeRecordIdDef(def);
179
2012
  inst.anytable = () => {
180
2013
  return inst.clone({
@@ -194,6 +2027,20 @@ export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst,
194
2027
  innerType,
195
2028
  });
196
2029
  };
2030
+ inst.id = inst.type;
2031
+ inst.value = inst.type;
2032
+ // ------- Parsing/Encoding/Decoding -------
2033
+ const _inst = inst;
2034
+ _inst.fromParts = (table, id, params) => _inst.decode(new RecordId(table, id), params);
2035
+ _inst.fromPartsAsync = (table, id, params) => _inst.decodeAsync(new RecordId(table, id), params);
2036
+ _inst.safeFromParts = (table, id, params) => _inst.safeDecode(new RecordId(table, id), params);
2037
+ _inst.safeFromPartsAsync = (table, id, params) => _inst.safeDecodeAsync(new RecordId(table, id), params);
2038
+ if (normalized.table?.length === 1) {
2039
+ _inst.fromId = (id, params) => _inst.decode(new RecordId(normalized.table?.[0] ?? "", id), params);
2040
+ _inst.fromIdAsync = (id, params) => _inst.decodeAsync(new RecordId(normalized.table?.[0] ?? "", id), params);
2041
+ _inst.safeFromId = (id, params) => _inst.safeDecode(new RecordId(normalized.table?.[0] ?? "", id), params);
2042
+ _inst.safeFromIdAsync = (id, params) => _inst.safeDecodeAsync(new RecordId(normalized.table?.[0] ?? "", id), params);
2043
+ }
197
2044
  inst._zod.parse = (payload, ctx) => {
198
2045
  if (payload.value instanceof RecordId) {
199
2046
  if (normalized.table &&
@@ -221,13 +2068,14 @@ export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst,
221
2068
  else if (result.issues.length) {
222
2069
  payload.issues.push(...core.util.prefixIssues("id", result.issues));
223
2070
  }
224
- payload.value = new RecordId(payload.value.table.name, result.value);
2071
+ else {
2072
+ payload.value = new RecordId(payload.value.table.name, result.value);
2073
+ }
225
2074
  }
226
2075
  else {
227
2076
  payload.issues.push({
228
2077
  code: "invalid_type",
229
- // TODO: Surreal specific issues
230
- expected: "custom",
2078
+ expected: "record_id",
231
2079
  input: payload.value,
232
2080
  });
233
2081
  }
@@ -236,14 +2084,11 @@ export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst,
236
2084
  return inst;
237
2085
  });
238
2086
  export function recordId(what, innerType) {
239
- return new SurrealZodRecordId({
240
- // Zod would not be happy if we have a custom type here, so we use any
2087
+ return new ZodSurrealdRecordId({
241
2088
  type: "any",
242
2089
  table: what ? (Array.isArray(what) ? what : [what]) : undefined,
243
2090
  innerType: innerType ?? any(),
244
- surreal: {
245
- type: "record_id",
246
- },
2091
+ surreal: {},
247
2092
  });
248
2093
  }
249
2094
  function handleFieldResult(result, final, field, input) {
@@ -252,12 +2097,12 @@ function handleFieldResult(result, final, field, input) {
252
2097
  }
253
2098
  if (result.value === undefined) {
254
2099
  if (field in input) {
255
- // @ts-expect-error: field not index-checked on final.value
2100
+ // @ts-expect-error: field not index-checked on final.value, doesnt matter
256
2101
  final.value[field] = undefined;
257
2102
  }
258
2103
  }
259
2104
  else {
260
- // @ts-expect-error: field not index-checked on final.value
2105
+ // @ts-expect-error: field not index-checked on final.value, doesnt matter
261
2106
  final.value[field] = result.value;
262
2107
  }
263
2108
  }
@@ -286,7 +2131,6 @@ function handleCatchall(promises, input, payload, ctx, def, inst) {
286
2131
  code: "unrecognized_keys",
287
2132
  keys: unrecognized,
288
2133
  input,
289
- inst,
290
2134
  });
291
2135
  }
292
2136
  if (!promises.length)
@@ -296,29 +2140,19 @@ function handleCatchall(promises, input, payload, ctx, def, inst) {
296
2140
  function normalizeTableDef(def) {
297
2141
  const fields = {};
298
2142
  const fieldNames = Object.keys(def.fields);
299
- if (def.fields.id) {
300
- if (def.fields.id instanceof SurrealZodRecordId) {
301
- fields.id = def.fields.id.table(def.name);
302
- }
303
- else {
304
- fields.id = recordId(def.name).type(def.fields.id);
305
- }
306
- }
307
- else {
2143
+ if (!def.fields.id) {
308
2144
  fields.id = recordId(def.name).type(any());
309
2145
  fieldNames.push("id");
310
2146
  }
311
- if (def.dto && !(fields.id instanceof SurrealZodOptional)) {
312
- fields.id = optional(fields.id);
2147
+ else if (def.fields.id instanceof ZodSurrealdRecordId) {
2148
+ fields.id = def.fields.id.table(def.name);
2149
+ }
2150
+ else {
2151
+ fields.id = recordId(def.name).type(def.fields.id);
313
2152
  }
314
2153
  for (const field of fieldNames) {
315
2154
  if (field === "id")
316
2155
  continue;
317
- // if (!def.fields[field]?._zod.traits.has("SurrealZodType")) {
318
- // throw new Error(
319
- // `Invalid field definition for "${field}": expected a Surreal Zod schema`,
320
- // );
321
- // }
322
2156
  fields[field] = def.fields[field];
323
2157
  }
324
2158
  return {
@@ -328,12 +2162,20 @@ function normalizeTableDef(def) {
328
2162
  fieldNamesSet: new Set(fieldNames),
329
2163
  };
330
2164
  }
331
- export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def) => {
332
- SurrealZodType.init(inst, def);
2165
+ export const ZodSurrealTable = core.$constructor("SurrealZodTable", (inst, def) => {
2166
+ // @ts-expect-error
2167
+ core.$ZodType.init(inst, def);
333
2168
  const normalized = normalizeTableDef(def);
334
2169
  // @ts-expect-error - through normalization id is always present
335
2170
  inst._zod.def.fields = normalized.fields;
336
2171
  const catchall = normalized.catchall;
2172
+ const table = new Table(def.name);
2173
+ assignParsingMethods(inst);
2174
+ inst.clone = (def, params) => core.clone(inst, def, params);
2175
+ inst.register = ((reg, meta) => {
2176
+ reg.add(inst, meta);
2177
+ return inst;
2178
+ });
337
2179
  inst.name = (name) => {
338
2180
  return inst.clone({
339
2181
  ...inst._zod.def,
@@ -341,12 +2183,49 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
341
2183
  });
342
2184
  };
343
2185
  inst.fields = (fields) => {
2186
+ if (inst._zod.def.surreal.tableType === "relation") {
2187
+ fields = {
2188
+ in: inst._zod.def.fields.in ?? recordId().type(any()),
2189
+ out: inst._zod.def.fields.out ?? recordId().type(any()),
2190
+ ...fields,
2191
+ };
2192
+ }
344
2193
  return inst.clone({
345
2194
  ...inst._zod.def,
346
2195
  // @ts-expect-error - id may or may not be provided
347
2196
  fields,
348
2197
  });
349
2198
  };
2199
+ // @ts-expect-error - type defined conditionally
2200
+ inst.from = (from) => {
2201
+ if (inst._zod.def.surreal.tableType !== "relation") {
2202
+ throw new Error("Cannot call .from() on a non-relation table");
2203
+ }
2204
+ return inst.clone({
2205
+ ...inst._zod.def,
2206
+ fields: {
2207
+ ...inst._zod.def.fields,
2208
+ in: from instanceof ZodSurrealdRecordId ? from : recordId(from),
2209
+ },
2210
+ });
2211
+ };
2212
+ // @ts-expect-error - type defined conditionally
2213
+ inst.to = (to) => {
2214
+ if (inst._zod.def.surreal.tableType !== "relation") {
2215
+ throw new Error("Cannot call .to() on a non-relation table");
2216
+ }
2217
+ return inst.clone({
2218
+ ...inst._zod.def,
2219
+ fields: {
2220
+ ...inst._zod.def.fields,
2221
+ out: to instanceof ZodSurrealdRecordId ? to : recordId(to),
2222
+ },
2223
+ });
2224
+ };
2225
+ // @ts-expect-error - type defined conditionally
2226
+ inst.in = inst.from;
2227
+ // @ts-expect-error - type defined conditionally
2228
+ inst.out = inst.to;
350
2229
  inst.any = () => {
351
2230
  return inst.clone({
352
2231
  ...inst._zod.def,
@@ -357,7 +2236,7 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
357
2236
  });
358
2237
  };
359
2238
  inst.normal = () => {
360
- return new SurrealZodTableNormal({
2239
+ return inst.clone({
361
2240
  ...inst._zod.def,
362
2241
  surreal: {
363
2242
  ...inst._zod.def.surreal,
@@ -366,14 +2245,13 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
366
2245
  });
367
2246
  };
368
2247
  inst.relation = () => {
369
- // @ts-expect-error - id set in constructor
370
- return new SurrealZodTableRelation({
2248
+ return inst.clone({
371
2249
  ...inst._zod.def,
372
- // fields: {
373
- // in: recordId().type(any()),
374
- // out: recordId().type(any()),
375
- // ...def.fields,
376
- // },
2250
+ fields: {
2251
+ in: recordId().type(any()),
2252
+ out: recordId().type(any()),
2253
+ ...inst._zod.def.fields,
2254
+ },
377
2255
  surreal: {
378
2256
  ...inst._zod.def.surreal,
379
2257
  tableType: "relation",
@@ -427,32 +2305,201 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
427
2305
  },
428
2306
  });
429
2307
  };
430
- // @ts-expect-error - through normalization id is always present
431
- inst.record = () => normalized.fields.id;
2308
+ inst.record = () => inst._zod.def.fields.id;
2309
+ inst.table = () => table;
432
2310
  inst.dto = () => {
433
- return inst.clone({
434
- ...inst._zod.def,
435
- dto: true,
436
- });
437
- };
438
- inst.entity = () => {
439
- let id = normalized.fields.id;
440
- while (id && id instanceof SurrealZodOptional) {
441
- id = id.unwrap();
442
- }
443
- return inst.clone({
444
- ...inst._zod.def,
445
- dto: false,
446
- fields: {
447
- ...normalized.fields,
448
- id,
2311
+ return new ZodSurrealObject({
2312
+ type: "object",
2313
+ shape: {
2314
+ ...inst._zod.def.fields,
2315
+ id: optional(inst._zod.def.fields.id),
449
2316
  },
2317
+ catchall: inst._zod.def.catchall,
2318
+ surreal: {},
450
2319
  });
451
2320
  };
452
2321
  // @ts-expect-error - overloaded
453
2322
  inst.toSurql = (statement = "define", options) =>
454
2323
  // @ts-expect-error - overloaded
455
2324
  tableToSurql(inst, statement, options);
2325
+ // @ts-expect-error - false-positive
2326
+ inst.extend = (extraFields) => {
2327
+ if (!core.util.isPlainObject(extraFields)) {
2328
+ throw new Error("Invalid input to extend: expected a plain object");
2329
+ }
2330
+ const checks = inst._zod.def.checks;
2331
+ const hasChecks = checks && checks.length > 0;
2332
+ if (hasChecks) {
2333
+ throw new Error("Table schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
2334
+ }
2335
+ const mergedDef = core.util.mergeDefs(inst._zod.def, {
2336
+ get fields() {
2337
+ const fields = { ...inst._zod.def.fields, ...extraFields };
2338
+ core.util.assignProp(this, "fields", fields); // self-caching
2339
+ return fields;
2340
+ },
2341
+ checks: [],
2342
+ });
2343
+ return inst.clone(mergedDef);
2344
+ };
2345
+ // @ts-expect-error - false-positive
2346
+ inst.safeExtend = (extraFields) => {
2347
+ if (!core.util.isPlainObject(extraFields)) {
2348
+ throw new Error("Invalid input to safeExtend: expected a plain object");
2349
+ }
2350
+ const def = {
2351
+ ...inst._zod.def,
2352
+ get fields() {
2353
+ const fields = { ...inst._zod.def.fields, ...extraFields };
2354
+ core.util.assignProp(this, "fields", fields); // self-caching
2355
+ return fields;
2356
+ },
2357
+ checks: inst._zod.def.checks,
2358
+ };
2359
+ return inst.clone(def);
2360
+ };
2361
+ inst.pick = (mask) => {
2362
+ const currDef = inst._zod.def;
2363
+ const def = core.util.mergeDefs(inst._zod.def, {
2364
+ get fields() {
2365
+ const newFields = {};
2366
+ for (const key in mask) {
2367
+ if (!(key in currDef.fields)) {
2368
+ throw new Error(`Unrecognized key: "${key}"`);
2369
+ }
2370
+ if (!mask[key])
2371
+ continue;
2372
+ newFields[key] = currDef.fields[key];
2373
+ }
2374
+ core.util.assignProp(this, "fields", newFields); // self-caching
2375
+ return newFields;
2376
+ },
2377
+ checks: [],
2378
+ });
2379
+ if ("id" in mask && mask.id === false) {
2380
+ return new classic.ZodObject({
2381
+ type: "object",
2382
+ shape: def.fields,
2383
+ catchall: def.catchall,
2384
+ });
2385
+ }
2386
+ return inst.clone(def);
2387
+ };
2388
+ inst.omit = (mask) => {
2389
+ const currDef = inst._zod.def;
2390
+ const def = core.util.mergeDefs(inst._zod.def, {
2391
+ get fields() {
2392
+ const newFields = { ...currDef.fields };
2393
+ for (const key in mask) {
2394
+ if (!(key in currDef.fields)) {
2395
+ throw new Error(`Unrecognized key: "${key}"`);
2396
+ }
2397
+ if (!mask[key])
2398
+ continue;
2399
+ delete newFields[key];
2400
+ }
2401
+ core.util.assignProp(this, "fields", newFields); // self-caching
2402
+ return newFields;
2403
+ },
2404
+ checks: [],
2405
+ });
2406
+ if ("id" in mask && mask.id === true) {
2407
+ return new classic.ZodObject({
2408
+ type: "object",
2409
+ shape: def.fields,
2410
+ catchall: def.catchall,
2411
+ });
2412
+ }
2413
+ return inst.clone(def);
2414
+ };
2415
+ inst.partial = (mask) => {
2416
+ const def = core.util.mergeDefs(inst._zod.def, {
2417
+ get fields() {
2418
+ const oldFields = inst._zod.def.fields;
2419
+ const fields = { ...oldFields };
2420
+ if (typeof mask === "object") {
2421
+ for (const key in mask) {
2422
+ if (!(key in oldFields)) {
2423
+ throw new Error(`Unrecognized key: "${key}"`);
2424
+ }
2425
+ if (!mask[key])
2426
+ continue;
2427
+ // if (oldShape[key]!._zod.optin === "optional") continue;
2428
+ fields[key] = classic.ZodOptional
2429
+ ? new classic.ZodOptional({
2430
+ type: "optional",
2431
+ innerType: oldFields[key],
2432
+ })
2433
+ : oldFields[key];
2434
+ }
2435
+ }
2436
+ else {
2437
+ for (const key in oldFields) {
2438
+ if (key === "id" && mask !== true)
2439
+ continue;
2440
+ // if (oldShape[key]!._zod.optin === "optional") continue;
2441
+ fields[key] = classic.ZodOptional
2442
+ ? new classic.ZodOptional({
2443
+ type: "optional",
2444
+ innerType: oldFields[key],
2445
+ })
2446
+ : oldFields[key];
2447
+ }
2448
+ }
2449
+ core.util.assignProp(this, "fields", fields); // self-caching
2450
+ return fields;
2451
+ },
2452
+ checks: [],
2453
+ });
2454
+ if (mask === true ||
2455
+ (typeof mask === "object" && "id" in mask && mask.id === true)) {
2456
+ return new classic.ZodObject({
2457
+ type: "object",
2458
+ shape: def.fields,
2459
+ catchall: def.catchall,
2460
+ });
2461
+ }
2462
+ return inst.clone(def);
2463
+ };
2464
+ inst.required = (mask) => {
2465
+ const def = core.util.mergeDefs(inst._zod.def, {
2466
+ get fields() {
2467
+ const oldFields = inst._zod.def.fields;
2468
+ const fields = { ...oldFields };
2469
+ if (mask) {
2470
+ for (const key in mask) {
2471
+ if (!(key in fields)) {
2472
+ throw new Error(`Unrecognized key: "${key}"`);
2473
+ }
2474
+ if (!mask[key])
2475
+ continue;
2476
+ if (key === "id")
2477
+ continue;
2478
+ // overwrite with non-optional
2479
+ fields[key] = new classic.ZodNonOptional({
2480
+ type: "nonoptional",
2481
+ innerType: oldFields[key],
2482
+ });
2483
+ }
2484
+ }
2485
+ else {
2486
+ for (const key in oldFields) {
2487
+ if (key === "id")
2488
+ continue;
2489
+ // overwrite with non-optional
2490
+ fields[key] = new classic.ZodNonOptional({
2491
+ type: "nonoptional",
2492
+ innerType: oldFields[key],
2493
+ });
2494
+ }
2495
+ }
2496
+ core.util.assignProp(this, "fields", fields); // self-caching
2497
+ return fields;
2498
+ },
2499
+ checks: [],
2500
+ });
2501
+ return inst.clone(def);
2502
+ };
456
2503
  inst._zod.parse = (payload, ctx) => {
457
2504
  const input = payload.value;
458
2505
  if (!core.util.isObject(input)) {
@@ -460,7 +2507,6 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
460
2507
  expected: "object",
461
2508
  code: "invalid_type",
462
2509
  input,
463
- inst,
464
2510
  });
465
2511
  return payload;
466
2512
  }
@@ -489,14 +2535,14 @@ export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def)
489
2535
  return inst;
490
2536
  });
491
2537
  export function table(name) {
492
- return new SurrealZodTable({
493
- type: "any",
2538
+ return new ZodSurrealTable({
2539
+ type: "table",
494
2540
  name,
495
2541
  // @ts-expect-error - id set in constructor
496
2542
  fields: {},
497
2543
  catchall: unknown(),
2544
+ dto: false,
498
2545
  surreal: {
499
- type: "table",
500
2546
  tableType: "any",
501
2547
  schemafull: false,
502
2548
  drop: false,
@@ -504,100 +2550,30 @@ export function table(name) {
504
2550
  },
505
2551
  });
506
2552
  }
507
- export const SurrealZodTableNormal = core.$constructor("SurrealZodTableNormal", (inst, def) => {
508
- SurrealZodTable.init(inst, def);
509
- });
510
2553
  export function normalTable(name) {
511
2554
  return table(name).normal();
512
2555
  }
513
- export const SurrealZodTableRelation = core.$constructor("SurrealZodTableRelation", (inst, def) => {
514
- SurrealZodTable.init(inst, def);
515
- inst.from = (from) => {
516
- return new SurrealZodTableRelation({
517
- ...def,
518
- fields: {
519
- ...def.fields,
520
- in: from instanceof SurrealZodRecordId ? from : recordId(from),
521
- },
522
- });
523
- };
524
- inst.to = (to) => {
525
- return new SurrealZodTableRelation({
526
- ...def,
527
- fields: {
528
- ...def.fields,
529
- out: to instanceof SurrealZodRecordId ? to : recordId(to),
530
- },
531
- });
532
- };
533
- inst.in = inst.from;
534
- inst.out = inst.to;
535
- inst.fields = (fields) => {
536
- return new SurrealZodTableRelation({
537
- ...def,
538
- fields: {
539
- ...def.fields,
540
- ...fields,
541
- },
2556
+ export const ZodSurrealDuration = core.$constructor("ZodSurrealDuration", (inst, def) => {
2557
+ ZodSurrealType.init(inst, def);
2558
+ ZodSurrealField.init(inst, def);
2559
+ inst._zod.parse = (payload, ctx) => {
2560
+ if (payload.value instanceof Duration) {
2561
+ return payload;
2562
+ }
2563
+ payload.issues.push({
2564
+ code: "invalid_type",
2565
+ expected: "duration",
2566
+ input: null,
542
2567
  });
2568
+ return payload;
543
2569
  };
544
2570
  return inst;
545
2571
  });
546
- export function relationTable(name) {
547
- return table(name).relation();
548
- }
549
- export const SurrealZodOptional = core.$constructor("SurrealZodOptional", (inst, def) => {
550
- // @ts-expect-error - unknown assertion error
551
- core.$ZodOptional.init(inst, def);
552
- SurrealZodType.init(inst, def);
553
- inst.unwrap = () => {
554
- return inst._zod.def.innerType;
555
- };
556
- });
557
- export function optional(innerType) {
558
- return new SurrealZodOptional({
559
- type: "optional",
560
- innerType,
561
- surreal: {
562
- type: "optional",
563
- },
564
- });
565
- }
566
- export const SurrealZodNonOptional = core.$constructor("SurrealZodNonOptional", (inst, def) => {
567
- // @ts-expect-error - unknown assertion error
568
- core.$ZodNonOptional.init(inst, def);
569
- SurrealZodType.init(inst, def);
570
- });
571
- export function nonoptional(innerType) {
572
- return new SurrealZodNonOptional({
573
- type: "nonoptional",
574
- innerType,
575
- surreal: {
576
- type: "nonoptional",
577
- },
578
- });
579
- }
580
- export const SurrealZodNullable = core.$constructor("SurrealZodNullable", (inst, def) => {
581
- // @ts-expect-error - unknown assertion error
582
- core.$ZodNullable.init(inst, def);
583
- SurrealZodType.init(inst, def);
584
- });
585
- export function nullable(innerType) {
586
- return new SurrealZodNullable({
587
- type: "nullable",
588
- innerType,
2572
+ export function duration() {
2573
+ return new ZodSurrealDuration({
2574
+ type: "duration",
589
2575
  surreal: {
590
- type: "nullable",
2576
+ type: "duration",
591
2577
  },
592
2578
  });
593
2579
  }
594
- /////////////////////////////////////////////////
595
- /////////////////////////////////////////////////
596
- ////////// //////////
597
- ////////// SurrealZodNullish //////////
598
- ////////// //////////
599
- /////////////////////////////////////////////////
600
- /////////////////////////////////////////////////
601
- export function nullish(innerType) {
602
- return optional(nullable(innerType));
603
- }