json-schema-to-typescript
json-schema-to-typescript copied to clipboard
Enum generator creates invalid properties when value contain special characters
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.jsonwith an enum that contains special characters:
{
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": [
"user.created",
"email/verified",
"2fa-enabled",
"account*deleted"
]
}
}
}
- 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.
*/
export interface ExampleSchema {
status?: Status
[k: string]: unknown
}
export const enum Status {
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:
enum Status {
user.created = "user.created",
email/verified = "email/verified",
2fa-enabled = "2fa-enabled",
account*deleted = "account*deleted"
}
Expected Behavior
The generator should escape or transform special characters to create valid TypeScript identifiers while preserving the original values as the enum values:
enum Status {
'user.created' = "user.created",
'email/verified' = "email/verified",
'2fa-enabled' = "2fa-enabled",
'account*deleted' = "account*deleted"
}
Environment
json-schema-to-typescriptversion:14.0.5- Node.js version:
v18.20.4 - Operating System: MacOS
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:
- Quote keyName in generateStandaloneEnum (replace
keyNamewith'${keyName}') - (Optional): quote only when the property name contains special characters
- This would make the implementation more complex unnecessarily as quoted properties without special characters are also valid
- Add a CLI option to opt-in this behaviour in order to avoid breaking changes in the generated code