Skip to content

Commit 5273559

Browse files
committed
✨ Initiated json validator with gql sdl
1 parent fc09c85 commit 5273559

File tree

15 files changed

+1186
-491
lines changed

15 files changed

+1186
-491
lines changed

Diff for: json-schema-generator/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fake.js

Diff for: json-schema-generator/gen.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import tjs from 'typescript-json-schema';
2+
import fs from 'fs';
3+
4+
const types: string[] = ['ConsoleUpdateAppMutationVariables'];
5+
6+
async function fake(files: string[], types: string[] = []) {
7+
const program = tjs.getProgramFromFiles(files, { lib: ['esnext'] });
8+
const validationKeywords = ['faker'];
9+
10+
const generator = tjs.buildGenerator(
11+
program,
12+
{ validationKeywords, required: true, include: files },
13+
files
14+
);
15+
if (!generator) {
16+
throw new Error('Failed to create generator');
17+
}
18+
19+
const userSymbols = generator.getUserSymbols();
20+
21+
const datas: {
22+
[key: string]: tjs.Definition;
23+
} = {};
24+
25+
userSymbols.forEach((sym) => {
26+
if (types.includes(sym)) {
27+
console.log(sym);
28+
const jsonSchema = generator.getSchemaForSymbol(sym);
29+
datas[sym] = jsonSchema;
30+
}
31+
});
32+
33+
return datas;
34+
}
35+
36+
// @ts-ignore
37+
// just to make it work in node
38+
global.location = new URL('https://kloudlite.io');
39+
40+
(async () => {
41+
const data = await fake(['src/generated/gql/server.ts'], types);
42+
43+
console.log(data);
44+
45+
fs.writeFileSync(
46+
'./json-schema-generator/schema.js',
47+
`// Generated file, Generated for validators
48+
49+
const schema = ${JSON.stringify(data, null, 2)};
50+
51+
export default schema;`
52+
);
53+
})();

0 commit comments

Comments
 (0)