ts-runtime-validation 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/dist/SchemaGenerator.js +20 -18
- package/dist/SchemaGenerator.js.map +1 -1
- package/package.json +1 -1
- package/src/SchemaGenerator.ts +22 -19
package/README.md
CHANGED
|
@@ -6,11 +6,11 @@ Get bulletproof type validation based off typescript interfaces without any extr
|
|
|
6
6
|
|
|
7
7
|
## How?
|
|
8
8
|
|
|
9
|
-
This is a code generator that is designed to run as a yarn / npm script. By default scans your source directory for files ending in the provided glob pattern. By default: `*.jsonschema.{ts,tsx}`. The output will create three files:
|
|
9
|
+
This is a code generator that is designed to run as a yarn / npm script. By default scans your source directory for files ending in the provided glob pattern. It will generate types based off exported type aliases and typescript interfaces. By default: `*.jsonschema.{ts,tsx}`. The output will create three files:
|
|
10
10
|
|
|
11
11
|
1. `./src/ts-runtime-validation/validation.schema.json` - containing the [jsonschema](http://json-schema.org/) types
|
|
12
12
|
1. `./src/SchemaDefinition.ts` - containing the typescript.
|
|
13
|
-
1. `./src/
|
|
13
|
+
1. `./src/isValidSchema.ts` - containing a type guard type inferring helper method (intended for consumption in your code base - examples below)
|
|
14
14
|
|
|
15
15
|
## Footnote
|
|
16
16
|
|
|
@@ -35,10 +35,13 @@ Options:
|
|
|
35
35
|
## Example usage of generated ts type validation
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
|
-
import { isValidSchema } from "./.ts-runtime-validation/
|
|
38
|
+
import { isValidSchema } from "./.ts-runtime-validation/isValidSchema"; // this is autogenerated by the CLI as a helper file
|
|
39
39
|
|
|
40
40
|
if (isValidSchema(data, "#/definitions/ITypeA")) {
|
|
41
41
|
// variable: data in this block will have typings for ITypeA
|
|
42
|
+
} else {
|
|
43
|
+
// type is invalid and not known here
|
|
44
|
+
throw new Error("Failed to validate payload");
|
|
42
45
|
}
|
|
43
46
|
```
|
|
44
47
|
|
package/dist/SchemaGenerator.js
CHANGED
|
@@ -62,7 +62,7 @@ class SchemaGenerator {
|
|
|
62
62
|
this.outputPath = path_1.default.join(this.options.rootPath, this.options.output);
|
|
63
63
|
this.jsonSchemaOutputFile = path_1.default.join(this.options.rootPath, this.options.output, validationSchemaFileName);
|
|
64
64
|
this.tsSchemaDefinitionOutputFile = path_1.default.join(this.options.rootPath, this.options.output, schemaDefinitionFileName);
|
|
65
|
-
this.isValidSchemaOutputFile = path_1.default.join(this.options.rootPath, this.options.output, "
|
|
65
|
+
this.isValidSchemaOutputFile = path_1.default.join(this.options.rootPath, this.options.output, "isValidSchema.ts");
|
|
66
66
|
this.Generate = () => __awaiter(this, void 0, void 0, function* () {
|
|
67
67
|
const { helpers, glob } = this.options;
|
|
68
68
|
const fileList = yield this.getMatchingFiles();
|
|
@@ -71,10 +71,9 @@ class SchemaGenerator {
|
|
|
71
71
|
console.log(`Aborting - no files found with glob: ${glob}`);
|
|
72
72
|
return;
|
|
73
73
|
}
|
|
74
|
-
const fileSchemas = yield this.
|
|
75
|
-
console.log(`Generating ${fileSchemas.size} validation schema(s)`);
|
|
74
|
+
const fileSchemas = yield this.getJsonSchemasForFiles(fileList);
|
|
76
75
|
if (fileSchemas.size === 0) {
|
|
77
|
-
console.log(`Aborting - no
|
|
76
|
+
console.log(`Aborting - no types found: ${glob}`);
|
|
78
77
|
return;
|
|
79
78
|
}
|
|
80
79
|
this.writeSchemaMapToValidationSchema(fileSchemas);
|
|
@@ -98,7 +97,7 @@ class SchemaGenerator {
|
|
|
98
97
|
});
|
|
99
98
|
return api.withPromise();
|
|
100
99
|
});
|
|
101
|
-
this.
|
|
100
|
+
this.getJsonSchemasForFiles = (filesList) => __awaiter(this, void 0, void 0, function* () {
|
|
102
101
|
const { additionalProperties } = this.options;
|
|
103
102
|
const schemaMap = new Map();
|
|
104
103
|
filesList.forEach((file) => {
|
|
@@ -107,8 +106,7 @@ class SchemaGenerator {
|
|
|
107
106
|
type: "*",
|
|
108
107
|
additionalProperties,
|
|
109
108
|
encodeRefs: false,
|
|
110
|
-
|
|
111
|
-
expose: "export",
|
|
109
|
+
sortProps: true,
|
|
112
110
|
};
|
|
113
111
|
const schemaGenerator = tsj.createGenerator(config);
|
|
114
112
|
const fileSchemas = schemaGenerator.createSchema(config.type);
|
|
@@ -148,6 +146,7 @@ class SchemaGenerator {
|
|
|
148
146
|
};
|
|
149
147
|
this.writeSchemaMapToValidationTypes = (schemaMap) => __awaiter(this, void 0, void 0, function* () {
|
|
150
148
|
const project = new ts_morph_1.Project(defaultTsMorphProjectSettings);
|
|
149
|
+
const readerProject = new ts_morph_1.Project(defaultTsMorphProjectSettings);
|
|
151
150
|
const symbols = [];
|
|
152
151
|
const importMap = new Map();
|
|
153
152
|
schemaMap.forEach((schema, filePath) => {
|
|
@@ -157,12 +156,18 @@ class SchemaGenerator {
|
|
|
157
156
|
const relativeFilePath = path_1.default.relative(this.outputPath, dir);
|
|
158
157
|
const importPath = `${relativeFilePath}/${fileWithoutExtension}`;
|
|
159
158
|
const defs = (_a = schema.definitions) !== null && _a !== void 0 ? _a : {};
|
|
159
|
+
const readerSourceFile = readerProject.addSourceFileAtPath(filePath);
|
|
160
160
|
Object.keys(defs).forEach((symbol) => {
|
|
161
161
|
var _a;
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
const typeAlias = readerSourceFile.getTypeAlias(symbol);
|
|
163
|
+
const typeInterface = readerSourceFile.getInterface(symbol);
|
|
164
|
+
const hasTypeOrInterface = (typeAlias !== null && typeAlias !== void 0 ? typeAlias : typeInterface) !== undefined;
|
|
165
|
+
if (hasTypeOrInterface) {
|
|
166
|
+
const namedImports = (_a = importMap.get(importPath)) !== null && _a !== void 0 ? _a : [];
|
|
167
|
+
namedImports.push(symbol);
|
|
168
|
+
importMap.set(importPath, namedImports);
|
|
169
|
+
symbols.push(symbol);
|
|
170
|
+
}
|
|
166
171
|
});
|
|
167
172
|
});
|
|
168
173
|
const sourceFile = project.createSourceFile(this.tsSchemaDefinitionOutputFile, {}, defaultCreateFileOptions);
|
|
@@ -170,6 +175,7 @@ class SchemaGenerator {
|
|
|
170
175
|
sourceFile.addImportDeclaration({ namedImports, moduleSpecifier: importPath });
|
|
171
176
|
});
|
|
172
177
|
sourceFile.addVariableStatement({
|
|
178
|
+
isExported: true,
|
|
173
179
|
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
|
|
174
180
|
declarations: [
|
|
175
181
|
{
|
|
@@ -188,14 +194,11 @@ class SchemaGenerator {
|
|
|
188
194
|
sourceFile.addInterface({
|
|
189
195
|
kind: ts_morph_1.StructureKind.Interface,
|
|
190
196
|
name: "ISchema",
|
|
191
|
-
isExported:
|
|
197
|
+
isExported: true,
|
|
192
198
|
properties: symbols.map((symbol) => {
|
|
193
199
|
return { name: `readonly ["#/definitions/${symbol}"]`, type: symbol };
|
|
194
200
|
}),
|
|
195
201
|
});
|
|
196
|
-
sourceFile.addExportDeclaration({
|
|
197
|
-
namedExports: ["schemas", "ISchema"],
|
|
198
|
-
});
|
|
199
202
|
yield project.save();
|
|
200
203
|
});
|
|
201
204
|
this.writeValidatorFunction = () => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -208,6 +211,7 @@ class SchemaGenerator {
|
|
|
208
211
|
moduleSpecifier: `./${path_1.default.parse(schemaDefinitionFileName).name}`,
|
|
209
212
|
});
|
|
210
213
|
sourceFile.addVariableStatement({
|
|
214
|
+
isExported: true,
|
|
211
215
|
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
|
|
212
216
|
declarations: [
|
|
213
217
|
{
|
|
@@ -221,6 +225,7 @@ class SchemaGenerator {
|
|
|
221
225
|
});
|
|
222
226
|
sourceFile.addVariableStatement({
|
|
223
227
|
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
|
|
228
|
+
isExported: true,
|
|
224
229
|
declarations: [
|
|
225
230
|
{
|
|
226
231
|
name: "isValidSchema",
|
|
@@ -233,9 +238,6 @@ class SchemaGenerator {
|
|
|
233
238
|
},
|
|
234
239
|
],
|
|
235
240
|
});
|
|
236
|
-
sourceFile.addExportDeclaration({
|
|
237
|
-
namedExports: ["validator", "isValidSchema"],
|
|
238
|
-
});
|
|
239
241
|
yield project.save();
|
|
240
242
|
});
|
|
241
243
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaGenerator.js","sourceRoot":"","sources":["../src/SchemaGenerator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA4B;AAE5B,4CAAoB;AACpB,0DAAkC;AAClC,gDAAwB;AACxB,uCAUkB;AAClB,8DAAgD;AAGhD,MAAM,6BAA6B,GAAmB;IAClD,oBAAoB,EAAE;QAClB,eAAe,EAAE,0BAAe,CAAC,UAAU;QAC3C,WAAW,EAAE,sBAAW,CAAC,QAAQ;QACjC,SAAS,EAAE,oBAAS,CAAC,MAAM;QAC3B,+BAA+B,EAAE,KAAK;QACtC,iBAAiB,EAAE,IAAI;KAC1B;CACJ,CAAC;AAEF,MAAM,wBAAwB,GAA4B;IACtD,SAAS,EAAE,IAAI;CAClB,CAAC;AAEF,MAAM,wBAAwB,GAAG,wBAAwB,CAAC;AAC1D,MAAM,wBAAwB,GAAG,qBAAqB,CAAC;AAEvD,MAAa,eAAe;IAMxB,YAA2B,OAAwB;QAAxB,YAAO,GAAP,OAAO,CAAiB;QAL3C,eAAU,GAAG,cAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACnE,yBAAoB,GAAG,cAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,wBAAwB,CAAC,CAAC;QACvG,iCAA4B,GAAG,cAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,O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AAC,2BAA2B,CAAC,CAAC;wBAClD,CAAC;qBACJ;iBACJ;aACJ,CAAC,CAAC;YAEH,UAAU,CAAC,oBAAoB,CAAC;gBAC5B,eAAe,EAAE,kCAAuB,CAAC,KAAK;gBAC9C,UAAU,EAAE,IAAI;gBAChB,YAAY,EAAE;oBACV;wBACI,IAAI,EAAE,eAAe;wBACrB,WAAW,EAAE,CAAC,MAAuB,EAAE,EAAE;4BACrC,MAAM,CAAC,SAAS,CAAC,2FAA2F,CAAC,CAAC;4BAC9G,MAAM,CAAC,SAAS,CAAC,mDAAmD,CAAC,CAAC;4BACtE,MAAM,CAAC,SAAS,CAAC,6CAA6C,CAAC,CAAC;4BAChE,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;wBAC1B,CAAC;qBACJ;iBACJ;aACJ,CAAC,CAAC;YACH,MAAM,OAAO,CAAC,IAAI,EAAE,CAAC;QACzB,CAAC,CAAA,CAAC;IApMoD,CAAC;CAqM1D;AA3MD,0CA2MC"}
|
package/package.json
CHANGED
package/src/SchemaGenerator.ts
CHANGED
|
@@ -38,7 +38,7 @@ export class SchemaGenerator {
|
|
|
38
38
|
private outputPath = path.join(this.options.rootPath, this.options.output);
|
|
39
39
|
private jsonSchemaOutputFile = path.join(this.options.rootPath, this.options.output, validationSchemaFileName);
|
|
40
40
|
private tsSchemaDefinitionOutputFile = path.join(this.options.rootPath, this.options.output, schemaDefinitionFileName);
|
|
41
|
-
private isValidSchemaOutputFile = path.join(this.options.rootPath, this.options.output, "
|
|
41
|
+
private isValidSchemaOutputFile = path.join(this.options.rootPath, this.options.output, "isValidSchema.ts");
|
|
42
42
|
|
|
43
43
|
public constructor(private options: ICommandOptions) {}
|
|
44
44
|
|
|
@@ -51,10 +51,10 @@ export class SchemaGenerator {
|
|
|
51
51
|
console.log(`Aborting - no files found with glob: ${glob}`);
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
|
-
const fileSchemas = await this.
|
|
55
|
-
|
|
54
|
+
const fileSchemas = await this.getJsonSchemasForFiles(fileList);
|
|
55
|
+
|
|
56
56
|
if (fileSchemas.size === 0) {
|
|
57
|
-
console.log(`Aborting - no
|
|
57
|
+
console.log(`Aborting - no types found: ${glob}`);
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
60
|
this.writeSchemaMapToValidationSchema(fileSchemas);
|
|
@@ -80,7 +80,7 @@ export class SchemaGenerator {
|
|
|
80
80
|
return api.withPromise() as Promise<Array<string>>;
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
private
|
|
83
|
+
private getJsonSchemasForFiles = async (filesList: Array<string>) => {
|
|
84
84
|
const { additionalProperties } = this.options;
|
|
85
85
|
const schemaMap = new Map<string, Schema>();
|
|
86
86
|
filesList.forEach((file) => {
|
|
@@ -89,8 +89,7 @@ export class SchemaGenerator {
|
|
|
89
89
|
type: "*",
|
|
90
90
|
additionalProperties,
|
|
91
91
|
encodeRefs: false,
|
|
92
|
-
|
|
93
|
-
expose: "export",
|
|
92
|
+
sortProps: true,
|
|
94
93
|
};
|
|
95
94
|
|
|
96
95
|
const schemaGenerator = tsj.createGenerator(config);
|
|
@@ -135,6 +134,7 @@ export class SchemaGenerator {
|
|
|
135
134
|
|
|
136
135
|
private writeSchemaMapToValidationTypes = async (schemaMap: Map<string, Schema>) => {
|
|
137
136
|
const project = new Project(defaultTsMorphProjectSettings);
|
|
137
|
+
const readerProject = new Project(defaultTsMorphProjectSettings);
|
|
138
138
|
|
|
139
139
|
const symbols: Array<string> = [];
|
|
140
140
|
|
|
@@ -146,11 +146,18 @@ export class SchemaGenerator {
|
|
|
146
146
|
const importPath = `${relativeFilePath}/${fileWithoutExtension}`;
|
|
147
147
|
const defs = schema.definitions ?? {};
|
|
148
148
|
|
|
149
|
+
const readerSourceFile = readerProject.addSourceFileAtPath(filePath);
|
|
150
|
+
|
|
149
151
|
Object.keys(defs).forEach((symbol) => {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
const typeAlias = readerSourceFile.getTypeAlias(symbol);
|
|
153
|
+
const typeInterface = readerSourceFile.getInterface(symbol);
|
|
154
|
+
const hasTypeOrInterface = (typeAlias ?? typeInterface) !== undefined;
|
|
155
|
+
if (hasTypeOrInterface) {
|
|
156
|
+
const namedImports = importMap.get(importPath) ?? [];
|
|
157
|
+
namedImports.push(symbol);
|
|
158
|
+
importMap.set(importPath, namedImports);
|
|
159
|
+
symbols.push(symbol);
|
|
160
|
+
}
|
|
154
161
|
});
|
|
155
162
|
});
|
|
156
163
|
|
|
@@ -161,6 +168,7 @@ export class SchemaGenerator {
|
|
|
161
168
|
});
|
|
162
169
|
|
|
163
170
|
sourceFile.addVariableStatement({
|
|
171
|
+
isExported: true,
|
|
164
172
|
declarationKind: VariableDeclarationKind.Const,
|
|
165
173
|
declarations: [
|
|
166
174
|
{
|
|
@@ -180,15 +188,12 @@ export class SchemaGenerator {
|
|
|
180
188
|
sourceFile.addInterface({
|
|
181
189
|
kind: StructureKind.Interface,
|
|
182
190
|
name: "ISchema",
|
|
183
|
-
isExported:
|
|
191
|
+
isExported: true,
|
|
184
192
|
properties: symbols.map((symbol) => {
|
|
185
193
|
return { name: `readonly ["#/definitions/${symbol}"]`, type: symbol };
|
|
186
194
|
}),
|
|
187
195
|
});
|
|
188
196
|
|
|
189
|
-
sourceFile.addExportDeclaration({
|
|
190
|
-
namedExports: ["schemas", "ISchema"],
|
|
191
|
-
});
|
|
192
197
|
await project.save();
|
|
193
198
|
};
|
|
194
199
|
|
|
@@ -202,6 +207,7 @@ export class SchemaGenerator {
|
|
|
202
207
|
moduleSpecifier: `./${path.parse(schemaDefinitionFileName).name}`,
|
|
203
208
|
});
|
|
204
209
|
sourceFile.addVariableStatement({
|
|
210
|
+
isExported: true,
|
|
205
211
|
declarationKind: VariableDeclarationKind.Const,
|
|
206
212
|
declarations: [
|
|
207
213
|
{
|
|
@@ -216,6 +222,7 @@ export class SchemaGenerator {
|
|
|
216
222
|
|
|
217
223
|
sourceFile.addVariableStatement({
|
|
218
224
|
declarationKind: VariableDeclarationKind.Const,
|
|
225
|
+
isExported: true,
|
|
219
226
|
declarations: [
|
|
220
227
|
{
|
|
221
228
|
name: "isValidSchema",
|
|
@@ -228,10 +235,6 @@ export class SchemaGenerator {
|
|
|
228
235
|
},
|
|
229
236
|
],
|
|
230
237
|
});
|
|
231
|
-
|
|
232
|
-
sourceFile.addExportDeclaration({
|
|
233
|
-
namedExports: ["validator", "isValidSchema"],
|
|
234
|
-
});
|
|
235
238
|
await project.save();
|
|
236
239
|
};
|
|
237
240
|
}
|