You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Create a JSON Schema example.schema.json with an enum that contains special characters:
Generate TypeScript types using json-schema-to-typescript
yarn run json2ts --inferStringEnumKeysFromValues -i example.schema.json
Observe an error in the output:
SyntaxError: An enum member name must be followed by a ',', '=', or '}'. (14:5)
12 |
13 |export const enum Status {
> 14 | user.created = "user.created",
| ^
15 | email/verified = "email/verified",
16 | 2fa-enabled = "2fa-enabled",
17 | account*deleted = "account*deleted"
Alternatively:
Generate TypeScript types using json-schema-to-typescript with formatting disabled
yarn run json2ts --inferStringEnumKeysFromValues --format false -i example.schema.json
Observe invalid TypeScript:
/* eslint-disable *//*** This file was automatically generated by json-schema-to-typescript.* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,* and run json-schema-to-typescript to regenerate this file.*/exportinterfaceExampleSchema{status?: Status[k: string]: unknown}exportconstenumStatus{user.created="user.created",email/verified="email/verified",2fa-enabled="2fa-enabled",account*deleted="account*deleted"}
Current Behavior
The generator creates enum properties that directly use the special characters, resulting in invalid TypeScript:
The generator should escape or transform special characters to create valid TypeScript identifiers while preserving the original values as the enum values:
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:
yarn run json2ts --inferStringEnumKeysFromValues --format false -i example.schema.json
Current Behavior
The generator creates enum properties that directly use the special characters, resulting in invalid TypeScript:
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}'
)The text was updated successfully, but these errors were encountered: