Skip to content

Commit

Permalink
Validate JSON config files
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Oct 22, 2024
1 parent b5c02a6 commit 287cff3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions src/tools/prepare_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 287cff3

Please sign in to comment.