Skip to content

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

Open
@alita-moore

Description

@alita-moore

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"
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions