Skip to content

Commit 02cce95

Browse files
ndunksdomoritz
authored andcommitted
Fix Error @TJS-custom annotation without value (YousefED#313)
1 parent 921dc10 commit 02cce95

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

test/programs/annotation-tjs/main.ts

+15
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,19 @@ interface MyObject {
6464
* @TJS-examples ["foo", 1]
6565
*/
6666
examples: string;
67+
68+
/**
69+
* @TJS-hide
70+
*/
71+
booleanAnnotationDefaultValue: string;
72+
73+
/**
74+
* @TJS-hide true
75+
*/
76+
booleanAnnotationWithTrue: string;
77+
78+
/**
79+
* @TJS-hide false
80+
*/
81+
booleanAnnotationWithFalse: string;
6782
}

test/programs/annotation-tjs/schema.json

+15
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,24 @@
5555
"uriTemplate": {
5656
"format": "uri-template",
5757
"type": "string"
58+
},
59+
"booleanAnnotationDefaultValue": {
60+
"hide": true,
61+
"type": "string"
62+
},
63+
"booleanAnnotationWithTrue": {
64+
"hide": true,
65+
"type": "string"
66+
},
67+
"booleanAnnotationWithFalse": {
68+
"hide": false,
69+
"type": "string"
5870
}
5971
},
6072
"required": [
73+
"booleanAnnotationDefaultValue",
74+
"booleanAnnotationWithFalse",
75+
"booleanAnnotationWithTrue",
6176
"dateTime",
6277
"email",
6378
"examples",

test/schema.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ describe("schema", () => {
190190
describe("annotations", () => {
191191
assertSchema("annotation-default", "MyObject");
192192
assertSchema("annotation-ref", "MyObject");
193-
assertSchema("annotation-tjs", "MyObject");
193+
assertSchema("annotation-tjs", "MyObject", {
194+
validationKeywords: [ "hide" ]
195+
});
194196
assertSchema("annotation-id", "MyObject");
195197

196198
assertSchema("typeof-keyword", "MyObject", {typeOfKeyword: true});

typescript-json-schema.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,18 @@ export class JsonSchemaGenerator {
373373
const jsdocs = symbol.getJsDocTags();
374374
jsdocs.forEach(doc => {
375375
// if we have @TJS-... annotations, we have to parse them
376-
const [name, text] = (doc.name === "TJS" ? new RegExp(REGEX_TJS_JSDOC).exec(doc.text!)!.slice(1,3) : [doc.name, doc.text]) as string[];
376+
let [ name, text ] = [doc.name, doc.text as string];
377+
if( doc.name === "TJS" ) {
378+
let match: string[] | RegExpExecArray | null = new RegExp(REGEX_TJS_JSDOC).exec(doc.text!);
379+
if( match ) {
380+
name = match[1];
381+
text = match[2];
382+
} else {
383+
// Treat empty text as boolean true
384+
name = (text as string).replace(/^[\s\-]+/, "");
385+
text = "true";
386+
}
387+
}
377388
if (validationKeywords[name] || this.userValidationKeywords[name]) {
378389
definition[name] = text === undefined ? "" : parseValue(text);
379390
} else {

0 commit comments

Comments
 (0)