From 10c1a3655d715bdcf4b6a0ea905fffa47ff92956 Mon Sep 17 00:00:00 2001 From: supmiku39 Date: Thu, 16 Apr 2020 02:05:04 +0900 Subject: [PATCH] make number convertible to boolean --- README.md | 7 +++++-- dist/utils/StrictTypeParser.js | 2 ++ lib/utils/StrictTypeParser.ts | 1 + package.json | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8913ae..9bf0b12 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,7 @@ To ensure type safety, `STP` provides some static functions to convert `any` typ |`integer`|`int64`|`number`, `string`|no check for range and floating point| |`number`||`number`, `string`|`NaN` and `Infinity` is valid| |`string`||`string`|| -|`boolean`||`boolean`, `string`|| +|`boolean`||`boolean`, `number`, `string`|| |`string`|`date`|`FullDate`, `Date`, `number`, `string`|| |`string`|`date-time`|`Date`, `number`, `string`|| |`string`|`byte`|`string`, `Buffer`|no validation check| @@ -325,8 +325,9 @@ Infinity // OK, although it is convertible to int64 // boolean true, false // OK 'true', 'false' // OK +0, 1, -1, 3.14, Infinity, NaN // OK (Only 0 is converted to false) 'T', 'F' // NG, only 'true' and 'false' are valid -0, 1 // NG, same reason +null, {} // NG // FullDate new FullDate() // OK @@ -518,6 +519,8 @@ This tool only supports `application/json` type for request and response body. A Other $ref like requestBody, responseBody are not supported currently. ## Versions +#### 2.0.2 +- make number convertible to boolean #### 2.0.1 - use IState as a generic type and remove it from api-codegen. #### 2.0.0 diff --git a/dist/utils/StrictTypeParser.js b/dist/utils/StrictTypeParser.js index db6eb39..d21de97 100644 --- a/dist/utils/StrictTypeParser.js +++ b/dist/utils/StrictTypeParser.js @@ -74,6 +74,8 @@ var StrictTypeParser; function _boolean(x, label) { if (typeof x === 'boolean') return x; + if (typeof x === 'number') + return x != 0; if (x === 'true') return true; if (x === 'false') diff --git a/lib/utils/StrictTypeParser.ts b/lib/utils/StrictTypeParser.ts index 9aa7bb5..d809691 100644 --- a/lib/utils/StrictTypeParser.ts +++ b/lib/utils/StrictTypeParser.ts @@ -40,6 +40,7 @@ export module StrictTypeParser { } export function _boolean(x: any, label: string): boolean { if (typeof x === 'boolean') return x; + if (typeof x === 'number') return x!=0; if (x==='true') return true; if (x==='false') return false; throw new BadTypeError(label, 'boolean', x); diff --git a/package.json b/package.json index f55ab17..5c1ab9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@supmiku39/api-ts-gen", - "version": "2.0.1", + "version": "2.0.2", "description": "OpenAPI code generator for TypeScript", "main": "dist/index.js", "types": "dist/index.d.ts",