surreal-zod 0.0.0-alpha.4 → 0.0.0-alpha.6
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/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/surql.d.ts +1 -1
- package/lib/surql.js +17 -19
- package/lib/zod/schema.d.ts +97 -0
- package/lib/zod/schema.js +298 -0
- package/lib/zod/utils.d.ts +2 -0
- package/lib/zod/utils.js +2 -0
- package/package.json +2 -2
- package/src/index.ts +1 -1
- package/src/surql.ts +19 -19
- package/src/zod/schema.ts +669 -0
- package/src/zod/utils.ts +6 -0
- package/src/zod.ts +0 -302
package/src/zod.ts
DELETED
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
import { RecordId, type RecordIdValue, type Table } from "surrealdb";
|
|
2
|
-
import z4 from "zod/v4";
|
|
3
|
-
import * as core from "zod/v4/core";
|
|
4
|
-
|
|
5
|
-
//////////////////////////////////////////////
|
|
6
|
-
//////////////////////////////////////////////
|
|
7
|
-
////////// //////////
|
|
8
|
-
////////// SurrealZodType //////////
|
|
9
|
-
////////// //////////
|
|
10
|
-
//////////////////////////////////////////////
|
|
11
|
-
//////////////////////////////////////////////
|
|
12
|
-
|
|
13
|
-
export interface SurrealZodTypeDef extends core.$ZodTypeDef {
|
|
14
|
-
surrealType?: "any" | "boolean" | "string" | "object" | "record_id";
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface SurrealZodTypeInternals<out O = unknown, out I = unknown>
|
|
18
|
-
extends core.$ZodTypeInternals<O, I> {
|
|
19
|
-
def: SurrealZodTypeDef;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface SurrealZodType<
|
|
23
|
-
out O = unknown,
|
|
24
|
-
out I = unknown,
|
|
25
|
-
out Internals extends SurrealZodTypeInternals<O, I> = SurrealZodTypeInternals<
|
|
26
|
-
O,
|
|
27
|
-
I
|
|
28
|
-
>,
|
|
29
|
-
> extends core.$ZodType<O, I, Internals> {
|
|
30
|
-
_surreal: true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface _SurrealZodType<
|
|
34
|
-
out Internals extends core.$ZodTypeInternals = core.$ZodTypeInternals,
|
|
35
|
-
> extends SurrealZodType<any, any, Internals> {}
|
|
36
|
-
|
|
37
|
-
export const SurrealZodType: core.$constructor<SurrealZodType> =
|
|
38
|
-
core.$constructor("SurrealZodType", (inst, def) => {
|
|
39
|
-
// @ts-expect-error - unknown assertion error
|
|
40
|
-
core.$ZodType.init(inst, def);
|
|
41
|
-
inst._surreal = true;
|
|
42
|
-
return inst;
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
/////////////////////////////////////////////
|
|
46
|
-
/////////////////////////////////////////////
|
|
47
|
-
////////// //////////
|
|
48
|
-
////////// SurrealZodAny //////////
|
|
49
|
-
////////// //////////
|
|
50
|
-
/////////////////////////////////////////////
|
|
51
|
-
/////////////////////////////////////////////
|
|
52
|
-
|
|
53
|
-
export interface SurrealZodAny extends _SurrealZodType<core.$ZodAnyInternals> {}
|
|
54
|
-
export const SurrealZodAny: core.$constructor<SurrealZodAny> =
|
|
55
|
-
core.$constructor("SurrealZodAny", (inst, def) => {
|
|
56
|
-
// @ts-expect-error - unknown assertion error
|
|
57
|
-
core.$ZodAny.init(inst, def);
|
|
58
|
-
SurrealZodType.init(inst, def);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
export function any(): SurrealZodAny {
|
|
62
|
-
return core._any(SurrealZodAny);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/////////////////////////////////////////////////
|
|
66
|
-
/////////////////////////////////////////////////
|
|
67
|
-
////////// //////////
|
|
68
|
-
////////// SurrealZodBoolean //////////
|
|
69
|
-
////////// //////////
|
|
70
|
-
/////////////////////////////////////////////////
|
|
71
|
-
/////////////////////////////////////////////////
|
|
72
|
-
|
|
73
|
-
export interface SurrealZodBoolean
|
|
74
|
-
extends _SurrealZodType<core.$ZodBooleanInternals> {}
|
|
75
|
-
export const SurrealZodBoolean: core.$constructor<SurrealZodBoolean> =
|
|
76
|
-
core.$constructor("SurrealZodBoolean", (inst, def) => {
|
|
77
|
-
// @ts-expect-error - unknown assertion error
|
|
78
|
-
core.$ZodBoolean.init(inst, def);
|
|
79
|
-
SurrealZodType.init(inst, def);
|
|
80
|
-
// const originalDefault = inst.default;
|
|
81
|
-
// inst.default = (defaultValue?: any) => {
|
|
82
|
-
// if (typeof defaultValue === "function") {
|
|
83
|
-
// throw new TypeError(
|
|
84
|
-
// "Functions for default values are not supported in surreal-zod",
|
|
85
|
-
// );
|
|
86
|
-
// }
|
|
87
|
-
// return originalDefault(defaultValue);
|
|
88
|
-
// };
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
export function boolean(
|
|
92
|
-
params?: string | core.$ZodBooleanParams,
|
|
93
|
-
): SurrealZodBoolean {
|
|
94
|
-
return core._boolean(SurrealZodBoolean, params);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
////////////////////////////////////////////////
|
|
98
|
-
////////////////////////////////////////////////
|
|
99
|
-
////////// //////////
|
|
100
|
-
////////// SurrealZodString //////////
|
|
101
|
-
////////// //////////
|
|
102
|
-
////////////////////////////////////////////////
|
|
103
|
-
////////////////////////////////////////////////
|
|
104
|
-
|
|
105
|
-
export interface SurrealZodString
|
|
106
|
-
extends _SurrealZodType<core.$ZodStringInternals<string>> {}
|
|
107
|
-
export const SurrealZodString: core.$constructor<SurrealZodString> =
|
|
108
|
-
core.$constructor("SurrealZodString", (inst, def) => {
|
|
109
|
-
// @ts-expect-error - unknown assertion error
|
|
110
|
-
core.$ZodString.init(inst, def);
|
|
111
|
-
SurrealZodType.init(inst, def);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
export function string(
|
|
115
|
-
params?: string | core.$ZodStringParams,
|
|
116
|
-
): SurrealZodString {
|
|
117
|
-
return core._string(SurrealZodString, params);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
////////////////////////////////////////////////
|
|
121
|
-
////////////////////////////////////////////////
|
|
122
|
-
////////// //////////
|
|
123
|
-
////////// SurrealZodObject //////////
|
|
124
|
-
////////// //////////
|
|
125
|
-
////////////////////////////////////////////////
|
|
126
|
-
////////////////////////////////////////////////
|
|
127
|
-
|
|
128
|
-
export interface SurrealZodObject<
|
|
129
|
-
// @ts-expect-error - unknown assertion error
|
|
130
|
-
out Shape extends core.$ZodShape = core.$ZodLooseShape,
|
|
131
|
-
out Config extends core.$ZodObjectConfig = core.$strip,
|
|
132
|
-
> extends _SurrealZodType<core.$ZodObjectInternals<Shape, Config>>,
|
|
133
|
-
core.$ZodObject<Shape, Config> {}
|
|
134
|
-
|
|
135
|
-
export const SurrealZodObject: core.$constructor<SurrealZodObject> =
|
|
136
|
-
core.$constructor("SurrealZodObject", (inst, def) => {
|
|
137
|
-
// TODO: Inline implementation and use core instead
|
|
138
|
-
// @ts-expect-error - unknown assertion error
|
|
139
|
-
z4.ZodObject.init(inst, def);
|
|
140
|
-
SurrealZodType.init(inst, def);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
export function object<
|
|
144
|
-
T extends core.$ZodLooseShape = Partial<Record<never, core.SomeType>>,
|
|
145
|
-
>(
|
|
146
|
-
shape?: T,
|
|
147
|
-
params?: string | core.$ZodObjectParams,
|
|
148
|
-
): SurrealZodObject<core.util.Writeable<T>, core.$strip> {
|
|
149
|
-
const def: core.$ZodObjectDef = {
|
|
150
|
-
type: "object",
|
|
151
|
-
shape: shape ?? {},
|
|
152
|
-
...core.util.normalizeParams(params),
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
return new SurrealZodObject(def) as any;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
//////////////////////////////////////////////////
|
|
159
|
-
//////////////////////////////////////////////////
|
|
160
|
-
////////// //////////
|
|
161
|
-
////////// SurrealZodRecordId //////////
|
|
162
|
-
////////// //////////
|
|
163
|
-
//////////////////////////////////////////////////
|
|
164
|
-
//////////////////////////////////////////////////
|
|
165
|
-
|
|
166
|
-
// export interface SurrealZodRecordIdDef<
|
|
167
|
-
// out W extends core.util.EnumLike = core.util.EnumLike,
|
|
168
|
-
// // TODO: Remove support for core types?
|
|
169
|
-
// T extends core.$ZodType | SurrealZodType = core.$ZodType | SurrealZodType,
|
|
170
|
-
// > extends SurrealZodTypeDef {
|
|
171
|
-
// type: "record_id";
|
|
172
|
-
// innerType: T;
|
|
173
|
-
// what?: W;
|
|
174
|
-
// }
|
|
175
|
-
|
|
176
|
-
export interface SurrealZodRecordIdDef<
|
|
177
|
-
out W extends { [key: string]: string } = { [key: string]: string },
|
|
178
|
-
// TODO: Remove support for core types?
|
|
179
|
-
T extends core.SomeType = core.SomeType,
|
|
180
|
-
> extends SurrealZodTypeDef {
|
|
181
|
-
surrealType: "record_id";
|
|
182
|
-
innerType: T;
|
|
183
|
-
what?: W;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export interface SurrealZodRecordIdInternals<
|
|
187
|
-
W extends { [key: string]: string } = { [key: string]: string },
|
|
188
|
-
// TODO: Remove support for core types?
|
|
189
|
-
T extends core.SomeType = core.SomeType,
|
|
190
|
-
> extends SurrealZodTypeInternals<RecordId<W[keyof W]>, RecordIdValue> {
|
|
191
|
-
def: SurrealZodRecordIdDef<W, T>;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export interface SurrealZodRecordId<
|
|
195
|
-
W extends { [key: string]: string } = { [key: string]: string },
|
|
196
|
-
T extends core.SomeType = core.SomeType,
|
|
197
|
-
> extends _SurrealZodType<SurrealZodRecordIdInternals<W, T>> {}
|
|
198
|
-
|
|
199
|
-
export const SurrealZodRecordId: core.$constructor<SurrealZodRecordId> =
|
|
200
|
-
core.$constructor("SurrealZodRecordId", (inst, def) => {
|
|
201
|
-
// @ts-expect-error - unknown assertion error
|
|
202
|
-
core.$ZodAny.init(inst, def);
|
|
203
|
-
SurrealZodType.init(inst, def);
|
|
204
|
-
inst._surreal = true;
|
|
205
|
-
inst._zod.parse = (payload, _ctx) => {
|
|
206
|
-
if (payload.value instanceof RecordId) {
|
|
207
|
-
if (def.what) {
|
|
208
|
-
if (def.what[payload.value.table.name] === undefined) {
|
|
209
|
-
payload.issues.push({
|
|
210
|
-
code: "invalid_value",
|
|
211
|
-
values: Object.keys(def.what),
|
|
212
|
-
input: payload.value.table.name,
|
|
213
|
-
message: `Expected record table to be one of ${Object.keys(def.what).join(" | ")} but found ${payload.value.table.name}`,
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
// } else if (typeof payload.value === "string") {
|
|
218
|
-
// let tablePart = '';
|
|
219
|
-
// let idPart = '';
|
|
220
|
-
// let quote = '';
|
|
221
|
-
// for (let i = 0; i < payload.value.length; i++) {
|
|
222
|
-
// const char = payload.value[i];
|
|
223
|
-
// if (char === '`') {
|
|
224
|
-
// if (quote === '`') {
|
|
225
|
-
// tablePart = payload.value.slice(1, i);
|
|
226
|
-
// } else quote = '`';
|
|
227
|
-
// }
|
|
228
|
-
// if (char === ':') {
|
|
229
|
-
// tablePart = payload.value.slice(0, charIndex);
|
|
230
|
-
// idPart = payload.value.slice(tablePart.length + 1);
|
|
231
|
-
// break;
|
|
232
|
-
// }
|
|
233
|
-
// tablePart += char;
|
|
234
|
-
// }
|
|
235
|
-
// for (const char of payload.value.slice(tablePart.length + 1)) {
|
|
236
|
-
// idPart += char;
|
|
237
|
-
// }
|
|
238
|
-
// if (/^(`|\u27e8)/.test(payload.value)) {
|
|
239
|
-
// payload.value = new RecordId(payload.value.split(":")[0], payload.value.split(":")[1]);
|
|
240
|
-
// } else {
|
|
241
|
-
// payload.issues.push({
|
|
242
|
-
// code: 'invalid_format',
|
|
243
|
-
// format: 'record_id',
|
|
244
|
-
// message: 'Invalid record id format',
|
|
245
|
-
// });
|
|
246
|
-
// }
|
|
247
|
-
} else {
|
|
248
|
-
payload.issues.push({
|
|
249
|
-
code: "invalid_type",
|
|
250
|
-
expected: "custom",
|
|
251
|
-
input: payload.value,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return payload;
|
|
256
|
-
};
|
|
257
|
-
return inst;
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
export function recordId<const W extends readonly string[]>(
|
|
261
|
-
what?: W,
|
|
262
|
-
innerType?: core.$ZodType | SurrealZodType,
|
|
263
|
-
): SurrealZodRecordId<core.util.ToEnum<W[number]>> {
|
|
264
|
-
return new SurrealZodRecordId({
|
|
265
|
-
// Zod would not be happy if we have a custom type here, so we use any
|
|
266
|
-
type: "any",
|
|
267
|
-
surrealType: "record_id",
|
|
268
|
-
what: what
|
|
269
|
-
? (Object.freeze(Object.fromEntries(what.map((v) => [v, v]))) as any)
|
|
270
|
-
: undefined,
|
|
271
|
-
innerType: innerType ?? any(),
|
|
272
|
-
}) as any;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// export interface $SurrealZodRecordIdDef extends $ZodTypeDef {
|
|
276
|
-
// type: "recordId";
|
|
277
|
-
// }
|
|
278
|
-
|
|
279
|
-
// export interface _SurrealZodRecordId extends z4._$ZodType {}
|
|
280
|
-
|
|
281
|
-
// /** @internal */
|
|
282
|
-
// export const _SurrealZodRecordId: z4.$constructor<_SurrealZodRecordId> =
|
|
283
|
-
// // z4.$constructor("SurrealZodRecordId", (inst, def) => {});
|
|
284
|
-
// export interface $SurrealZodRecordIdInternals extends $ZodTypeInternals {}
|
|
285
|
-
|
|
286
|
-
// export interface $SurrealZodRecordId<
|
|
287
|
-
// T extends string = string,
|
|
288
|
-
// V extends RecordIdValue = RecordIdValue,
|
|
289
|
-
// > extends $ZodType {
|
|
290
|
-
// _zod: $SurrealZodRecordIdInternals;
|
|
291
|
-
// }
|
|
292
|
-
// // export const recordId = <S extends z4.$ZodType>(schema: S) => {};
|
|
293
|
-
|
|
294
|
-
// export type $SurrealZodTypes = $ZodString | $ZodNumber | $ZodObject;
|
|
295
|
-
// export type $SurrealZodTypeName = $SurrealZodTypes["_zod"]["def"]["type"];
|
|
296
|
-
|
|
297
|
-
export type SurrealZodTypes =
|
|
298
|
-
| SurrealZodAny
|
|
299
|
-
| SurrealZodBoolean
|
|
300
|
-
| SurrealZodString
|
|
301
|
-
| SurrealZodObject
|
|
302
|
-
| SurrealZodRecordId;
|