surreal-zod 0.0.0-alpha.7 → 0.0.0-alpha.9
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/package.json +1 -1
- package/lib/zod.d.ts +0 -55
- package/lib/zod.js +0 -126
package/package.json
CHANGED
package/lib/zod.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
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" | "boolean" | "string" | "object" | "record_id";
|
|
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: true;
|
|
11
|
-
}
|
|
12
|
-
export interface _SurrealZodType<out 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 SurrealZodBoolean extends _SurrealZodType<core.$ZodBooleanInternals> {
|
|
20
|
-
}
|
|
21
|
-
export declare const SurrealZodBoolean: core.$constructor<SurrealZodBoolean>;
|
|
22
|
-
export declare function boolean(params?: string | core.$ZodBooleanParams): SurrealZodBoolean;
|
|
23
|
-
export interface SurrealZodString extends _SurrealZodType<core.$ZodStringInternals<string>> {
|
|
24
|
-
}
|
|
25
|
-
export declare const SurrealZodString: core.$constructor<SurrealZodString>;
|
|
26
|
-
export declare function string(params?: string | core.$ZodStringParams): SurrealZodString;
|
|
27
|
-
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> {
|
|
28
|
-
}
|
|
29
|
-
export declare const SurrealZodObject: core.$constructor<SurrealZodObject>;
|
|
30
|
-
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>;
|
|
31
|
-
export interface SurrealZodRecordIdDef<out W extends {
|
|
32
|
-
[key: string]: string;
|
|
33
|
-
} = {
|
|
34
|
-
[key: string]: string;
|
|
35
|
-
}, T extends core.SomeType = core.SomeType> extends SurrealZodTypeDef {
|
|
36
|
-
surrealType: "record_id";
|
|
37
|
-
innerType: T;
|
|
38
|
-
what?: W;
|
|
39
|
-
}
|
|
40
|
-
export interface SurrealZodRecordIdInternals<W extends {
|
|
41
|
-
[key: string]: string;
|
|
42
|
-
} = {
|
|
43
|
-
[key: string]: string;
|
|
44
|
-
}, T extends core.SomeType = core.SomeType> extends SurrealZodTypeInternals<RecordId<W[keyof W]>, RecordIdValue> {
|
|
45
|
-
def: SurrealZodRecordIdDef<W, T>;
|
|
46
|
-
}
|
|
47
|
-
export interface SurrealZodRecordId<W extends {
|
|
48
|
-
[key: string]: string;
|
|
49
|
-
} = {
|
|
50
|
-
[key: string]: string;
|
|
51
|
-
}, T extends core.SomeType = core.SomeType> extends _SurrealZodType<SurrealZodRecordIdInternals<W, T>> {
|
|
52
|
-
}
|
|
53
|
-
export declare const SurrealZodRecordId: core.$constructor<SurrealZodRecordId>;
|
|
54
|
-
export declare function recordId<const W extends readonly string[]>(what?: W, innerType?: core.$ZodType | SurrealZodType): SurrealZodRecordId<core.util.ToEnum<W[number]>>;
|
|
55
|
-
export type SurrealZodTypes = SurrealZodAny | SurrealZodBoolean | SurrealZodString | SurrealZodObject | SurrealZodRecordId;
|
package/lib/zod.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { RecordId } from "surrealdb";
|
|
2
|
-
import z4 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 = true;
|
|
8
|
-
return inst;
|
|
9
|
-
});
|
|
10
|
-
export const SurrealZodAny = core.$constructor("SurrealZodAny", (inst, def) => {
|
|
11
|
-
// @ts-expect-error - unknown assertion error
|
|
12
|
-
core.$ZodAny.init(inst, def);
|
|
13
|
-
SurrealZodType.init(inst, def);
|
|
14
|
-
});
|
|
15
|
-
export function any() {
|
|
16
|
-
return core._any(SurrealZodAny);
|
|
17
|
-
}
|
|
18
|
-
export const SurrealZodBoolean = core.$constructor("SurrealZodBoolean", (inst, def) => {
|
|
19
|
-
// @ts-expect-error - unknown assertion error
|
|
20
|
-
core.$ZodBoolean.init(inst, def);
|
|
21
|
-
SurrealZodType.init(inst, def);
|
|
22
|
-
// const originalDefault = inst.default;
|
|
23
|
-
// inst.default = (defaultValue?: any) => {
|
|
24
|
-
// if (typeof defaultValue === "function") {
|
|
25
|
-
// throw new TypeError(
|
|
26
|
-
// "Functions for default values are not supported in surreal-zod",
|
|
27
|
-
// );
|
|
28
|
-
// }
|
|
29
|
-
// return originalDefault(defaultValue);
|
|
30
|
-
// };
|
|
31
|
-
});
|
|
32
|
-
export function boolean(params) {
|
|
33
|
-
return core._boolean(SurrealZodBoolean, params);
|
|
34
|
-
}
|
|
35
|
-
export const SurrealZodString = core.$constructor("SurrealZodString", (inst, def) => {
|
|
36
|
-
// @ts-expect-error - unknown assertion error
|
|
37
|
-
core.$ZodString.init(inst, def);
|
|
38
|
-
SurrealZodType.init(inst, def);
|
|
39
|
-
});
|
|
40
|
-
export function string(params) {
|
|
41
|
-
return core._string(SurrealZodString, params);
|
|
42
|
-
}
|
|
43
|
-
export const SurrealZodObject = core.$constructor("SurrealZodObject", (inst, def) => {
|
|
44
|
-
// TODO: Inline implementation and use core instead
|
|
45
|
-
// @ts-expect-error - unknown assertion error
|
|
46
|
-
z4.ZodObject.init(inst, def);
|
|
47
|
-
SurrealZodType.init(inst, def);
|
|
48
|
-
});
|
|
49
|
-
export function object(shape, params) {
|
|
50
|
-
const def = {
|
|
51
|
-
type: "object",
|
|
52
|
-
shape: shape ?? {},
|
|
53
|
-
...core.util.normalizeParams(params),
|
|
54
|
-
};
|
|
55
|
-
return new SurrealZodObject(def);
|
|
56
|
-
}
|
|
57
|
-
export const SurrealZodRecordId = core.$constructor("SurrealZodRecordId", (inst, def) => {
|
|
58
|
-
// @ts-expect-error - unknown assertion error
|
|
59
|
-
core.$ZodAny.init(inst, def);
|
|
60
|
-
SurrealZodType.init(inst, def);
|
|
61
|
-
inst._surreal = true;
|
|
62
|
-
inst._zod.parse = (payload, _ctx) => {
|
|
63
|
-
if (payload.value instanceof RecordId) {
|
|
64
|
-
if (def.what) {
|
|
65
|
-
if (def.what[payload.value.table.name] === undefined) {
|
|
66
|
-
payload.issues.push({
|
|
67
|
-
code: "invalid_value",
|
|
68
|
-
values: Object.keys(def.what),
|
|
69
|
-
input: payload.value.table.name,
|
|
70
|
-
message: `Expected record table to be one of ${Object.keys(def.what).join(" | ")} but found ${payload.value.table.name}`,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// } else if (typeof payload.value === "string") {
|
|
75
|
-
// let tablePart = '';
|
|
76
|
-
// let idPart = '';
|
|
77
|
-
// let quote = '';
|
|
78
|
-
// for (let i = 0; i < payload.value.length; i++) {
|
|
79
|
-
// const char = payload.value[i];
|
|
80
|
-
// if (char === '`') {
|
|
81
|
-
// if (quote === '`') {
|
|
82
|
-
// tablePart = payload.value.slice(1, i);
|
|
83
|
-
// } else quote = '`';
|
|
84
|
-
// }
|
|
85
|
-
// if (char === ':') {
|
|
86
|
-
// tablePart = payload.value.slice(0, charIndex);
|
|
87
|
-
// idPart = payload.value.slice(tablePart.length + 1);
|
|
88
|
-
// break;
|
|
89
|
-
// }
|
|
90
|
-
// tablePart += char;
|
|
91
|
-
// }
|
|
92
|
-
// for (const char of payload.value.slice(tablePart.length + 1)) {
|
|
93
|
-
// idPart += char;
|
|
94
|
-
// }
|
|
95
|
-
// if (/^(`|\u27e8)/.test(payload.value)) {
|
|
96
|
-
// payload.value = new RecordId(payload.value.split(":")[0], payload.value.split(":")[1]);
|
|
97
|
-
// } else {
|
|
98
|
-
// payload.issues.push({
|
|
99
|
-
// code: 'invalid_format',
|
|
100
|
-
// format: 'record_id',
|
|
101
|
-
// message: 'Invalid record id format',
|
|
102
|
-
// });
|
|
103
|
-
// }
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
payload.issues.push({
|
|
107
|
-
code: "invalid_type",
|
|
108
|
-
expected: "custom",
|
|
109
|
-
input: payload.value,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
return payload;
|
|
113
|
-
};
|
|
114
|
-
return inst;
|
|
115
|
-
});
|
|
116
|
-
export function recordId(what, innerType) {
|
|
117
|
-
return new SurrealZodRecordId({
|
|
118
|
-
// Zod would not be happy if we have a custom type here, so we use any
|
|
119
|
-
type: "any",
|
|
120
|
-
surrealType: "record_id",
|
|
121
|
-
what: what
|
|
122
|
-
? Object.freeze(Object.fromEntries(what.map((v) => [v, v])))
|
|
123
|
-
: undefined,
|
|
124
|
-
innerType: innerType ?? any(),
|
|
125
|
-
});
|
|
126
|
-
}
|