Skip to content

Commit

Permalink
Add test case for regressed enum generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Jan 6, 2025
1 parent 2c5778e commit e6723ff
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/valid-data-other.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("valid-data-other", () => {
it("enums-mixed", assertValidSchema("enums-mixed", "Enum"));
it("enums-member", assertValidSchema("enums-member", "MyObject"));
it("enums-template-literal", assertValidSchema("enums-template-literal", "MyObject"));
it("enums-union", assertValidSchema("enums-union", "MyObject"));

it("function-parameters-default-value", assertValidSchema("function-parameters-default-value", "myFunction"));
it("function-parameters-declaration", assertValidSchema("function-parameters-declaration", "myFunction"));
Expand Down
56 changes: 56 additions & 0 deletions test/valid-data/enums-string-union/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"properties": {
"enumMemberWithLiteral": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "foo",
"type": "string"
}
]
},
"enumMemberWithLiteralAndNull": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "foo",
"type": "string"
},
{
"type": "null"
}
]
},
"enumMembers": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "beta",
"type": "string"
}
]
}
},
"required": [
"enumMembers",
"enumMemberWithLiteral",
"enumMemberWithLiteralAndNull"
],
"type": "object"
}
}
}
16 changes: 16 additions & 0 deletions test/valid-data/enums-union/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enum Alphabet {
Alpha = "alpha",
Beta = "beta",
Omega = 666,
}

export type MyObject = {
// All the members above should be output as enums, not anyOf
enumMembers: Alphabet.Alpha | Alphabet.Beta;
enumMemberWithLiteral: Alphabet.Alpha | "foo";
enumMemberWithLiteralAndNull: Alphabet.Alpha | "foo" | null;
enumMembersWithNumber: Alphabet.Alpha | Alphabet.Omega;
wholeEnum: Alphabet; // Should output just all of Alphabet
wholeEnumWithLiteral: Alphabet | "bar"; // Should output all of Alphabet members (2 strings, 1 number) and "bar"
wholeEnumWithLiteralAndNull: Alphabet | "bar" | null; // Smae as above, but with null
};
124 changes: 124 additions & 0 deletions test/valid-data/enums-union/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"properties": {
"enumMemberWithLiteral": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "foo",
"type": "string"
}
]
},
"enumMemberWithLiteralAndNull": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "foo",
"type": "string"
},
{
"type": "null"
}
]
},
"enumMembers": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": "beta",
"type": "string"
}
]
},
"enumMembersWithNumber": {
"anyOf": [
{
"const": "alpha",
"type": "string"
},
{
"const": 666,
"type": "number"
}
]
},
"wholeEnum": {
"enum": [
"alpha",
"beta",
666
],
"type": [
"string",
"number"
]
},
"wholeEnumWithLiteral": {
"anyOf": [
{
"enum": [
"alpha",
"beta",
666
],
"type": [
"string",
"number"
]
},
{
"const": "bar",
"type": "string"
}
]
},
"wholeEnumWithLiteralAndNull": {
"anyOf": [
{
"enum": [
"alpha",
"beta",
666
],
"type": [
"string",
"number"
]
},
{
"const": "bar",
"type": "string"
},
{
"type": "null"
}
]
}
},
"required": [
"enumMembers",
"enumMemberWithLiteral",
"enumMemberWithLiteralAndNull",
"enumMembersWithNumber",
"wholeEnum",
"wholeEnumWithLiteral",
"wholeEnumWithLiteralAndNull"
],
"type": "object"
}
}
}

0 comments on commit e6723ff

Please sign in to comment.