diff --git a/package-lock.json b/package-lock.json index 0ae4183..e1cf0e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "BSD-3-Clause", "dependencies": { "@jupyterlite/contents": "^0.4.1", - "comlink": "^4.4.1" + "comlink": "^4.4.1", + "zod": "^3.23.8" }, "devDependencies": { "@rspack/cli": "^1.0.3", @@ -1552,6 +1553,7 @@ "version": "8.17.1", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", "integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==", + "license": "MIT", "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", @@ -6338,6 +6340,15 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "3.23.8", + "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz", + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/package.json b/package.json index 245daf5..2060d53 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ }, "dependencies": { "@jupyterlite/contents": "^0.4.1", - "comlink": "^4.4.1" + "comlink": "^4.4.1", + "zod": "^3.23.8" }, "devDependencies": { "@rspack/cli": "^1.0.3", diff --git a/src/tools/prepare_wasm.ts b/src/tools/prepare_wasm.ts index ae39509..86f26fb 100644 --- a/src/tools/prepare_wasm.ts +++ b/src/tools/prepare_wasm.ts @@ -11,6 +11,7 @@ const fs = require('node:fs'); const path = require('node:path'); const { execSync } = require('node:child_process'); +const zod = require('zod'); /* eslint-enable */ const ENV_NAME = 'cockle_wasm_env'; @@ -39,6 +40,26 @@ if (fs.existsSync(otherConfigFilename)) { cockleConfig = cockleConfig.concat(extraConfig); } +// Validate input schema, raising ZodError if fails. +const inputSchema = zod.array( + zod + .object({ + package: zod.string(), + modules: zod.optional( + zod.array( + zod + .object({ + name: zod.string(), + commands: zod.optional(zod.string()) + }) + .strict() + ) + ) + }) + .strict() +); +inputSchema.parse(cockleConfig); + // Required emscripten-wasm32 packages. const packageNames = cockleConfig.map((item: any) => item.package); console.log('Required package names', packageNames); @@ -87,6 +108,28 @@ for (const packageConfig of cockleConfig) { } } +// Validate output schema, raising ZodError if fails. +const outputSchema = zod.array( + zod + .object({ + package: zod.string(), + build_string: zod.string(), + platform: zod.string(), + version: zod.string(), + channel: zod.string(), + modules: zod.array( + zod + .object({ + name: zod.string(), + commands: zod.string() + }) + .strict() + ) + }) + .strict() +); +outputSchema.parse(cockleConfig); + // Output config file. let targetConfigFile = 'cockle-config.json'; if (wantCopy) {