Skip to content

Commit

Permalink
chore: generate v4 json schemas based with zod (#279)
Browse files Browse the repository at this point in the history
* chore: generate v4 json schemas based with zod

* fix: small typo
  • Loading branch information
phanshiyu committed May 6, 2024
1 parent bf462f8 commit 81e1dbe
Show file tree
Hide file tree
Showing 7 changed files with 1,266 additions and 7 deletions.
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"lint": "eslint . --ext .ts,.json --max-warnings 0",
"lint:fix": "npm run lint -- --fix",
"generate-v4-fixtures": "npx ts-node scripts/generateV4JsonFixtures.ts",
"generate-v4-json-schemas": "npx ts-node scripts/generateV4JsonSchemas.ts",
"publish:schema": "./scripts/publishSchema.sh",
"postinstall": "node scripts/postInstall; npm run generate-v4-fixtures",
"prebuild": "node scripts/preBuild"
Expand Down Expand Up @@ -90,7 +91,8 @@
"semantic-release": "^22.0.8",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"zod-to-json-schema": "^3.23.0"
},
"dependencies": {
"@govtechsg/jsonld": "^0.1.0",
Expand Down
36 changes: 36 additions & 0 deletions scripts/generateV4JsonSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from "fs";
import path from "path";
import { zodToJsonSchema } from "zod-to-json-schema";
import { V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "../src/4.0/types";

const OUTPUT_DIR = path.resolve("./src/4.0/jsonSchemas/__generated__");

// make sure the output directory exists
if (fs.existsSync(OUTPUT_DIR)) {
fs.rmSync(OUTPUT_DIR, { recursive: true });
}
fs.mkdirSync(OUTPUT_DIR, { recursive: true });

const ZOD_SCHEMAS = [
{
filename: "v4-document.schema.json",
schemaName: "v4Document",
zodSchema: V4Document,
},
{
filename: "v4-wrapped-document.schema.json",
schemaName: "v4WrappedDocument",
zodSchema: V4WrappedDocument,
},
{
filename: "v4-signed-wrapped-document.schema.json",
schemaName: "v4SignedWrappedDocument",
zodSchema: V4SignedWrappedDocument,
},
];

for (const { filename, zodSchema, schemaName } of ZOD_SCHEMAS) {
const jsonSchema = zodToJsonSchema(zodSchema, schemaName);

fs.writeFileSync(path.join(OUTPUT_DIR, filename), JSON.stringify(jsonSchema, null, 2));
}

0 comments on commit 81e1dbe

Please sign in to comment.