ts-runtime-validation 1.0.2 → 1.0.3

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 CHANGED
@@ -6,7 +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}`.
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:
10
+
11
+ 1. `./src/ts-runtime-validation/validation.schema.json` - containing the [jsonschema](http://json-schema.org/) types
12
+ 1. `./src/SchemaDefinition.ts` - containing the typescript.
13
+ 1. `./src/isSchemaValid.ts` - containing a type guard type inferring helper method (intended for consumption in your code base - examples below)
10
14
 
11
15
  ## Footnote
12
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-runtime-validation",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "author": "Matthew Duong <thegalah@gmail.com>",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -40,11 +40,9 @@ export class SchemaGenerator {
40
40
  private tsSchemaDefinitionOutputFile = path.join(this.options.rootPath, this.options.output, schemaDefinitionFileName);
41
41
  private isValidSchemaOutputFile = path.join(this.options.rootPath, this.options.output, "isSchemaValid.ts");
42
42
 
43
- public constructor(private options: ICommandOptions) {
44
- this.generateOutput();
45
- }
43
+ public constructor(private options: ICommandOptions) {}
46
44
 
47
- private generateOutput = async () => {
45
+ public Generate = async () => {
48
46
  const { helpers } = this.options;
49
47
  const fileList = await this.getMatchingFiles();
50
48
  console.log(`Found ${fileList.length} schema file(s)`);
package/src/index.ts CHANGED
@@ -32,4 +32,5 @@ program.parse();
32
32
 
33
33
  const options = program.opts<ICommandOptions>();
34
34
 
35
- new SchemaGenerator(options);
35
+ const generator = new SchemaGenerator(options);
36
+ generator.Generate();
@@ -1,39 +0,0 @@
1
- const fs = require("fs");
2
- const schema = require("../src/validation.schema.json");
3
- const OUTPUT_FILE = "./src/SchemaDefinition.ts";
4
-
5
- const reservedDefinitionNameFilterFn = (definition) => {
6
- return definition !== "ISchema" && definition !== "Schemas";
7
- };
8
-
9
- const definitions = Object.keys(schema.definitions).filter(reservedDefinitionNameFilterFn);
10
-
11
- const lines = [];
12
- lines.push(`// THIS IS AN AUTOGENERATED FILE PLEASE DO NOT MODIFY MANUALLY`);
13
-
14
- lines.push(`import {${definitions.join(", ")}} from "./Types";`);
15
- lines.push("");
16
-
17
- lines.push(`export const schemas: Record<keyof ISchema, string> = {`);
18
- definitions.forEach((definition) => {
19
- lines.push(` ["#/definitions/${definition}"]: "${definition}",`);
20
- });
21
- lines.push(`}`);
22
-
23
- lines.push("");
24
-
25
- lines.push("export interface ISchema {");
26
-
27
- definitions.forEach((definition) => {
28
- lines.push(` ["#/definitions/${definition}"]: ${definition},`);
29
- });
30
- lines.push("}");
31
- lines.push("");
32
-
33
- const output = lines.join("\n");
34
-
35
- fs.writeFile(OUTPUT_FILE, output, { flag: "w" }, function (err) {
36
- if (err) {
37
- return console.log(err);
38
- }
39
- });