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/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
package/lib/surql.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BoundQuery, RecordId, Table } from "surrealdb";
|
|
2
2
|
import * as z4 from "zod/v4/core";
|
|
3
|
-
import type { SurrealZodType } from "./zod";
|
|
3
|
+
import type { SurrealZodType } from "./zod/schema";
|
|
4
4
|
export type ZodTypeName = z4.$ZodType["_zod"]["def"]["type"];
|
|
5
5
|
export type SurrealZodTypeName = SurrealZodType["_zod"]["def"]["type"];
|
|
6
6
|
export interface ZodToSurqlOptions<S extends z4.$ZodObject> {
|
package/lib/surql.js
CHANGED
|
@@ -119,7 +119,24 @@ export function zodTypeToSurrealType(type, parents = [], context) {
|
|
|
119
119
|
const checks = getChecks(schema);
|
|
120
120
|
parseChecks(context.name, checks, context, def.type);
|
|
121
121
|
// console.log(zodToSexpr(type));
|
|
122
|
+
if ("surrealType" in def) {
|
|
123
|
+
switch (def.surrealType) {
|
|
124
|
+
case "record_id":
|
|
125
|
+
if (def.table) {
|
|
126
|
+
return `record<${def.table.map(escapeIdent).join(" | ")}>`;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return "record";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
122
133
|
switch (def.type) {
|
|
134
|
+
case "any":
|
|
135
|
+
case "unknown":
|
|
136
|
+
return "any";
|
|
137
|
+
case "never":
|
|
138
|
+
case "undefined":
|
|
139
|
+
return "NONE";
|
|
123
140
|
case "string":
|
|
124
141
|
return "string";
|
|
125
142
|
case "boolean":
|
|
@@ -144,25 +161,6 @@ export function zodTypeToSurrealType(type, parents = [], context) {
|
|
|
144
161
|
// return "bigint";
|
|
145
162
|
// case "symbol":
|
|
146
163
|
// return "symbol";
|
|
147
|
-
case "any": {
|
|
148
|
-
//===============================
|
|
149
|
-
// Surreal Specific Types
|
|
150
|
-
//===============================
|
|
151
|
-
if ("surrealType" in def) {
|
|
152
|
-
if (def.surrealType === "record_id") {
|
|
153
|
-
if (def.what) {
|
|
154
|
-
return `record<${Object.keys(def.what).join(" | ")}>`;
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
return "record";
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return "any";
|
|
162
|
-
}
|
|
163
|
-
case "undefined": {
|
|
164
|
-
return "NONE";
|
|
165
|
-
}
|
|
166
164
|
case "default": {
|
|
167
165
|
// if (typeof def.defaultValue === "function") {
|
|
168
166
|
// context.default = { value: def.defaultValue(), always: false };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { RecordId, type RecordIdValue } from "surrealdb";
|
|
2
|
+
import * as core from "zod/v4/core";
|
|
3
|
+
export interface SurrealZodTypeDef extends core.$ZodTypeDef {
|
|
4
|
+
surrealType?: "any" | "unknown" | "boolean" | "string" | "object" | "record_id" | "table";
|
|
5
|
+
}
|
|
6
|
+
export interface SurrealZodTypeInternals<out O = unknown, out I = unknown> extends core.$ZodTypeInternals<O, I> {
|
|
7
|
+
def: SurrealZodTypeDef;
|
|
8
|
+
}
|
|
9
|
+
export interface SurrealZodType<out O = unknown, out I = unknown, out Internals extends SurrealZodTypeInternals<O, I> = SurrealZodTypeInternals<O, I>> extends core.$ZodType<O, I, Internals> {
|
|
10
|
+
_surreal: {};
|
|
11
|
+
}
|
|
12
|
+
export interface _SurrealZodType<Internals extends core.$ZodTypeInternals = core.$ZodTypeInternals> extends SurrealZodType<any, any, Internals> {
|
|
13
|
+
}
|
|
14
|
+
export declare const SurrealZodType: core.$constructor<SurrealZodType>;
|
|
15
|
+
export interface SurrealZodAny extends _SurrealZodType<core.$ZodAnyInternals> {
|
|
16
|
+
}
|
|
17
|
+
export declare const SurrealZodAny: core.$constructor<SurrealZodAny>;
|
|
18
|
+
export declare function any(): SurrealZodAny;
|
|
19
|
+
export interface SurrealZodUnknown extends _SurrealZodType<core.$ZodUnknownInternals> {
|
|
20
|
+
}
|
|
21
|
+
export declare const SurrealZodUnknown: core.$constructor<SurrealZodUnknown>;
|
|
22
|
+
export declare function unknown(): SurrealZodUnknown;
|
|
23
|
+
export interface SurrealZodNever extends _SurrealZodType<core.$ZodNeverInternals> {
|
|
24
|
+
}
|
|
25
|
+
export declare const SurrealZodNever: core.$constructor<SurrealZodNever>;
|
|
26
|
+
export declare function never(params?: string | core.$ZodNeverParams): SurrealZodNever;
|
|
27
|
+
export interface SurrealZodBoolean extends _SurrealZodType<core.$ZodBooleanInternals> {
|
|
28
|
+
}
|
|
29
|
+
export declare const SurrealZodBoolean: core.$constructor<SurrealZodBoolean>;
|
|
30
|
+
export declare function boolean(params?: string | core.$ZodBooleanParams): SurrealZodBoolean;
|
|
31
|
+
export interface SurrealZodString extends _SurrealZodType<core.$ZodStringInternals<string>> {
|
|
32
|
+
}
|
|
33
|
+
export declare const SurrealZodString: core.$constructor<SurrealZodString>;
|
|
34
|
+
export declare function string(params?: string | core.$ZodStringParams): SurrealZodString;
|
|
35
|
+
export interface SurrealZodObject<out Shape extends core.$ZodShape = core.$ZodLooseShape, out Config extends core.$ZodObjectConfig = core.$strip> extends _SurrealZodType<core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
|
|
36
|
+
}
|
|
37
|
+
export declare const SurrealZodObject: core.$constructor<SurrealZodObject>;
|
|
38
|
+
export declare function object<T extends core.$ZodLooseShape = Partial<Record<never, core.SomeType>>>(shape?: T, params?: string | core.$ZodObjectParams): SurrealZodObject<core.util.Writeable<T>, core.$strip>;
|
|
39
|
+
export type SurrealZodRecordIdValue = core.$ZodAny | core.$ZodUnknown | core.$ZodString | core.$ZodNumber | core.$ZodObject | core.$ZodArray;
|
|
40
|
+
export interface SurrealZodRecordIdDef<Table extends string = string, Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue> extends SurrealZodTypeDef {
|
|
41
|
+
surrealType: "record_id";
|
|
42
|
+
innerType: Id;
|
|
43
|
+
table?: Table[];
|
|
44
|
+
}
|
|
45
|
+
export type SurrealZodRecordIdValueOutput<T> = T extends {
|
|
46
|
+
_zod: {
|
|
47
|
+
output: any;
|
|
48
|
+
};
|
|
49
|
+
} ? T["_zod"]["output"] : RecordIdValue;
|
|
50
|
+
export interface SurrealZodRecordIdInternals<Table extends string = string, Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue> extends SurrealZodTypeInternals<RecordId<Table, SurrealZodRecordIdValueOutput<Id>>, RecordIdValue> {
|
|
51
|
+
def: SurrealZodRecordIdDef<Table, Id>;
|
|
52
|
+
}
|
|
53
|
+
export interface SurrealZodRecordId<Table extends string = string, Id extends SurrealZodRecordIdValue = SurrealZodRecordIdValue> extends _SurrealZodType<SurrealZodRecordIdInternals<Table, Id>> {
|
|
54
|
+
table<NewTable extends string | string[]>(table: NewTable): SurrealZodRecordId<NewTable extends string ? NewTable : NewTable[number], Id>;
|
|
55
|
+
type<NewType extends SurrealZodRecordIdValue>(innerType: NewType): SurrealZodRecordId<Table, NewType>;
|
|
56
|
+
}
|
|
57
|
+
export declare const SurrealZodRecordId: core.$constructor<SurrealZodRecordId>;
|
|
58
|
+
export declare function recordId<const W extends string | string[]>(what?: W, innerType?: SurrealZodRecordIdValue): SurrealZodRecordId<W extends string ? W : W[number]>;
|
|
59
|
+
export type SurrealZodTableFields = {
|
|
60
|
+
readonly [key: string]: SurrealZodType;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Normalizes the fields of a table schema to include the id field if it is not present.
|
|
64
|
+
* If the id field is present, it will be normalized using the table name and the inner type.
|
|
65
|
+
*/
|
|
66
|
+
export type NormalizedFields<Name extends string = string, Fields extends SurrealZodTableFields = SurrealZodTableFields> = Fields extends {
|
|
67
|
+
id: SurrealZodType;
|
|
68
|
+
} ? Fields["id"] extends SurrealZodRecordId<infer _N, infer T> ? Omit<Fields, "id"> & {
|
|
69
|
+
id: SurrealZodRecordId<Name, T>;
|
|
70
|
+
} : Fields["id"] extends SurrealZodRecordIdValue ? Omit<Fields, "id"> & {
|
|
71
|
+
id: SurrealZodRecordId<Name, Fields["id"]>;
|
|
72
|
+
} : Omit<Fields, "id"> & {
|
|
73
|
+
id: SurrealZodRecordId<Name>;
|
|
74
|
+
} : Fields & {
|
|
75
|
+
id: SurrealZodRecordId<Name>;
|
|
76
|
+
};
|
|
77
|
+
export interface SurrealZodTableDef<Name extends string = string, Fields extends SurrealZodTableFields = SurrealZodTableFields> extends SurrealZodTypeDef {
|
|
78
|
+
surrealType: "table";
|
|
79
|
+
name: Name;
|
|
80
|
+
fields: NormalizedFields<Name, Fields>;
|
|
81
|
+
comment?: string;
|
|
82
|
+
catchall?: SurrealZodType;
|
|
83
|
+
}
|
|
84
|
+
export interface SurrealZodTableInternals<Name extends string = string, Fields extends SurrealZodTableFields = SurrealZodTableFields> extends SurrealZodTypeInternals {
|
|
85
|
+
def: SurrealZodTableDef<Name, Fields>;
|
|
86
|
+
}
|
|
87
|
+
export interface SurrealZodTable<out Name extends string = string, out Fields extends SurrealZodTableFields = SurrealZodTableFields> extends _SurrealZodType<SurrealZodTableInternals<Name, Fields>> {
|
|
88
|
+
name<NewName extends string>(name: NewName): SurrealZodTable<NewName, Fields>;
|
|
89
|
+
fields<NewFields extends SurrealZodTableFields>(fields: NewFields): SurrealZodTable<Name, NewFields>;
|
|
90
|
+
comment(comment: string): this;
|
|
91
|
+
schemafull(): this;
|
|
92
|
+
schemaless(): this;
|
|
93
|
+
record(): this["_zod"]["def"]["fields"]["id"];
|
|
94
|
+
}
|
|
95
|
+
export declare const SurrealZodTable: core.$constructor<SurrealZodTable>;
|
|
96
|
+
export declare function table<Name extends string = string>(name: Name): SurrealZodTable<Name>;
|
|
97
|
+
export type SurrealZodTypes = SurrealZodAny | SurrealZodBoolean | SurrealZodString | SurrealZodObject | SurrealZodRecordId | SurrealZodTable;
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { escapeIdent, RecordId, Surreal, } from "surrealdb";
|
|
2
|
+
import z4, { normalize } from "zod/v4";
|
|
3
|
+
import * as core from "zod/v4/core";
|
|
4
|
+
export const SurrealZodType = core.$constructor("SurrealZodType", (inst, def) => {
|
|
5
|
+
// @ts-expect-error - unknown assertion error
|
|
6
|
+
core.$ZodType.init(inst, def);
|
|
7
|
+
inst._surreal = {};
|
|
8
|
+
inst;
|
|
9
|
+
return inst;
|
|
10
|
+
});
|
|
11
|
+
export const SurrealZodAny = core.$constructor("SurrealZodAny", (inst, def) => {
|
|
12
|
+
// @ts-expect-error - unknown assertion error
|
|
13
|
+
core.$ZodAny.init(inst, def);
|
|
14
|
+
SurrealZodType.init(inst, def);
|
|
15
|
+
});
|
|
16
|
+
export function any() {
|
|
17
|
+
return core._any(SurrealZodAny);
|
|
18
|
+
}
|
|
19
|
+
export const SurrealZodUnknown = core.$constructor("SurrealZodUnknown", (inst, def) => {
|
|
20
|
+
// @ts-expect-error - unknown assertion error
|
|
21
|
+
core.$ZodUnknown.init(inst, def);
|
|
22
|
+
SurrealZodType.init(inst, def);
|
|
23
|
+
});
|
|
24
|
+
export function unknown() {
|
|
25
|
+
return core._unknown(SurrealZodUnknown);
|
|
26
|
+
}
|
|
27
|
+
export const SurrealZodNever = core.$constructor("SurrealZodNever", (inst, def) => {
|
|
28
|
+
// @ts-expect-error - unknown assertion error
|
|
29
|
+
core.$ZodNever.init(inst, def);
|
|
30
|
+
SurrealZodType.init(inst, def);
|
|
31
|
+
});
|
|
32
|
+
export function never(params) {
|
|
33
|
+
return core._never(SurrealZodNever, params);
|
|
34
|
+
}
|
|
35
|
+
export const SurrealZodBoolean = core.$constructor("SurrealZodBoolean", (inst, def) => {
|
|
36
|
+
// @ts-expect-error - unknown assertion error
|
|
37
|
+
core.$ZodBoolean.init(inst, def);
|
|
38
|
+
SurrealZodType.init(inst, def);
|
|
39
|
+
// const originalDefault = inst.default;
|
|
40
|
+
// inst.default = (defaultValue?: any) => {
|
|
41
|
+
// if (typeof defaultValue === "function") {
|
|
42
|
+
// throw new TypeError(
|
|
43
|
+
// "Functions for default values are not supported in surreal-zod",
|
|
44
|
+
// );
|
|
45
|
+
// }
|
|
46
|
+
// return originalDefault(defaultValue);
|
|
47
|
+
// };
|
|
48
|
+
});
|
|
49
|
+
export function boolean(params) {
|
|
50
|
+
return core._boolean(SurrealZodBoolean, params);
|
|
51
|
+
}
|
|
52
|
+
export const SurrealZodString = core.$constructor("SurrealZodString", (inst, def) => {
|
|
53
|
+
// @ts-expect-error - unknown assertion error
|
|
54
|
+
core.$ZodString.init(inst, def);
|
|
55
|
+
SurrealZodType.init(inst, def);
|
|
56
|
+
});
|
|
57
|
+
export function string(params) {
|
|
58
|
+
return core._string(SurrealZodString, params);
|
|
59
|
+
}
|
|
60
|
+
export const SurrealZodObject = core.$constructor("SurrealZodObject", (inst, def) => {
|
|
61
|
+
// TODO: Inline implementation and use core instead
|
|
62
|
+
// @ts-expect-error - unknown assertion error
|
|
63
|
+
z4.ZodObject.init(inst, def);
|
|
64
|
+
SurrealZodType.init(inst, def);
|
|
65
|
+
});
|
|
66
|
+
export function object(shape, params) {
|
|
67
|
+
const def = {
|
|
68
|
+
type: "object",
|
|
69
|
+
shape: shape ?? {},
|
|
70
|
+
...core.util.normalizeParams(params),
|
|
71
|
+
};
|
|
72
|
+
return new SurrealZodObject(def);
|
|
73
|
+
}
|
|
74
|
+
export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst, def) => {
|
|
75
|
+
SurrealZodType.init(inst, def);
|
|
76
|
+
inst._surreal = true;
|
|
77
|
+
inst.table = (table) => {
|
|
78
|
+
return new SurrealZodRecordId({
|
|
79
|
+
...def,
|
|
80
|
+
table: Array.isArray(table) ? table : [table],
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
inst.type = (innerType) => {
|
|
84
|
+
return new SurrealZodRecordId({
|
|
85
|
+
...def,
|
|
86
|
+
innerType,
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
inst._zod.parse = (payload, ctx) => {
|
|
90
|
+
if (payload.value instanceof RecordId) {
|
|
91
|
+
if (def.table && !def.table.includes(payload.value.table.name)) {
|
|
92
|
+
payload.issues.push({
|
|
93
|
+
code: "invalid_value",
|
|
94
|
+
values: def.table,
|
|
95
|
+
input: payload.value.table.name,
|
|
96
|
+
message: def.table.length > 1
|
|
97
|
+
? `Expected RecordId's table to be one of ${def.table.map(escapeIdent).join(" | ")} but found ${payload.value.table.name}`
|
|
98
|
+
: `Expected RecordId's table to be ${def.table[0]} but found ${payload.value.table.name}`,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (def.innerType) {
|
|
102
|
+
const schema = def.innerType._zod;
|
|
103
|
+
const result = schema.run({ value: payload.value.id, issues: [] }, ctx);
|
|
104
|
+
if (result instanceof Promise) {
|
|
105
|
+
return result.then((result) => {
|
|
106
|
+
if (result.issues.length) {
|
|
107
|
+
payload.issues.push(...core.util.prefixIssues("id", result.issues));
|
|
108
|
+
}
|
|
109
|
+
payload.value = new RecordId(payload.value.table.name, result.value);
|
|
110
|
+
return payload;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else if (result.issues.length) {
|
|
114
|
+
payload.issues.push(...core.util.prefixIssues("id", result.issues));
|
|
115
|
+
}
|
|
116
|
+
payload.value = new RecordId(payload.value.table.name, result.value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
payload.issues.push({
|
|
121
|
+
code: "invalid_type",
|
|
122
|
+
// TODO: Surreal specific issues
|
|
123
|
+
expected: "custom",
|
|
124
|
+
input: payload.value,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return payload;
|
|
128
|
+
};
|
|
129
|
+
return inst;
|
|
130
|
+
});
|
|
131
|
+
export function recordId(what, innerType) {
|
|
132
|
+
return new SurrealZodRecordId({
|
|
133
|
+
// Zod would not be happy if we have a custom type here, so we use any
|
|
134
|
+
type: "any",
|
|
135
|
+
surrealType: "record_id",
|
|
136
|
+
table: what ? (Array.isArray(what) ? what : [what]) : undefined,
|
|
137
|
+
innerType: innerType ?? any(),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function handleFieldResult(result, final, field, input) {
|
|
141
|
+
if (result.issues.length) {
|
|
142
|
+
final.issues.push(...core.util.prefixIssues(field, result.issues));
|
|
143
|
+
}
|
|
144
|
+
if (result.value === undefined) {
|
|
145
|
+
if (field in input) {
|
|
146
|
+
// @ts-expect-error: field not index-checked on final.value
|
|
147
|
+
final.value[field] = undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// @ts-expect-error: field not index-checked on final.value
|
|
152
|
+
final.value[field] = result.value;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function handleCatchall(promises, input, payload, ctx, def, inst) {
|
|
156
|
+
const unrecognized = [];
|
|
157
|
+
const known = def.fieldNamesSet;
|
|
158
|
+
// biome-ignore lint/style/noNonNullAssertion: already asserted
|
|
159
|
+
const _catchall = def.catchall._zod;
|
|
160
|
+
const type = _catchall.def.type;
|
|
161
|
+
for (const field in input) {
|
|
162
|
+
if (known.has(field))
|
|
163
|
+
continue;
|
|
164
|
+
if (type === "never") {
|
|
165
|
+
unrecognized.push(field);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const result = _catchall.run({ value: input[field], issues: [] }, ctx);
|
|
169
|
+
if (result instanceof Promise) {
|
|
170
|
+
promises.push(result.then((result) => handleFieldResult(result, payload, field, input)));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
handleFieldResult(result, payload, field, input);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (unrecognized.length) {
|
|
177
|
+
payload.issues.push({
|
|
178
|
+
code: "unrecognized_keys",
|
|
179
|
+
keys: unrecognized,
|
|
180
|
+
input,
|
|
181
|
+
inst,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (!promises.length)
|
|
185
|
+
return payload;
|
|
186
|
+
return Promise.all(promises).then(() => payload);
|
|
187
|
+
}
|
|
188
|
+
function normalizeTableDef(def) {
|
|
189
|
+
const fields = Object.keys(def.fields);
|
|
190
|
+
for (const field of fields) {
|
|
191
|
+
if (!def.fields[field]?._zod.traits.has("SurrealZodType")) {
|
|
192
|
+
throw new Error(`Invalid field definition for "${field}": expected a Surreal Zod schema`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (def.fields.id) {
|
|
196
|
+
if (def.fields.id instanceof SurrealZodRecordId) {
|
|
197
|
+
def.fields.id = def.fields.id.table(def.name);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
def.fields.id = recordId(def.name).type(def.fields.id);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
...def,
|
|
205
|
+
fields: def.fields,
|
|
206
|
+
fieldNames: fields,
|
|
207
|
+
fieldNamesSet: new Set(fields),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export const SurrealZodTable = core.$constructor("SurrealZodTable", (inst, def) => {
|
|
211
|
+
SurrealZodType.init(inst, def);
|
|
212
|
+
const normalized = core.util.cached(() => normalizeTableDef(def));
|
|
213
|
+
const catchall = def.catchall;
|
|
214
|
+
let value;
|
|
215
|
+
inst.name = (name) => {
|
|
216
|
+
return new SurrealZodTable({
|
|
217
|
+
...def,
|
|
218
|
+
name,
|
|
219
|
+
// biome-ignore lint/suspicious/noExplicitAny: false-positive
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
inst.fields = (fields) => {
|
|
223
|
+
return new SurrealZodTable({
|
|
224
|
+
...def,
|
|
225
|
+
fields: {
|
|
226
|
+
id: recordId(def.name),
|
|
227
|
+
...fields,
|
|
228
|
+
},
|
|
229
|
+
// biome-ignore lint/suspicious/noExplicitAny: false-positive
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
inst.comment = (comment) => {
|
|
233
|
+
return new SurrealZodTable({
|
|
234
|
+
...def,
|
|
235
|
+
comment,
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
inst.schemafull = () => {
|
|
239
|
+
return new SurrealZodTable({
|
|
240
|
+
...def,
|
|
241
|
+
catchall: never(),
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
inst.schemaless = () => {
|
|
245
|
+
return new SurrealZodTable({
|
|
246
|
+
...def,
|
|
247
|
+
catchall: unknown(),
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
inst.record = () => normalized.value.fields.id;
|
|
251
|
+
inst._zod.parse = (payload, ctx) => {
|
|
252
|
+
value ??= normalized.value;
|
|
253
|
+
const input = payload.value;
|
|
254
|
+
if (!core.util.isObject(input)) {
|
|
255
|
+
payload.issues.push({
|
|
256
|
+
expected: "object",
|
|
257
|
+
code: "invalid_type",
|
|
258
|
+
input,
|
|
259
|
+
inst,
|
|
260
|
+
});
|
|
261
|
+
return payload;
|
|
262
|
+
}
|
|
263
|
+
payload.value = {};
|
|
264
|
+
const promises = [];
|
|
265
|
+
const fields = value.fields;
|
|
266
|
+
for (const field of value.fieldNames) {
|
|
267
|
+
// biome-ignore lint/style/noNonNullAssertion: bounds already checked
|
|
268
|
+
const schema = fields[field];
|
|
269
|
+
const result = schema._zod.run({ value: input[field], issues: [] }, ctx);
|
|
270
|
+
if (result instanceof Promise) {
|
|
271
|
+
promises.push(result.then((result) => {
|
|
272
|
+
handleFieldResult(result, payload, field, input);
|
|
273
|
+
}));
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
handleFieldResult(result, payload, field, input);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (!catchall) {
|
|
280
|
+
return promises.length
|
|
281
|
+
? Promise.all(promises).then(() => payload)
|
|
282
|
+
: payload;
|
|
283
|
+
}
|
|
284
|
+
return handleCatchall(promises, input, payload, ctx, normalized.value, inst);
|
|
285
|
+
};
|
|
286
|
+
return inst;
|
|
287
|
+
});
|
|
288
|
+
export function table(name) {
|
|
289
|
+
return new SurrealZodTable({
|
|
290
|
+
type: "any",
|
|
291
|
+
surrealType: "table",
|
|
292
|
+
name,
|
|
293
|
+
fields: {
|
|
294
|
+
id: recordId(name),
|
|
295
|
+
},
|
|
296
|
+
catchall: unknown(),
|
|
297
|
+
});
|
|
298
|
+
}
|
package/lib/zod/utils.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "surreal-zod",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.6",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "tsc",
|
|
6
6
|
"prepack": "bun run build"
|
|
@@ -32,6 +32,6 @@
|
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@surrealdb/node": "2.3.4",
|
|
34
34
|
"dedent": "^1.7.0",
|
|
35
|
-
"surrealdb": "
|
|
35
|
+
"surrealdb": "github:msanchezdev/surrealdb.js#typed-record-ids:packages/sdk"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/src/index.ts
CHANGED
package/src/surql.ts
CHANGED
|
@@ -10,7 +10,7 @@ import * as z4 from "zod/v4/core";
|
|
|
10
10
|
import z from "zod";
|
|
11
11
|
import dedent from "dedent";
|
|
12
12
|
import type sz from ".";
|
|
13
|
-
import type { SurrealZodType, SurrealZodTypes } from "./zod";
|
|
13
|
+
import type { SurrealZodType, SurrealZodTypes } from "./zod/schema";
|
|
14
14
|
|
|
15
15
|
export type ZodTypeName = z4.$ZodType["_zod"]["def"]["type"];
|
|
16
16
|
export type SurrealZodTypeName = SurrealZodType["_zod"]["def"]["type"];
|
|
@@ -209,7 +209,24 @@ export function zodTypeToSurrealType(
|
|
|
209
209
|
parseChecks(context.name, checks, context, def.type);
|
|
210
210
|
// console.log(zodToSexpr(type));
|
|
211
211
|
|
|
212
|
+
if ("surrealType" in def) {
|
|
213
|
+
switch (def.surrealType) {
|
|
214
|
+
case "record_id":
|
|
215
|
+
if (def.table) {
|
|
216
|
+
return `record<${def.table.map(escapeIdent).join(" | ")}>`;
|
|
217
|
+
} else {
|
|
218
|
+
return "record";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
212
223
|
switch (def.type) {
|
|
224
|
+
case "any":
|
|
225
|
+
case "unknown":
|
|
226
|
+
return "any";
|
|
227
|
+
case "never":
|
|
228
|
+
case "undefined":
|
|
229
|
+
return "NONE";
|
|
213
230
|
case "string":
|
|
214
231
|
return "string";
|
|
215
232
|
case "boolean":
|
|
@@ -234,24 +251,7 @@ export function zodTypeToSurrealType(
|
|
|
234
251
|
// return "bigint";
|
|
235
252
|
// case "symbol":
|
|
236
253
|
// return "symbol";
|
|
237
|
-
|
|
238
|
-
//===============================
|
|
239
|
-
// Surreal Specific Types
|
|
240
|
-
//===============================
|
|
241
|
-
if ("surrealType" in def) {
|
|
242
|
-
if (def.surrealType === "record_id") {
|
|
243
|
-
if (def.what) {
|
|
244
|
-
return `record<${Object.keys(def.what).join(" | ")}>`;
|
|
245
|
-
} else {
|
|
246
|
-
return "record";
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return "any";
|
|
251
|
-
}
|
|
252
|
-
case "undefined": {
|
|
253
|
-
return "NONE";
|
|
254
|
-
}
|
|
254
|
+
|
|
255
255
|
case "default": {
|
|
256
256
|
// if (typeof def.defaultValue === "function") {
|
|
257
257
|
// context.default = { value: def.defaultValue(), always: false };
|