Skip to content

Commit 6d2fe54

Browse files
rpetrichdomoritz
authored andcommitted
Add rejectDateType option (YousefED#224)
* Add --rejectDateType option that rejects any type definitions that reference Date * Add test for date-string format * Error instead of generating partial schemas for types that can't be represented in JSON-Schema; verify that schema for Date types fails to generate when rejectDateType is set * Add --rejectDateType to the README
1 parent 60379cd commit 6d2fe54

File tree

6 files changed

+61
-8
lines changed

6 files changed

+61
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Options:
4545
--ignoreErrors Generate even if the program has errors. [boolean] [default: false]
4646
--excludePrivate Exclude private members from the schema [boolean] [default: false]
4747
--uniqueNames Use unique names for type symbols. [boolean] [default: false]
48+
--rejectDateType Rejects Date fields in type definitions. [boolean] [default: false]
4849
--id Set schema id. [string] [default: ""]
4950
```
5051

test/programs/dates/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type DateAlias = Date;
2+
3+
interface MyObject {
4+
var1: Date;
5+
var2: DateAlias;
6+
}

test/programs/dates/schema.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"properties": {
4+
"var1": {
5+
"description": "Enables basic storage and retrieval of dates and times.",
6+
"format": "date-time",
7+
"type": "string"
8+
},
9+
"var2": {
10+
"description": "Enables basic storage and retrieval of dates and times.",
11+
"format": "date-time",
12+
"type": "string"
13+
}
14+
},
15+
"required": [
16+
"var1",
17+
"var2"
18+
],
19+
"type": "object"
20+
}
21+

test/schema.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ export function assertSchemas(group: string, type: string, settings: TJS.Partial
6565
});
6666
}
6767

68+
export function assertRejection(group: string, type: string, settings: TJS.PartialArgs = {}, compilerOptions?: TJS.CompilerOptions) {
69+
it(group + " should reject input", () => {
70+
let schema = null;
71+
assert.throws(() => {
72+
if (!("required" in settings)) {
73+
settings.required = true;
74+
}
75+
76+
const files = [resolve(BASE + group + "/main.ts")];
77+
schema = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files);
78+
});
79+
assert.equal(schema, null, "Expected no schema to be generated");
80+
});
81+
}
82+
6883
describe("interfaces", () => {
6984
it("should return an instance of JsonSchemaGenerator", () => {
7085
const program = TJS.getProgramFromFiles([resolve(BASE + "comments/main.ts")]);
@@ -247,6 +262,13 @@ describe("schema", () => {
247262
assertSchema("string-literals-inline", "MyObject");
248263
});
249264

265+
describe("dates", () => {
266+
assertSchema("dates", "MyObject");
267+
assertRejection("dates", "MyObject", {
268+
rejectDateType: true
269+
});
270+
});
271+
250272
describe("namespaces", () => {
251273
assertSchema("namespace", "Type");
252274
assertSchema("namespace-deep-1", "RootNamespace.Def");

typescript-json-schema-cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export function run() {
3838
.describe("uniqueNames", "Use unique names for type symbols.")
3939
.array("include").default("*", defaultArgs.include)
4040
.describe("include", "Further limit tsconfig to include only matching files.")
41+
.boolean("rejectDateType").default("rejectDateType", defaultArgs.rejectDateType)
42+
.describe("rejectDateType", "Rejects Date fields in type definitions.")
4143
.string("id").default("id", defaultArgs.id)
4244
.describe("id", "ID of schema.")
4345
.argv;
@@ -59,7 +61,8 @@ export function run() {
5961
include: args.include,
6062
excludePrivate: args.excludePrivate,
6163
uniqueNames: args.uniqueNames,
62-
id: args.id
64+
rejectDateType: args.rejectDateType,
65+
id: args.id,
6366
});
6467
}
6568

typescript-json-schema.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export function getDefaultArgs(): Args {
3030
include: [],
3131
excludePrivate: false,
3232
uniqueNames: false,
33-
id: ""
33+
rejectDateType: false,
34+
id: "",
3435
};
3536
}
3637

@@ -55,6 +56,7 @@ export type Args = {
5556
include: string[];
5657
excludePrivate: boolean;
5758
uniqueNames: boolean;
59+
rejectDateType: boolean;
5860
id: string;
5961
};
6062

@@ -398,7 +400,7 @@ export class JsonSchemaGenerator {
398400
definition.type = "undefined";
399401
} else if (flags & ts.TypeFlags.Any) {
400402
// no type restriction, so that anything will match
401-
} else if (propertyTypeString === "Date") {
403+
} else if (propertyTypeString === "Date" && !this.args.rejectDateType) {
402404
definition.type = "string";
403405
definition.format = "date-time";
404406
} else if (propertyTypeString === "object") {
@@ -415,11 +417,9 @@ export class JsonSchemaGenerator {
415417
definition.items = this.getTypeDefinition(arrayType);
416418
} else {
417419
// Report that type could not be processed
418-
let info: any = propertyType;
419-
try {
420-
info = JSON.stringify(propertyType);
421-
} catch(err) {}
422-
console.error("Unsupported type: ", info);
420+
const error = new TypeError("Unsupported type: " + propertyTypeString);
421+
(error as any).type = propertyType;
422+
throw error;
423423
// definition = this.getTypeDefinition(propertyType, tc);
424424
}
425425
}

0 commit comments

Comments
 (0)