Skip to content

Commit

Permalink
Option for setting the default number type (number or integer). (Y…
Browse files Browse the repository at this point in the history
…ousefED#316)

Exceptions can be specified with annotations.
  • Loading branch information
havogt authored and domoritz committed Aug 25, 2019
1 parent 9f5b2d5 commit b4ce233
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ In case no `tsconfig.json` is available for your project, you can directly speci

* Generate schema from a typescript type: `typescript-json-schema "project/directory/**/*.ts" TYPE`

The `TYPE` can either be a single, fully qualified type or `"*"` to generate the schema for all types.
The `TYPE` can either be a single, fully qualified type or `"*"` to generate the schema for all types.

```
Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>
Expand All @@ -46,7 +46,8 @@ Options:
--excludePrivate Exclude private members from the schema [boolean] [default: false]
--uniqueNames Use unique names for type symbols. [boolean] [default: false]
--rejectDateType Rejects Date fields in type definitions. [boolean] [default: false]
--id Set schema id. [string] [default: ""]
--id Set schema id. [string] [default: ""]
--defaultNumberType Default number type. [choices: "number", "integer"] [default: "number"]
```

### Programmatic use
Expand Down Expand Up @@ -169,4 +170,3 @@ Inspired and builds upon [Typson](https://github.com/lbovet/typson/), but typesc
`npm run debug -- test/programs/type-alias-single/main.ts --aliasRefs true MyString`

And connect via the debugger protocol.

6 changes: 6 additions & 0 deletions test/programs/type-default-number-as-integer/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface MyObject {
as_integer: number;

/** @TJS-type number */
as_number: number;
}
16 changes: 16 additions & 0 deletions test/programs/type-default-number-as-integer/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"properties": {
"as_integer": {
"type": "integer"
},
"as_number": {
"type": "number"
}
},
"type": "object"
}
}
}
4 changes: 4 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ describe("schema", () => {
assertSchemas("argument-id", "MyObject", {
id: "someSchemaId"
});

assertSchemas("type-default-number-as-integer", "*", {
defaultNumberType: "integer"
});
});

describe("object index", () => {
Expand Down
4 changes: 4 additions & 0 deletions typescript-json-schema-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export function run() {
.describe("rejectDateType", "Rejects Date fields in type definitions.")
.string("id").default("id", defaultArgs.id)
.describe("id", "ID of schema.")
.option("defaultNumberType").choices("defaultNumberType", ["number", "integer"])
.default("defaultNumberType", defaultArgs.defaultNumberType)
.describe("defaultNumberType", "Default number type.")
.argv;

exec(args._[0], args._[1], {
Expand All @@ -63,6 +66,7 @@ export function run() {
uniqueNames: args.uniqueNames,
rejectDateType: args.rejectDateType,
id: args.id,
defaultNumberType: args.defaultNumberType,
});
}

Expand Down
6 changes: 4 additions & 2 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function getDefaultArgs(): Args {
uniqueNames: false,
rejectDateType: false,
id: "",
defaultNumberType: "number"
};
}

Expand All @@ -60,6 +61,7 @@ export type Args = {
uniqueNames: boolean;
rejectDateType: boolean;
id: string;
defaultNumberType: "number" | "integer";
};

export type PartialArgs = Partial<Args>;
Expand Down Expand Up @@ -394,7 +396,7 @@ export class JsonSchemaGenerator {
});
}

private getDefinitionForRootType(propertyType: ts.Type, reffedType: ts.Symbol, definition: Definition) {
private getDefinitionForRootType(propertyType: ts.Type, reffedType: ts.Symbol, definition: Definition, defaultNumberType = this.args.defaultNumberType) {
const tupleType = resolveTupleType(propertyType);

if (tupleType) { // tuple
Expand All @@ -420,7 +422,7 @@ export class JsonSchemaGenerator {
if (flags & ts.TypeFlags.String) {
definition.type = "string";
} else if (flags & ts.TypeFlags.Number) {
const isInteger = (definition.type === "integer" || (reffedType && reffedType.getName() === "integer"));
const isInteger = (definition.type === "integer" || (reffedType && reffedType.getName() === "integer")) || defaultNumberType === "integer";
definition.type = isInteger ? "integer" : "number";
} else if (flags & ts.TypeFlags.Boolean) {
definition.type = "boolean";
Expand Down

0 comments on commit b4ce233

Please sign in to comment.