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

Generate zod schemas for bridging #1159

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .semgrepignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
website/**

# Just used for build so ignoring
s2zodQuicktypeUtil.js
s2tQuicktypeUtil.js
schemas/api/t2sQuicktypeUtil.js

Expand Down
103 changes: 65 additions & 38 deletions package-lock.json

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

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
],
"scripts": {
"start": "tsdx watch",
"prebuild": "npm run typegen && npm run typegen-bridging",
"prebuild": "npm run typegen && npm run typegen-bridging && npm run typegen-bridging-zod",
"build": "tsdx build",
"test": "tsdx test --verbose",
"lint": "tsdx lint src/api test",
"preprepare": "npm run typegen && npm run typegen-bridging",
"disabled-preprepare": "npm run typegen && npm run typegen-bridging && npm run typegen-bridging-zod",
"prepare": "tsdx build",
"typegen": "node s2tQuicktypeUtil.js schemas/context src/context/ContextTypes.ts && tsdx lint src/context/ --fix",
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts && tsdx lint src/bridging/ --fix"
"typegen-bridging": "node s2tQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingTypes.ts && tsdx lint src/bridging/ --fix",
"typegen-bridging-zod": "node s2zodQuicktypeUtil.js schemas/api schemas/bridging schemas/context/context.schema.json src/bridging/BridgingZodSchemas.ts"
},
"peerDependencies": {},
"husky": {
"hooks": {
"pre-commit": "tsdx lint src test"
Expand All @@ -48,9 +48,12 @@
"devDependencies": {
"husky": "^4.3.0",
"jest-mock-extended": "^1.0.13",
"quicktype": "23.0.78",
"quicktype": "23.0.104",
"tsdx": "^0.14.1",
"tslib": "^2.0.1",
"typescript": "^4.0.3"
},
"dependencies": {
"zod": "^3.22.4"
}
}
57 changes: 57 additions & 0 deletions s2zodQuicktypeUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* SPDX-License-Identifier: Apache-2.0
* Copyright FINOS FDC3 contributors - see NOTICE file
*/

/** Utility for preparing arguments to quicktype, which workaround a specific
* quicktype bug in command line argument handling (where a directory is used
* as input the source language argument is ignored which causes our schemas
* to be interpreted as JSON input, rather than JSONSchema).
* Bug issue:
* */

const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;

const args = process.argv.slice(2);
const outputFile = args.pop();
const inputs = args;

console.log('Inputs: ' + inputs.join(' | '));
console.log('Output file argument: ' + outputFile);

let sources = '';

let dirIndex = 0;

while (dirIndex < inputs.length) {
if (inputs[dirIndex].endsWith('.schema.json')) {
sources += `--src ${path.join(inputs[dirIndex])} `;
} else {
fs.readdirSync(inputs[dirIndex], { withFileTypes: true }).forEach(file => {
if (file.isDirectory()) {
inputs.push(path.join(inputs[dirIndex], file.name));
} else if (file.name.endsWith('.schema.json')) {
sources += `--src ${path.join(inputs[dirIndex], file.name)} `;
}
});
}
dirIndex++;
}

// Normalise path to local quicktype executable.
//const quicktypeExec = ['.', 'node_modules', '.bin', 'quicktype'].join(path.sep);
const quicktypeExec = "node --stack_trace_limit=100 --max-old-space-size=4096 \"../quicktype/dist/index.js\""

const command = `${quicktypeExec} -s schema -l typescript-zod -o ${outputFile} ${sources}`;
console.log('command to run: ' + command);

exec(command, function(error, stdout, stderr) {
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.log(stderr);
}
});
4 changes: 2 additions & 2 deletions src/bridging/BridgingTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,8 @@ export interface ConnectionStep6ConnectedAgentsUpdatePayload {
*/
export interface DesktopAgentImplementationMetadata {
/**
* Used in Desktop Agent Bridging to attribute or target a message to a
* particular Desktop Agent.
* Used in Desktop Agent Bridging to attribute or target a message to a particular Desktop
* Agent.
*/
desktopAgent: string;
/**
Expand Down
Loading
Loading