Skip to content

Commit

Permalink
Support boolean literals
Browse files Browse the repository at this point in the history
  • Loading branch information
akheron committed Sep 1, 2023
1 parent 6dd60e2 commit f0d4397
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,10 @@ const typeToSchema = (
if (isNumberType(type)) {
return { type: 'number', ...base }
}
if (isBooleanLiteralType(type)) {
const isTrue = type === ctx.checker.getTrueType()
return { type: 'boolean', enum: [isTrue], ...base }
}
if (isBooleanType(type)) {
return { type: 'boolean', ...base }
}
Expand Down
14 changes: 14 additions & 0 deletions tests/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,12 @@ Object {
"format": "date-time",
"type": "string",
},
"falseLit": Object {
"enum": Array [
false,
],
"type": "boolean",
},
"nullableNum": Object {
"nullable": true,
"type": "number",
Expand Down Expand Up @@ -797,6 +803,12 @@ Object {
],
"type": "string",
},
"trueLit": Object {
"enum": Array [
true,
],
"type": "boolean",
},
},
"required": Array [
"str",
Expand All @@ -807,6 +819,8 @@ Object {
"numLits",
"strLit",
"strLits",
"trueLit",
"falseLit",
"date",
],
"type": "object",
Expand Down
2 changes: 2 additions & 0 deletions tests/test-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const codec = t.intersection([
numLits: t.union([t.literal(42), t.literal(123)]),
strLit: t.literal('foo'),
strLits: t.union([t.literal('foo'), t.literal('bar')]),
trueLit: t.literal(true),
falseLit: t.literal(false),
date: DateFromISOString,
}),
t.partial({ optionalBool: t.boolean }),
Expand Down

0 comments on commit f0d4397

Please sign in to comment.