@supawatch/target-valibot 0.7.1 → 0.9.0

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/dist/index.d.ts CHANGED
@@ -2,13 +2,13 @@ import type { Rendered, Snapshot, Table, Target, TargetCapabilities, TargetOptio
2
2
  export interface ValibotTargetOptions extends TargetOptions {
3
3
  strict?: boolean;
4
4
  }
5
- export declare function exportNameFor(table: Table): string;
5
+ export declare function exportNameFor(table: Table, snapshot: Snapshot): string;
6
6
  export declare class ValibotTarget implements Target<ValibotTargetOptions> {
7
7
  readonly name = "valibot";
8
8
  readonly fileExtension = ".mjs";
9
9
  readonly capabilities: TargetCapabilities;
10
- renderTable(table: Table, _snapshot: Snapshot, opts: ValibotTargetOptions): Rendered;
11
- renderTypes(table: Table, _snapshot: Snapshot, opts: ValibotTargetOptions): string;
10
+ renderTable(table: Table, snapshot: Snapshot, opts: ValibotTargetOptions): Rendered;
11
+ renderTypes(table: Table, snapshot: Snapshot, opts: ValibotTargetOptions): string;
12
12
  verifier(): Verifier;
13
13
  }
14
14
  export default ValibotTarget;
package/dist/index.js CHANGED
@@ -1,11 +1,14 @@
1
1
  import { pathToFileURL } from "node:url";
2
2
  import * as v from "valibot";
3
+ import { exportBaseName, fileBaseName } from "@supawatch/core";
3
4
  function valibotExpr(runtime) {
4
5
  switch (runtime.kind) {
5
6
  case "number":
7
+ // Floats can really be NaN (measured); v.number() rejects it,
8
+ // while +-Infinity already passes.
6
9
  return runtime.integer
7
10
  ? "v.pipe(v.number(), v.integer())"
8
- : "v.number()";
11
+ : "v.union([v.number(), v.nan()])";
9
12
  case "string":
10
13
  return runtime.format === "uuid"
11
14
  ? "v.pipe(v.string(), v.uuid())"
@@ -13,7 +16,9 @@ function valibotExpr(runtime) {
13
16
  case "boolean":
14
17
  return "v.boolean()";
15
18
  case "date":
16
- return "v.date()";
19
+ // Invalid Date instances are real driver output for timestamp
20
+ // 'infinity' and BC values; v.date() rejects them.
21
+ return "v.instance(Date)";
17
22
  case "bytes":
18
23
  return "v.instance(Uint8Array)";
19
24
  case "json":
@@ -22,6 +27,8 @@ function valibotExpr(runtime) {
22
27
  case "array":
23
28
  return `v.array(${valibotExpr(runtime.element)})`;
24
29
  case "enum": {
30
+ if (runtime.labels.length === 0)
31
+ return "v.never()";
25
32
  const labels = runtime.labels.map((l) => JSON.stringify(l)).join(", ");
26
33
  return `v.picklist([${labels}])`;
27
34
  }
@@ -47,14 +54,16 @@ function tsType(runtime) {
47
54
  return el.includes("|") ? `(${el})[]` : `${el}[]`;
48
55
  }
49
56
  case "enum":
57
+ if (runtime.labels.length === 0)
58
+ return "never";
50
59
  return runtime.labels.map((l) => JSON.stringify(l)).join(" | ");
51
60
  }
52
61
  }
53
- export function exportNameFor(table) {
54
- return table.name.replace(/[^a-zA-Z0-9_]/g, "_") + "Row";
62
+ export function exportNameFor(table, snapshot) {
63
+ return exportBaseName(table, snapshot) + "Row";
55
64
  }
56
- function baseNameFor(table) {
57
- return table.name.replace(/[^a-zA-Z0-9_]/g, "_");
65
+ function baseNameFor(table, snapshot) {
66
+ return exportBaseName(table, snapshot);
58
67
  }
59
68
  function fieldSchema(col) {
60
69
  const base = valibotExpr(col.runtime);
@@ -91,30 +100,30 @@ export class ValibotTarget {
91
100
  brandedTypes: true,
92
101
  dateInstances: true,
93
102
  };
94
- renderTable(table, _snapshot, opts) {
103
+ renderTable(table, snapshot, opts) {
95
104
  const strict = opts.strict !== false;
96
105
  const ctor = strict ? "v.strictObject" : "v.object";
97
106
  const fields = table.columns
98
107
  .map((col) => ` ${JSON.stringify(col.name)}: ${fieldSchema(col)},`)
99
108
  .join("\n");
100
109
  const parts = [
101
- `export const ${exportNameFor(table)} = ${ctor}({\n${fields}\n});`,
110
+ `export const ${exportNameFor(table, snapshot)} = ${ctor}({\n${fields}\n});`,
102
111
  ];
103
112
  if (opts.emit?.insert && table.kind === "table") {
104
- parts.push(variantBody(table, ctor, `${baseNameFor(table)}Insert`, insertOptional));
113
+ parts.push(variantBody(table, ctor, `${baseNameFor(table, snapshot)}Insert`, insertOptional));
105
114
  }
106
115
  if (opts.emit?.update && table.kind === "table") {
107
- parts.push(variantBody(table, ctor, `${baseNameFor(table)}Update`, () => true));
116
+ parts.push(variantBody(table, ctor, `${baseNameFor(table, snapshot)}Update`, () => true));
108
117
  }
109
118
  return {
110
119
  imports: [{ from: "valibot", namespace: "v" }],
111
120
  body: parts.join("\n\n"),
112
- exportName: exportNameFor(table),
121
+ exportName: exportNameFor(table, snapshot),
113
122
  };
114
123
  }
115
- renderTypes(table, _snapshot, opts) {
116
- const name = exportNameFor(table);
117
- const base = baseNameFor(table);
124
+ renderTypes(table, snapshot, opts) {
125
+ const name = exportNameFor(table, snapshot);
126
+ const base = baseNameFor(table, snapshot);
118
127
  const rowType = `${base}RowType`;
119
128
  const typeOf = (col) => {
120
129
  const override = jsonOverrideFor(table, col, opts.jsonTypes);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supawatch/target-valibot",
3
- "version": "0.7.1",
3
+ "version": "0.9.0",
4
4
  "description": "Valibot schemas generated from live Postgres by supawatch, mapped to what the driver actually returns.",
5
5
  "keywords": [
6
6
  "supabase",
@@ -33,7 +33,7 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "valibot": "^1.1.0",
36
- "@supawatch/core": "0.7.1"
36
+ "@supawatch/core": "0.9.0"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "valibot": "^1.0.0"