From 4cd4908ab1c3703d70a0e4c11d5b274dbca9aaeb Mon Sep 17 00:00:00 2001 From: Grigas <35135765+grigasp@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:48:41 +0300 Subject: [PATCH] fix: support enum template literals (#1637) --- src/Utils/extractLiterals.ts | 3 ++- test/valid-data-other.test.ts | 1 + .../valid-data/enums-template-literal/main.ts | 8 ++++++++ .../enums-template-literal/schema.json | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/valid-data/enums-template-literal/main.ts create mode 100644 test/valid-data/enums-template-literal/schema.json diff --git a/src/Utils/extractLiterals.ts b/src/Utils/extractLiterals.ts index 47df38664..21d740118 100644 --- a/src/Utils/extractLiterals.ts +++ b/src/Utils/extractLiterals.ts @@ -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"; @@ -13,7 +14,7 @@ function* _extractLiterals(type: BaseType): Iterable { 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); } diff --git a/test/valid-data-other.test.ts b/test/valid-data-other.test.ts index bc3047903..bae3b412d 100644 --- a/test/valid-data-other.test.ts +++ b/test/valid-data-other.test.ts @@ -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", diff --git a/test/valid-data/enums-template-literal/main.ts b/test/valid-data/enums-template-literal/main.ts new file mode 100644 index 000000000..c84d3f455 --- /dev/null +++ b/test/valid-data/enums-template-literal/main.ts @@ -0,0 +1,8 @@ +enum MyEnum { + A = "a", + B = "b", +} + +export type MyObject = { + prop?: `${MyEnum}`; +} diff --git a/test/valid-data/enums-template-literal/schema.json b/test/valid-data/enums-template-literal/schema.json new file mode 100644 index 000000000..33c8a36b7 --- /dev/null +++ b/test/valid-data/enums-template-literal/schema.json @@ -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" + } + } +}