Open quentinverlhac opened 3 weeks ago
For what's worth, I stumbled on this issue as well. A simple workaround is making a patch adding quotes around enum keys:
diff --git a/node_modules/json-schema-to-typescript/dist/src/generator.js b/node_modules/json-schema-to-typescript/dist/src/generator.js
index [2](https://bitbucket.org/atlassian/shepherd-front-end/pull-requests/1446/diff#Lpatches/json-schema-to-typescript+15.0.2.patchT2)24571d..20ecd87 100644
--- a/node_modules/json-schema-to-typescript/dist/src/generator.js
+++ b/node_modules/json-schema-to-typescript/dist/src/generator.js
@@ -280,7 +280,7 @@ function generateStandaloneEnum(ast, options) {
(options.enableConstEnums ? 'const ' : '') +
`enum ${(0, utils_1.toSafeString)(ast.standaloneName)} {` +
'\n' +
- ast.params.map(({ ast, keyName }) => keyName + ' = ' + (0, exports.generateType)(ast, options)).join(',\n') +
+ ast.params.map(({ ast, keyName }) => '"' + keyName + '" = ' + (0, exports.generateType)(ast, options)).join(',\n') +
'\n' +
'}');
}
Enum generator creates invalid properties when value contain special characters
Description
When generating TypeScript enums from JSON Schema enums that contain special characters in some of their values, the resulting enum properties are invalid TypeScript identifiers.
Steps to Reproduce
example.schema.json
with an enum that contains special characters:Alternatively:
Generate TypeScript types using json-schema-to-typescript with formatting disabled
Observe invalid TypeScript:
export interface ExampleSchema { status?: Status
}
export const enum Status { user.created = "user.created", email/verified = "email/verified", 2fa-enabled = "2fa-enabled", accountdeleted = "accountdeleted" }
Expected Behavior
The generator should escape or transform special characters to create valid TypeScript identifiers while preserving the original values as the enum values:
Environment
json-schema-to-typescript
version:14.0.5
v18.20.4
13.2.1 (22D68)
Additional Context
This is particularly important when working with APIs or existing schemas that use dot notation or other special characters in their enum values.
Possible Solution
Consider quoting the generated property names:
keyName
with'${keyName}'
)