json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Enum generator creates invalid properties when value contain special characters

Open quentinverlhac opened this issue 1 year ago • 0 comments

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

  1. Create a JSON Schema example.schema.json with an enum that contains special characters:
{
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": [
        "user.created",
        "email/verified",
        "2fa-enabled",
        "account*deleted"
      ]
    }
  }
}
  1. Generate TypeScript types using json-schema-to-typescript
yarn run json2ts --inferStringEnumKeysFromValues -i example.schema.json
  1. 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:

  1. Generate TypeScript types using json-schema-to-typescript with formatting disabled
yarn run json2ts --inferStringEnumKeysFromValues --format false -i example.schema.json
  1. 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-typescript version: 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:

  1. Quote keyName in generateStandaloneEnum (replace keyName with '${keyName}')
  2. (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
  3. Add a CLI option to opt-in this behaviour in order to avoid breaking changes in the generated code

quentinverlhac avatar Oct 25 '24 10:10 quentinverlhac