Skip to content

Commit

Permalink
add docker compose schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sepbot committed Nov 2, 2024
1 parent 365f095 commit a209176
Show file tree
Hide file tree
Showing 6 changed files with 684 additions and 8 deletions.
20 changes: 20 additions & 0 deletions generator/compose.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { writeFile } from "node:fs/promises";

import { compile } from "json-schema-to-typescript";
import { generate } from "ts-to-zod";
import { fetch } from "undici";

// TODO: see if they finally added versioning: https://github.com/compose-spec/compose-spec/issues/104
const SCHEMA_URL = "https://raw.githubusercontent.com/compose-spec/compose-spec/refs/heads/main/schema/compose-spec.json";

// json-schema -> typescript -> zod
// done this way because going to zod directly produces an extremely complex schema
// mostly due to patternProperties abuse
async function main() {
const schema = await (await fetch(SCHEMA_URL)).json();
const ts = await compile(schema, "docker-compose");
const out = generate({ sourceText: ts, getSchemaName: (id) => id[0].toUpperCase() + id.slice(1) + "Schema" });
await writeFile("./src/compose.ts", out.getZodSchemasFile(""));
}

(async () => await main())();
6 changes: 3 additions & 3 deletions generator/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { fetch } from "undici";

import { applyFixes } from "./fixes.mjs";

const SCHEMA_URL = "https://docs.docker.com/reference/api/engine/version/v1.47.yaml"

async function main() {

const schema = YAML.parse(
await (await fetch("https://docs.docker.com/reference/api/engine/version/v1.47.yaml")).text(),
);
const schema = YAML.parse(await (await fetch(SCHEMA_URL)).text());

applyFixes(schema);
await dereference(schema);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"postbuild": "tsc-alias --project tsconfig-build.json",
"format": "biome check --verbose --write --unsafe",
"pregenerate": "rm -rf ./dist ./src",
"generate": "node ./generator/generator.mjs",
"generate": "node ./generator/generator.mjs && node ./generator/compose.mjs",
"postgenerate": "pnpm run format",
"test": "jest",
"prepare": "husky"
Expand All @@ -50,10 +50,12 @@
"@types/ssh2": "^1.15.1",
"husky": "^9.1.6",
"jest": "^29.7.0",
"json-schema-to-typescript": "^15.0.2",
"json-schema-to-zod": "^2.4.1",
"nunjucks": "^3.2.4",
"semver": "^7.6.3",
"ts-jest": "^29.2.5",
"ts-to-zod": "^3.13.0",
"tsc-alias": "^1.8.10",
"typescript": "^5.6.3",
"yaml": "^2.6.0"
Expand Down
Loading

0 comments on commit a209176

Please sign in to comment.