@supawatch/target-valibot 0.1.1
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/LICENSE +21 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +177 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Omar Dulaimi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Rendered, Snapshot, Table, Target, TargetCapabilities, TargetOptions, Verifier } from "@supawatch/core";
|
|
2
|
+
export interface ValibotTargetOptions extends TargetOptions {
|
|
3
|
+
strict?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function exportNameFor(table: Table): string;
|
|
6
|
+
export declare class ValibotTarget implements Target<ValibotTargetOptions> {
|
|
7
|
+
readonly name = "valibot";
|
|
8
|
+
readonly fileExtension = ".mjs";
|
|
9
|
+
readonly capabilities: TargetCapabilities;
|
|
10
|
+
renderTable(table: Table, _snapshot: Snapshot, opts: ValibotTargetOptions): Rendered;
|
|
11
|
+
renderTypes(table: Table, _snapshot: Snapshot, opts: ValibotTargetOptions): string;
|
|
12
|
+
verifier(): Verifier;
|
|
13
|
+
}
|
|
14
|
+
export default ValibotTarget;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
function valibotExpr(runtime) {
|
|
4
|
+
switch (runtime.kind) {
|
|
5
|
+
case "number":
|
|
6
|
+
return runtime.integer
|
|
7
|
+
? "v.pipe(v.number(), v.integer())"
|
|
8
|
+
: "v.number()";
|
|
9
|
+
case "string":
|
|
10
|
+
return runtime.format === "uuid"
|
|
11
|
+
? "v.pipe(v.string(), v.uuid())"
|
|
12
|
+
: "v.string()";
|
|
13
|
+
case "boolean":
|
|
14
|
+
return "v.boolean()";
|
|
15
|
+
case "date":
|
|
16
|
+
return "v.date()";
|
|
17
|
+
case "bytes":
|
|
18
|
+
return "v.instance(Uint8Array)";
|
|
19
|
+
case "json":
|
|
20
|
+
case "unknown":
|
|
21
|
+
return "v.unknown()";
|
|
22
|
+
case "array":
|
|
23
|
+
return `v.array(${valibotExpr(runtime.element)})`;
|
|
24
|
+
case "enum": {
|
|
25
|
+
const labels = runtime.labels.map((l) => JSON.stringify(l)).join(", ");
|
|
26
|
+
return `v.picklist([${labels}])`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function tsType(runtime) {
|
|
31
|
+
switch (runtime.kind) {
|
|
32
|
+
case "number":
|
|
33
|
+
return "number";
|
|
34
|
+
case "string":
|
|
35
|
+
return "string";
|
|
36
|
+
case "boolean":
|
|
37
|
+
return "boolean";
|
|
38
|
+
case "date":
|
|
39
|
+
return "Date";
|
|
40
|
+
case "bytes":
|
|
41
|
+
return "Uint8Array";
|
|
42
|
+
case "json":
|
|
43
|
+
case "unknown":
|
|
44
|
+
return "unknown";
|
|
45
|
+
case "array": {
|
|
46
|
+
const el = tsType(runtime.element);
|
|
47
|
+
return el.includes("|") ? `(${el})[]` : `${el}[]`;
|
|
48
|
+
}
|
|
49
|
+
case "enum":
|
|
50
|
+
return runtime.labels.map((l) => JSON.stringify(l)).join(" | ");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function exportNameFor(table) {
|
|
54
|
+
return table.name.replace(/[^a-zA-Z0-9_]/g, "_") + "Row";
|
|
55
|
+
}
|
|
56
|
+
function baseNameFor(table) {
|
|
57
|
+
return table.name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
58
|
+
}
|
|
59
|
+
function fieldSchema(col) {
|
|
60
|
+
const base = valibotExpr(col.runtime);
|
|
61
|
+
return col.nullable ? `v.nullable(${base})` : base;
|
|
62
|
+
}
|
|
63
|
+
function jsonOverrideFor(table, col, jsonTypes) {
|
|
64
|
+
if (!jsonTypes || col.runtime.kind !== "json")
|
|
65
|
+
return undefined;
|
|
66
|
+
return (jsonTypes[`${table.schema}.${table.name}.${col.name}`] ??
|
|
67
|
+
jsonTypes[`${table.name}.${col.name}`]);
|
|
68
|
+
}
|
|
69
|
+
function writableColumns(table) {
|
|
70
|
+
return table.columns.filter((c) => !c.generated && c.identity !== "always");
|
|
71
|
+
}
|
|
72
|
+
function insertOptional(col) {
|
|
73
|
+
return col.hasDefault || col.nullable || col.identity === "default";
|
|
74
|
+
}
|
|
75
|
+
function variantBody(table, ctor, name, optionalWhen) {
|
|
76
|
+
const fields = writableColumns(table)
|
|
77
|
+
.map((col) => {
|
|
78
|
+
let s = fieldSchema(col);
|
|
79
|
+
if (optionalWhen(col))
|
|
80
|
+
s = `v.optional(${s})`;
|
|
81
|
+
return ` ${JSON.stringify(col.name)}: ${s},`;
|
|
82
|
+
})
|
|
83
|
+
.join("\n");
|
|
84
|
+
return `export const ${name} = ${ctor}({\n${fields}\n});`;
|
|
85
|
+
}
|
|
86
|
+
export class ValibotTarget {
|
|
87
|
+
name = "valibot";
|
|
88
|
+
fileExtension = ".mjs";
|
|
89
|
+
capabilities = {
|
|
90
|
+
strictObjects: true,
|
|
91
|
+
brandedTypes: true,
|
|
92
|
+
dateInstances: true,
|
|
93
|
+
};
|
|
94
|
+
renderTable(table, _snapshot, opts) {
|
|
95
|
+
const strict = opts.strict !== false;
|
|
96
|
+
const ctor = strict ? "v.strictObject" : "v.object";
|
|
97
|
+
const fields = table.columns
|
|
98
|
+
.map((col) => ` ${JSON.stringify(col.name)}: ${fieldSchema(col)},`)
|
|
99
|
+
.join("\n");
|
|
100
|
+
const parts = [
|
|
101
|
+
`export const ${exportNameFor(table)} = ${ctor}({\n${fields}\n});`,
|
|
102
|
+
];
|
|
103
|
+
if (opts.emit?.insert && table.kind === "table") {
|
|
104
|
+
parts.push(variantBody(table, ctor, `${baseNameFor(table)}Insert`, insertOptional));
|
|
105
|
+
}
|
|
106
|
+
if (opts.emit?.update && table.kind === "table") {
|
|
107
|
+
parts.push(variantBody(table, ctor, `${baseNameFor(table)}Update`, () => true));
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
imports: [{ from: "valibot", namespace: "v" }],
|
|
111
|
+
body: parts.join("\n\n"),
|
|
112
|
+
exportName: exportNameFor(table),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
renderTypes(table, _snapshot, opts) {
|
|
116
|
+
const name = exportNameFor(table);
|
|
117
|
+
const base = baseNameFor(table);
|
|
118
|
+
const rowType = `${base}RowType`;
|
|
119
|
+
const typeOf = (col) => {
|
|
120
|
+
const override = jsonOverrideFor(table, col, opts.jsonTypes);
|
|
121
|
+
const b = override ?? tsType(col.runtime);
|
|
122
|
+
return col.nullable && b !== "unknown" ? `${b} | null` : b;
|
|
123
|
+
};
|
|
124
|
+
const fields = table.columns
|
|
125
|
+
.map((col) => ` ${JSON.stringify(col.name)}: ${typeOf(col)};`)
|
|
126
|
+
.join("\n");
|
|
127
|
+
const lines = [
|
|
128
|
+
"// Generated by supawatch. Do not edit.",
|
|
129
|
+
'import type { GenericSchema } from "valibot";',
|
|
130
|
+
"",
|
|
131
|
+
`export type ${rowType} = {`,
|
|
132
|
+
fields,
|
|
133
|
+
"};",
|
|
134
|
+
`export declare const ${name}: GenericSchema<unknown, ${rowType}>;`,
|
|
135
|
+
];
|
|
136
|
+
const variantTypes = (typeName, exportName, optionalWhen) => {
|
|
137
|
+
const vf = writableColumns(table)
|
|
138
|
+
.map((col) => {
|
|
139
|
+
const opt = optionalWhen(col) ? "?" : "";
|
|
140
|
+
return ` ${JSON.stringify(col.name)}${opt}: ${typeOf(col)};`;
|
|
141
|
+
})
|
|
142
|
+
.join("\n");
|
|
143
|
+
lines.push("", `export type ${typeName} = {`, vf, "};");
|
|
144
|
+
lines.push(`export declare const ${exportName}: GenericSchema<unknown, ${typeName}>;`);
|
|
145
|
+
};
|
|
146
|
+
if (opts.emit?.insert && table.kind === "table") {
|
|
147
|
+
variantTypes(`${base}InsertType`, `${base}Insert`, insertOptional);
|
|
148
|
+
}
|
|
149
|
+
if (opts.emit?.update && table.kind === "table") {
|
|
150
|
+
variantTypes(`${base}UpdateType`, `${base}Update`, () => true);
|
|
151
|
+
}
|
|
152
|
+
lines.push("");
|
|
153
|
+
return lines.join("\n");
|
|
154
|
+
}
|
|
155
|
+
verifier() {
|
|
156
|
+
return {
|
|
157
|
+
async load(file, exportName) {
|
|
158
|
+
const url = pathToFileURL(file).href + `?v=${Date.now()}`;
|
|
159
|
+
const mod = (await import(url));
|
|
160
|
+
const schema = mod[exportName];
|
|
161
|
+
if (!schema) {
|
|
162
|
+
throw new Error(`no export ${exportName} in ${file}; exports: ${Object.keys(mod).join(",")}`);
|
|
163
|
+
}
|
|
164
|
+
return schema;
|
|
165
|
+
},
|
|
166
|
+
check(schema, value) {
|
|
167
|
+
// Uses this package's own valibot to run a schema built by the
|
|
168
|
+
// consumer's copy; safe within the same major.
|
|
169
|
+
const r = v.safeParse(schema, value);
|
|
170
|
+
if (r.success)
|
|
171
|
+
return { ok: true };
|
|
172
|
+
return { ok: false, reason: r.issues[0]?.message };
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
export default ValibotTarget;
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supawatch/target-valibot",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Omar Dulaimi",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/omar-dulaimi/supawatch.git",
|
|
9
|
+
"directory": "packages/target-valibot"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"valibot": "^1.1.0",
|
|
25
|
+
"@supawatch/core": "0.1.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"valibot": "^1.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^22.20.1",
|
|
32
|
+
"typescript": "^5.9.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p tsconfig.json"
|
|
36
|
+
}
|
|
37
|
+
}
|