Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: generate v4 json schemas based with zod #279

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
Loading
Loading