Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,23 @@ class MyObject {
```


## [default-properties-initializer](./test/programs/default-properties-initializer)

```ts
export class MyObject {
varString: string = "foo";
varSingleQuoted: string = 'bar';
varNumber: number = 123;
varFloat: number = 3.21;
varNegative: number = -5;
varBoolean: boolean = true;
varNull: null = null;
varArray: number[] = [1, 2, 3];
varStringArray: string[] = ["a", "b"];
}
```


## [enums-compiled-compute](./test/programs/enums-compiled-compute)

```ts
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"safe-stable-stringify": "^2.5.0",
"ts-node": "^10.9.2",
"typescript": "~5.9.3",
"vm2": "^3.11.3",
"yargs": "^18.0.0"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions test/programs/default-properties-initializer/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class MyObject {
varString: string = "foo";
varSingleQuoted: string = 'bar';
varNumber: number = 123;
varFloat: number = 3.21;
varNegative: number = -5;
varBoolean: boolean = true;
varNull: null = null;
varArray: number[] = [1, 2, 3];
varStringArray: string[] = ["a", "b"];
}
67 changes: 67 additions & 0 deletions test/programs/default-properties-initializer/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "object",
"properties": {
"varString": {
"type": "string",
"default": "foo"
},
"varSingleQuoted": {
"type": "string",
"default": "bar"
},
"varNumber": {
"type": "number",
"default": 123
},
"varFloat": {
"type": "number",
"default": 3.21
},
"varNegative": {
"type": "number",
"default": -5
},
"varBoolean": {
"type": "boolean",
"default": true
},
"varNull": {
"type": "null",
"default": null
},
"varArray": {
"type": "array",
"items": {
"type": "number"
},
"default": [
1,
2,
3
]
},
"varStringArray": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"a",
"b"
]
}
},
"additionalProperties": false,
"required": [
"varArray",
"varBoolean",
"varFloat",
"varNegative",
"varNull",
"varNumber",
"varSingleQuoted",
"varString",
"varStringArray"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
2 changes: 2 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ describe("schema", () => {

assertSchema("default-properties", "MyObject");

assertSchema("default-properties-initializer", "MyObject");

// not supported yet #116
// assertSchema("interface-extra-props", "MyObject");

Expand Down
57 changes: 39 additions & 18 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export { Program, CompilerOptions, Symbol } from "typescript";

export { ts };

const { VM } = require("vm2");

const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g;
const REGEX_TSCONFIG_NAME = /^.*\.json$/;
const REGEX_TJS_JSDOC = /^-([\w]+)\s+(\S|\S[\s\S]*\S)\s*$/g;
Expand Down Expand Up @@ -250,6 +248,40 @@ function parseValue(symbol: ts.Symbol, key: string, value: string): any {
}
}

/**
* Evaluate a property initializer expression to its literal value, without
* executing any code. Only the literal forms that can be represented in a JSON
* Schema `default` are supported: string, number, boolean, null and arrays of
* those. Returns `undefined` for anything that is not a supported literal.
*/
function evaluateLiteralInitializer(node: ts.Node): PrimitiveType | any[] | undefined {
if (ts.isStringLiteralLike(node)) {
return node.text;
} else if (ts.isNumericLiteral(node)) {
return Number(node.text);
} else if (ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.MinusToken) {
const operand = evaluateLiteralInitializer(node.operand);
return typeof operand === "number" ? -operand : undefined;
} else if (node.kind === ts.SyntaxKind.TrueKeyword) {
return true;
} else if (node.kind === ts.SyntaxKind.FalseKeyword) {
return false;
} else if (node.kind === ts.SyntaxKind.NullKeyword) {
return null;
} else if (ts.isArrayLiteralExpression(node)) {
const values: any[] = [];
for (const element of node.elements) {
const value = evaluateLiteralInitializer(element);
if (value === undefined) {
return undefined;
}
values.push(value);
}
return values;
}
return undefined;
}

function extractLiteralValue(typ: ts.Type): PrimitiveType | undefined {
let str = (<ts.LiteralType>typ).value;
if (str === undefined) {
Expand Down Expand Up @@ -866,22 +898,11 @@ export class JsonSchemaGenerator {
} else if ((<any>initial).kind && (<any>initial).kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
definition.default = initial.getText();
} else {
try {
const vm = new VM();
const val = vm.run("sandboxvar=" + initial.getText()) as any;
if (
val === null ||
typeof val === "string" ||
typeof val === "number" ||
typeof val === "boolean" ||
Object.prototype.toString.call(val) === "[object Array]"
) {
definition.default = val;
} else if (val) {
console.warn("unknown initializer for property " + propertyName + ": " + val);
}
} catch (e) {
console.warn("exception evaluating initializer for property " + propertyName);
const val = evaluateLiteralInitializer(initial);
if (val !== undefined) {
definition.default = val;
} else {
console.warn("unknown initializer for property " + propertyName + ": " + initial.getText());
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@
dependencies:
undici-types "~7.16.0"

acorn-walk@^8.1.1, acorn-walk@^8.3.4:
acorn-walk@^8.1.1:
version "8.3.5"
resolved "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.5.tgz#8a6b8ca8fc5b34685af15dabb44118663c296496"
integrity sha512-HEHNfbars9v4pgpW6SO1KSPkfoS0xVOM/9UzkJltjlsHZmJasxg8aXkuZa7SMf8vKGIBhpUsPluQSqhJFCqebw==
dependencies:
acorn "^8.11.0"

acorn@^8.11.0, acorn@^8.15.0, acorn@^8.4.1:
acorn@^8.11.0, acorn@^8.4.1:
version "8.16.0"
resolved "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz#4ce79c89be40afe7afe8f3adb902a1f1ce9ac08a"
integrity sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==
Expand Down Expand Up @@ -1006,14 +1006,6 @@ v8-compile-cache-lib@^3.0.1:
resolved "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==

vm2@^3.11.3:
version "3.11.3"
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.11.3.tgz#5323018a93ae2862c9d52b07b658ebb8d74903f0"
integrity sha512-DO1TTKuOc+veL11VNOvJwRab80mghFKE40Av3bl6pdXs11bdiDMuR73owy+dS2EsTZEvRUeBkkBuDVRjV/RgEw==
dependencies:
acorn "^8.15.0"
acorn-walk "^8.3.4"

which@^2.0.1:
version "2.0.2"
resolved "https://registry.npmjs.org/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
Expand Down
Loading