@supawatch/target-json-schema 0.2.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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +93 -0
- package/package.json +47 -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/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @supawatch/target-json-schema
|
|
2
|
+
|
|
3
|
+
The JSON Schema target for
|
|
4
|
+
[supawatch](https://github.com/omar-dulaimi/supawatch). Emits one draft-07
|
|
5
|
+
schema per table describing the row as the driver returns it, for API docs,
|
|
6
|
+
cross-language validation, and anything else that speaks JSON Schema.
|
|
7
|
+
|
|
8
|
+
Configure it in `supawatch.config.ts` (the CLI loads this package by name):
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
targets: [{ kind: "json-schema", strict: true }]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`strict` (default) sets `additionalProperties: false`. The target carries
|
|
15
|
+
its own Ajv verifier, so generated schemas are ground-truth checked against
|
|
16
|
+
real rows like every validator target.
|
|
17
|
+
|
|
18
|
+
Honest limit: JSON Schema cannot express JS `Date` or `Uint8Array`
|
|
19
|
+
instances, so timestamp and bytea columns emit an accept-anything schema
|
|
20
|
+
with a `$comment` saying why. The alternative, claiming `string`, would
|
|
21
|
+
reject every real driver row.
|
|
22
|
+
|
|
23
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Rendered, Snapshot, Table, Target, TargetCapabilities, TargetOptions, Verifier } from "@supawatch/core";
|
|
2
|
+
export type JsonSchemaTargetOptions = TargetOptions;
|
|
3
|
+
export declare function exportNameFor(table: Table): string;
|
|
4
|
+
export declare class JsonSchemaTarget implements Target<JsonSchemaTargetOptions> {
|
|
5
|
+
readonly name = "json-schema";
|
|
6
|
+
readonly fileExtension = ".schema.json";
|
|
7
|
+
readonly barrel = false;
|
|
8
|
+
readonly capabilities: TargetCapabilities;
|
|
9
|
+
renderTable(table: Table, _snapshot: Snapshot, opts: JsonSchemaTargetOptions): Rendered;
|
|
10
|
+
assembleFile(rendered: Rendered): string;
|
|
11
|
+
verifier(): Verifier;
|
|
12
|
+
}
|
|
13
|
+
export default JsonSchemaTarget;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { Ajv } from "ajv";
|
|
3
|
+
// JSON Schema has no type for JS Date instances or Uint8Array; those
|
|
4
|
+
// columns get an accept-anything schema with a $comment saying why. The
|
|
5
|
+
// honest alternative would be claiming "string", which would reject every
|
|
6
|
+
// real driver row and fail ground truth.
|
|
7
|
+
function schemaFor(runtime) {
|
|
8
|
+
switch (runtime.kind) {
|
|
9
|
+
case "number":
|
|
10
|
+
return runtime.integer ? { type: "integer" } : { type: "number" };
|
|
11
|
+
case "string":
|
|
12
|
+
return { type: "string" };
|
|
13
|
+
case "boolean":
|
|
14
|
+
return { type: "boolean" };
|
|
15
|
+
case "date":
|
|
16
|
+
return { $comment: "JS Date instance at runtime; not expressible in JSON Schema" };
|
|
17
|
+
case "bytes":
|
|
18
|
+
return { $comment: "Uint8Array at runtime; not expressible in JSON Schema" };
|
|
19
|
+
case "json":
|
|
20
|
+
case "unknown":
|
|
21
|
+
return {};
|
|
22
|
+
case "array":
|
|
23
|
+
return { type: "array", items: schemaFor(runtime.element) };
|
|
24
|
+
case "enum":
|
|
25
|
+
return { enum: runtime.labels };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function fieldSchema(col) {
|
|
29
|
+
const base = schemaFor(col.runtime);
|
|
30
|
+
if (!col.nullable)
|
|
31
|
+
return base;
|
|
32
|
+
if (Object.keys(base).length === 0 || !("type" in base) && !("enum" in base)) {
|
|
33
|
+
return base; // accept-anything already includes null
|
|
34
|
+
}
|
|
35
|
+
return { anyOf: [base, { type: "null" }] };
|
|
36
|
+
}
|
|
37
|
+
export function exportNameFor(table) {
|
|
38
|
+
return table.name.replace(/[^a-zA-Z0-9_]/g, "_") + "Row";
|
|
39
|
+
}
|
|
40
|
+
export class JsonSchemaTarget {
|
|
41
|
+
name = "json-schema";
|
|
42
|
+
fileExtension = ".schema.json";
|
|
43
|
+
barrel = false;
|
|
44
|
+
capabilities = {
|
|
45
|
+
strictObjects: true,
|
|
46
|
+
brandedTypes: false,
|
|
47
|
+
dateInstances: false,
|
|
48
|
+
};
|
|
49
|
+
renderTable(table, _snapshot, opts) {
|
|
50
|
+
const strict = opts.strict !== false;
|
|
51
|
+
const properties = {};
|
|
52
|
+
for (const col of table.columns) {
|
|
53
|
+
properties[col.name] = fieldSchema(col);
|
|
54
|
+
}
|
|
55
|
+
const schema = {
|
|
56
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
57
|
+
$id: `${table.schema}.${table.name}`,
|
|
58
|
+
$comment: "Generated by supawatch. Do not edit. Shapes follow driver runtime values.",
|
|
59
|
+
type: "object",
|
|
60
|
+
properties,
|
|
61
|
+
required: table.columns.map((c) => c.name),
|
|
62
|
+
additionalProperties: !strict,
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
imports: [],
|
|
66
|
+
body: JSON.stringify(schema, null, 2) + "\n",
|
|
67
|
+
exportName: exportNameFor(table),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
assembleFile(rendered) {
|
|
71
|
+
return rendered.body;
|
|
72
|
+
}
|
|
73
|
+
verifier() {
|
|
74
|
+
const ajv = new Ajv({ strict: false, allowUnionTypes: true });
|
|
75
|
+
return {
|
|
76
|
+
async load(file, _exportName) {
|
|
77
|
+
const schema = JSON.parse(readFileSync(file, "utf8"));
|
|
78
|
+
return ajv.compile(schema);
|
|
79
|
+
},
|
|
80
|
+
check(schema, value) {
|
|
81
|
+
const validate = schema;
|
|
82
|
+
if (validate(value))
|
|
83
|
+
return { ok: true };
|
|
84
|
+
const first = validate.errors?.[0];
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
reason: first ? `${first.instancePath} ${first.message ?? ""}`.trim() : "invalid",
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export default JsonSchemaTarget;
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supawatch/target-json-schema",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Draft-07 JSON Schemas generated from live Postgres by supawatch, Ajv-verified against real rows.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"supabase",
|
|
7
|
+
"postgres",
|
|
8
|
+
"postgresql",
|
|
9
|
+
"codegen",
|
|
10
|
+
"schema",
|
|
11
|
+
"typescript",
|
|
12
|
+
"json-schema",
|
|
13
|
+
"ajv",
|
|
14
|
+
"validation",
|
|
15
|
+
"openapi"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Omar Dulaimi",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/omar-dulaimi/supawatch.git",
|
|
22
|
+
"directory": "packages/target-json-schema"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"ajv": "^8.17.0",
|
|
38
|
+
"@supawatch/core": "0.2.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^22.20.1",
|
|
42
|
+
"typescript": "^5.9.0"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.json"
|
|
46
|
+
}
|
|
47
|
+
}
|