YousefED / YousefED/typescript-json-schema

Bug: enum with 1 value is not an enum in the JSON schema (evaluates as constant)

Open
#577 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.3k
Forks
332
PR merge metrics
No merged PRs in 30d

Description

Description

When you export an enum with one value it maps to a constant in the schema, but I expect it to map to an enum with one element.

Reproduction

see the reproduction in the following codesandbox: https://codesandbox.io/p/sandbox/lucid-kirch-r3rxlg

in short, the input typescript file is

export enum Test {
  apple = "apple",
}

export enum Test2 {
  apple = "apple",
  orange = "orange",
}

And the command to compile into json schema is

npx typescript-json-schema index.ts '*' -o out/generated.json --required --aliasRefs --refs --titles --propOrder --noExtraProps

Expected Results

I expect the results to be an enum with one element

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Test": {
            "enum": [
                "apple"
            ],
            "type": "string"
        },
        "Test2": {
            "enum": [
                "apple",
                "orange"
            ],
            "type": "string"
        }
    }
}

Actual Results

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Test": {
            "const": "apple",
            "type": "string"
        },
        "Test2": {
            "enum": [
                "apple",
                "orange"
            ],
            "type": "string"
        }
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript example in index.ts and run the npx typescript-json-schema command from the issue, or open the linked CodeSandbox. Compare the generated definitions for Test and Test2 with the expected JSON: the single-value Test should use an enum containing "apple" rather than a const.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.