UNPKG

surrealdb-zod

Version:

Re-useable zod schema's for use with SurrealDB sdk

275 lines (274 loc) 6.46 kB
import { DateTime as S, Decimal as I, Duration as g, FileRef as x, Future as G, Geometry as P, GeometryPoint as b, GeometryLine as T, GeometryPolygon as M, GeometryMultiPoint as B, GeometryMultiLine as D, GeometryMultiPolygon as E, GeometryCollection as F, RecordIdRange as p, RecordId as f, StringRecordId as L, Table as m, Uuid as O, Range as C, BoundIncluded as l, BoundExcluded as h } from "surrealdb"; import { custom as r, object as y, NEVER as U, undefined as j } from "zod"; function V(e) { return typeof e == "object" ? e === null ? "null" : e.constructor.name : typeof e; } function o({ expected: e, input: n }) { const t = V(n); return `Invalid input: expected ${e}, received ${t}`; } function d(e, n) { return e instanceof l ? _(n) : e instanceof h ? $(n) : j(); } const z = r((e) => e instanceof S, { error: (e) => o({ expected: "DateTime", input: e.input }) }), A = r((e) => e instanceof I, { error: (e) => o({ expected: "Decimal", input: e.input }) }), H = r((e) => e instanceof g, { error: (e) => o({ expected: "Duration", input: e.input }) }), J = r((e) => e instanceof x, { error: (e) => o({ expected: "FileRef", input: e.input }) }), K = r((e) => e instanceof G, { error: (e) => o({ expected: "Future", input: e.input }) }), Q = r((e) => e instanceof P, { error: (e) => o({ expected: "Geometry", input: e.input }) }), W = r((e) => e instanceof b, { error: (e) => o({ expected: "GeometryPoint", input: e.input }) }), X = r((e) => e instanceof T, { error: (e) => o({ expected: "GeometryLine", input: e.input }) }), Y = r((e) => e instanceof M, { error: (e) => o({ expected: "GeometryPolygon", input: e.input }) }), Z = r((e) => e instanceof B, { error: (e) => o({ expected: "GeometryMultiPoint", input: e.input }) }), v = r((e) => e instanceof D, { error: (e) => o({ expected: "GeometryMultiLine", input: e.input }) }), ee = r((e) => e instanceof E, { error: (e) => o({ expected: "GeometryMultiPolygon", input: e.input }) }), ne = r((e) => e instanceof F, { error: (e) => o({ expected: "GeometryCollection", input: e.input }) }); function te(e, n) { return r((t) => t instanceof C, { error: (t) => o({ expected: "Range", input: t.input }) }).superRefine((t, s) => { const u = d(t.begin, e).safeParse(t.begin); if (!u.success) for (const c of u.error.issues) s.addIssue({ ...c, path: ["value", ...c.path] }); const i = d(t.end, n).safeParse(t.end); if (!i.success) for (const c of i.error.issues) s.addIssue({ ...c, path: ["value", ...c.path] }); }); } function _(e) { return r((n) => n instanceof l, { error: (n) => o({ expected: "BoundIncluded", input: n.input }) }).superRefine((n, t) => { const s = e.safeParse(n.value); if (s.success) return U; for (const u of s.error.issues) t.addIssue({ ...u, path: ["value", ...u.path] }); }); } function $(e) { return r((n) => n instanceof h, { error: (n) => o({ expected: "BoundExcluded", input: n.input }) }).superRefine((n, t) => { const s = e.safeParse(n.value); if (!s.success) for (const u of s.error.issues) t.addIssue({ ...u, path: ["value", ...u.path] }); }); } const re = r((e) => e instanceof p, { error: (e) => o({ expected: "RecordIdRange", input: e.input }) }); function oe(e, n) { return r( (t) => t instanceof p, { error: (t) => o({ expected: "RecordIdRange", input: t.input }) } ).superRefine((t, s) => { const u = R(e).safeParse(t.table); if (!u.success) for (const i of u.error.issues) s.addIssue({ ...i, path: ["table", ...i.path] }); if (n) { const i = d(t.begin, n).safeParse(t.begin); if (!i.success) for (const a of i.error.issues) s.addIssue({ ...a, path: ["begin", ...a.path] }); const c = d(t.end, n).safeParse(t.end); if (!c.success) for (const a of c.error.issues) s.addIssue({ ...a, path: ["end", ...a.path] }); } }); } const N = r((e) => e instanceof f, { error: (e) => o({ expected: "RecordId", input: e.input }) }); function k(e, n) { return r((t) => t instanceof f, { error: (t) => o({ expected: "RecordId", input: t.input }) }).superRefine((t, s) => { const u = R(e).safeParse(t.table); if (!u.success) for (const i of u.error.issues) s.addIssue({ ...i, path: ["table", ...i.path] }); if (n) { const i = n.safeParse(t.id); if (!i.success) for (const c of i.error.issues) s.addIssue({ ...c, path: ["table", ...c.path] }); } }); } const se = r((e) => e instanceof L, { error: (e) => o({ expected: "StringRecordId", input: e.input }) }), ue = y({ id: N }); function ie(e, n) { return y({ id: k(e, n) }); } const ce = r((e) => e instanceof m, { error: (e) => o({ expected: "Table", input: e.input }) }); function R(e) { return r((n) => n instanceof m, { error: (n) => o({ expected: "Table", input: n.input }) }).superRefine((n, t) => { n.name !== e && t.addIssue({ code: "invalid_value", values: [e], input: n.name, path: ["name"] }); }); } const ae = r((e) => e instanceof O, { error: (e) => o({ expected: "Uuid", input: e.input }) }); export { $ as BoundExcludedSchema, _ as BoundIncludedSchema, z as DateTimeSchema, A as DecimalSchema, H as DurationSchema, J as FileRefSchema, K as FutureSchema, ne as GeometryCollectionSchema, X as GeometryLineSchema, v as GeometryMultiLineSchema, Z as GeometryMultiPointSchema, ee as GeometryMultiPolygonSchema, W as GeometryPointSchema, Y as GeometryPolygonSchema, Q as GeometrySchema, te as RangeSchema, re as RecordIdRangeSchema, oe as RecordIdRangeSchemaOf, N as RecordIdSchema, k as RecordIdSchemaOf, ue as RecordSchema, ie as RecordSchemaOf, se as StringRecordIdSchema, ce as TableSchema, R as TableSchemaOf, ae as UuidSchema };