Skip to content

Commit

Permalink
fix: support enum template literals (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigasp authored Apr 12, 2023
1 parent 6b06e30 commit 4cd4908
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Utils/extractLiterals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UnknownTypeError } from "../Error/UnknownTypeError";
import { AliasType } from "../Type/AliasType";
import { BaseType } from "../Type/BaseType";
import { BooleanType } from "../Type/BooleanType";
import { EnumType } from "../Type/EnumType";
import { LiteralType } from "../Type/LiteralType";
import { UnionType } from "../Type/UnionType";

Expand All @@ -13,7 +14,7 @@ function* _extractLiterals(type: BaseType): Iterable<string> {
yield type.getValue().toString();
return;
}
if (type instanceof UnionType) {
if (type instanceof UnionType || type instanceof EnumType) {
for (const t of type.getTypes()) {
yield* _extractLiterals(t);
}
Expand Down
1 change: 1 addition & 0 deletions test/valid-data-other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe("valid-data-other", () => {
it("enums-compute", assertValidSchema("enums-compute", "Enum"));
it("enums-mixed", assertValidSchema("enums-mixed", "Enum"));
it("enums-member", assertValidSchema("enums-member", "MyObject"));
it("enums-template-literal", assertValidSchema("enums-template-literal", "MyObject"));

it(
"function-parameters-default-value",
Expand Down
8 changes: 8 additions & 0 deletions test/valid-data/enums-template-literal/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
enum MyEnum {
A = "a",
B = "b",
}

export type MyObject = {
prop?: `${MyEnum}`;
}
19 changes: 19 additions & 0 deletions test/valid-data/enums-template-literal/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"properties": {
"prop": {
"enum": [
"a",
"b"
],
"type": "string"
}
},
"type": "object"
}
}
}

0 comments on commit 4cd4908

Please sign in to comment.