Skip to content

Commit 98000b4

Browse files
authored
fix: support enums with single value (#33) (#34)
1 parent 312caf6 commit 98000b4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/language/typescript.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,10 @@ const serializeSchemaObject = (schema: TSchemaObject, rootName: string, cwd: str
362362

363363
const serializeEnum = (enumValue: Array<string | number | boolean>): TSerializedType => {
364364
const type = enumValue.map(value => `'${value}'`).join(' | ');
365-
const io = `union([${enumValue.map(value => `literal('${value}')`).join(',')}])`;
365+
const io =
366+
enumValue.length === 1
367+
? `literal(${type})`
368+
: `union([${enumValue.map(value => `literal('${value}')`).join(',')}])`;
366369
return serializedType(type, io, [dependency('union', 'io-ts'), dependency('literal', 'io-ts')], EMPTY_REFS);
367370
};
368371

test/specs/json/common.json

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"delivered"
3535
]
3636
},
37+
"state" : {
38+
"type" : "string",
39+
"description" : "order state",
40+
"enum" : [ "ACTIVE" ]
41+
},
3742
"complete": {
3843
"type": "boolean",
3944
"default": false

0 commit comments

Comments
 (0)