Archived
1
0
Fork 0

make number convertible to boolean

This commit is contained in:
supmiku39 2020-04-16 02:05:04 +09:00
parent d51c345867
commit 10c1a3655d
4 changed files with 9 additions and 3 deletions

View file

@ -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| |`integer`|`int64`|`number`, `string`|no check for range and floating point|
|`number`||`number`, `string`|`NaN` and `Infinity` is valid| |`number`||`number`, `string`|`NaN` and `Infinity` is valid|
|`string`||`string`|| |`string`||`string`||
|`boolean`||`boolean`, `string`|| |`boolean`||`boolean`, `number`, `string`||
|`string`|`date`|`FullDate`, `Date`, `number`, `string`|| |`string`|`date`|`FullDate`, `Date`, `number`, `string`||
|`string`|`date-time`|`Date`, `number`, `string`|| |`string`|`date-time`|`Date`, `number`, `string`||
|`string`|`byte`|`string`, `Buffer`|no validation check| |`string`|`byte`|`string`, `Buffer`|no validation check|
@ -325,8 +325,9 @@ Infinity // OK, although it is convertible to int64
// boolean // boolean
true, false // OK true, false // OK
'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 'T', 'F' // NG, only 'true' and 'false' are valid
0, 1 // NG, same reason null, {} // NG
// FullDate // FullDate
new FullDate() // OK 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. Other $ref like requestBody, responseBody are not supported currently.
## Versions ## Versions
#### 2.0.2
- make number convertible to boolean
#### 2.0.1 #### 2.0.1
- use IState as a generic type and remove it from api-codegen. - use IState as a generic type and remove it from api-codegen.
#### 2.0.0 #### 2.0.0

View file

@ -74,6 +74,8 @@ var StrictTypeParser;
function _boolean(x, label) { function _boolean(x, label) {
if (typeof x === 'boolean') if (typeof x === 'boolean')
return x; return x;
if (typeof x === 'number')
return x != 0;
if (x === 'true') if (x === 'true')
return true; return true;
if (x === 'false') if (x === 'false')

View file

@ -40,6 +40,7 @@ export module StrictTypeParser {
} }
export function _boolean(x: any, label: string): boolean { export function _boolean(x: any, label: string): boolean {
if (typeof x === 'boolean') return x; if (typeof x === 'boolean') return x;
if (typeof x === 'number') return x!=0;
if (x==='true') return true; if (x==='true') return true;
if (x==='false') return false; if (x==='false') return false;
throw new BadTypeError(label, 'boolean', x); throw new BadTypeError(label, 'boolean', x);

View file

@ -1,6 +1,6 @@
{ {
"name": "@supmiku39/api-ts-gen", "name": "@supmiku39/api-ts-gen",
"version": "2.0.1", "version": "2.0.2",
"description": "OpenAPI code generator for TypeScript", "description": "OpenAPI code generator for TypeScript",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",