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

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