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

Validate JSON config files #63

Merged
merged 1 commit into from
Oct 22, 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
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
Loading