Skip to content

Commit

Permalink
fix: fix string enum keys for values with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Haddow committed Aug 19, 2024
1 parent 8bfacf2 commit 0d51675
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions plugins/typescript/src/core/schemaToEnumDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ describe("schemaToTypeAliasDeclaration", () => {
it("should generate a string enums", () => {
const schema: SchemaObject = {
type: "string",
enum: ["AVAILABLE", "PENDING", "SOLD"],
enum: ["AVAILABLE", "PENDING", "SOLD", "WITH SPACE"],
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export enum Test {
AVAILABLE = "AVAILABLE",
PENDING = "PENDING",
SOLD = "SOLD"
SOLD = "SOLD",
"WITH SPACE" = "WITH SPACE"
}"
`);
});
Expand Down
1 change: 1 addition & 0 deletions plugins/typescript/src/core/schemaToEnumDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function getEnumMembers(

if (typeof enumValue === "string") {
enumName = enumValue.match(/^\d/) ? `"_${enumValue}"` : enumValue;
enumName = enumName.match(/[^"]+.*\s.*/) ? `"${enumName}"` : enumName;
enumValueNode = f.createStringLiteral(enumValue);
} else if (typeof enumValue === "number") {
enumName = convertNumberToWord(enumValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe("generateSchemaTypes", () => {
export enum DogBreed {
saimois = "saimois",
bengal = "bengal",
british shorthair = "british shorthair"
"british shorthair" = "british shorthair"
}
/**
Expand Down

0 comments on commit 0d51675

Please sign in to comment.